我使用下面的脚本在购物车中有特定属性的产品时设置一个标志。我的问题是,因为我在结帐中使用了这个,我相信它与结帐中的其他变量/数组冲突,并且总量是应该的两倍。
我的问题是如何在不影响其余签出的所有其他变量的情况下将标志传递出该脚本。我试过unset($cart);本以为会有帮助,但没有成功!
(此代码位于结帐的顶部。phtml文件)
<?php
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addFieldToFilter(
array(
array('attribute'=> 'WWHAM','eq' => '1')
)
);
$exemptProducts = array();
$exemptProductFound = false;
foreach ($collection as $product) {
$exemptProducts[] = $product->getId();
}
// now let's check if any are in the basket
$cart = new Mage_Checkout_Model_Cart();
$cart->init();
foreach ($cart->getItems() as $item) {
if(in_array($item->getProductId(), $exemptProducts)) {
// to make it simple, just set a flag
$exemptProductFound = true;
}
}
unset($cart);
?>
我对购物车不是很熟悉,但是代替
$cart = new Mage_Checkout_Model_Cart();
$cart->init();
试
Mage::getSingleton('checkout/cart')
首先,你应该永远不要用new。没有理由不使用Mage::getBlah
。
其次,问题可能在于你使用Mage::getModel()
,我认为你应该使用Mage::getSingleton()
。
不同之处在于getModel
将始终为该模型提供一个新对象,而getSingleton
将检查是否已经存在并返回该对象。这意味着您将获得现有购物车对象的属性,我认为这是问题所在。试试这个,我们就从这里开始。
编辑:这个答案深入到Magento的内部,向您展示两者之间的区别。