如何清洁和更好地遵循"斗志旺盛" php代码:
<?php
error_reporting(E_ALL);
require_once('app/Mage.php');
Mage::init();
Mage::getSingleton("core/session", array("name" => "frontend"));
$productList = array(
array(),
array(),
array()
);
$count = Mage::getSingleton('checkout/session')->getQuote()->getItemsQty();
$cart = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$name = array();
$qty = array();
$price = array();
for ($i = 0; $i < $count; $i++) {
$name[$i] = $cart[$i]->getProduct()->getName();
$qty[$i] = $cart[$i]->getQty();
$price[$i] = $cart[$i]->getProduct()->getPrice();
$productList[$i] = array(
$name[$i],
$qty[$i],
$price[$i]
);
}
print_r($productList);
我想知道,如何美化这样的东西。可能是我应该使用更短的数组初始化形式,在这种情况下使用一些标准的php函数等等。
答案 0 :(得分:1)
使用下面的代码
<?php
error_reporting(E_ALL);
require_once('app/Mage.php');
Mage::init();
Mage::getSingleton("core/session", array("name" => "frontend"));
$productList = array();
$quote = Mage::getSingleton('checkout/session')->getQuote();
foreach ($quote->getAllItems() as $item) {
$productList[]=array($item->getName(),$item->getQty(),$item->getPrice());
}
答案 1 :(得分:0)
我猜这种方法好一点
<?php
error_reporting(E_ALL);
require_once("app/Mage.php");
function setData($acc, $item)
{
$acc[] = [
$item->getProduct()->getName(),
$item->getQty(),
$item->getProduct()->getPrice()
];
return $acc;
}
Mage::init();
Mage::getSingleton("core/session", ["name" => "frontend"]);
print_r(array_reduce(Mage::getSingleton("checkout/session")->getQuote()
->getAllVisibleItems(), "setData", []));
答案 2 :(得分:0)
ini_set('display_errors', '1');
require_once('app/Mage.php');
Mage::app('default');
$storeId = 0;
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load($storeId));
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
foreach ($products as $product) {
print_r($product->getdata());
}