简体   繁体   English

PHP脚本中的变量会影响Magento结帐

[英]Variables from PHP script affecting Magento checkout

I use the script below to set a flag when a product with a specific attribute is in the cart. 当具有特定属性的产品在购物车中时,我使用以下脚本设置标志。 My problem is that because I use this in the checkout I belive it conflicts with the other variables/arrays in the checkout and the grand total is double what it should be. 我的问题是,因为我在结帐中使用了此代码,所以我认为它与结帐中的其他变量/数组冲突,并且总计是应有的两倍。

My question is how do I pass the flag out of this script without all the other variables effecting the rest of the checkout. 我的问题是如何在所有其他变量都不影响其余结帐的情况下将标志从该脚本中传递出去。 I have tried unset($cart); 我尝试过unset($ cart); thinking this would help but with no success! 认为这会有所帮助,但没有成功!

(This code is placed at the top of my checkout.phtml file) (此代码位于我的checkout.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);
?>

I'm not extremely familiar with cart, but instead of 我对购物车不是很熟悉,但是

$cart = new Mage_Checkout_Model_Cart();
$cart->init();

Try 尝试

Mage::getSingleton('checkout/cart')

First and foremost: You should almost never use 'new'. 首先,最重要的是:您几乎永远不要使用“新”。 There's no real reason not to use Mage::getBlah . 没有真正的理由不使用Mage::getBlah

Secondly, the issue may lie in your using Mage::getModel() , I think you need to use Mage::getSingleton() . 其次,问题可能出在使用Mage::getModel() ,我认为您需要使用Mage::getSingleton()

The difference is that getModel will always give you a new object for that model while getSingleton will check to see if one already exists and return that instead. 区别在于, getModel将始终为该模型提供一个新对象,而getSingleton将检查是否已经存在并返回该对象。 This means you're getting the properties of the existing cart object, which I think is the issue. 这意味着您正在获取现有购物车对象的属性,我认为这是问题所在。 Try that and we'll go from there. 试试看,我们将从那里继续。

EDIT: This answer digs into the Magento internals to show you the difference between the two. 编辑: 这个答案深入到Magento内部,向您展示两者之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM