Magento的:修改车的特定产品的数量(magento : modify the quantity

2019-10-19 22:55发布

IM深化发展自定义价格的扩展,这是我的代码

item.php:

class WebDirect_CustomPrice_Model_Sales_Quote_Item 
extends Mage_Sales_Model_Quote_Item {

    public function representProduct($product) {
        $parentResult = parent::representProduct($product);

        //if parent result is already false, we already have a different product, exit.
        if ($parentResult === false) {
            return $parentResult;
        }

        $bool = "";

        $itemProduct = $this->getProduct();

        Mage::app()->getRequest()->getPost();       
        $customPrice = Mage::app()->getRequest()->getParam('custom_price');

        $cart = Mage::getModel('checkout/cart')->getQuote();
        foreach ($cart->getAllItems() as $item) {
            $productId = $item->getProductId();
            $productPrice = $item->getPrice();

            if($productId == $itemProduct->getId()){
                /*
                 * do our check for 'same product' here.
                * Returns true if attribute is the same thus it is hte same product
                */
                if ($productPrice == $customPrice) {
                    $bool = true; //same product
                    break;
                } else {
                    $bool = false; //different product
                }       
            }

        }return $bool;

    }




}

观察报:

class WebDirect_CustomPrice_Model_Observer {
    const MODULE_NAME = 'WebDirect_CustomPrice';
    public $test = false;
    public function convertPricespanToInput($observer = NULL) {
        if (!$observer) {
            return;
        }
        if ('product.info.simple' == $observer->getEvent()->getBlock()->getNameInLayout()) {
            if (!Mage::getStoreConfig('advanced/modules_disable_output/'.self::MODULE_NAME)) {
                $transport = $observer->getEvent()->getTransport();
                $block = new WebDirect_CustomPrice_Block_priceSpanToInput();
                $block->setPassingTransport($transport['html']);
                $block->toHtml();
            }
        }
        return $this;
    }

    /**
     * @param Varien_Event_Observer $observer
     */
    public function applyCustomPrice(Varien_Event_Observer $observer) {

        // @var $item Mage_Sales_Model_Quote_Item 
        $item = $observer->getQuoteItem();
        if ($item->getParentItem()) {
            $item = $item->getParentItem();
        }

        Mage::app()->getRequest()->getPost();

        $customPrice = Mage::app()->getRequest()->getParam('custom_price');
        $defaultCp = $item->getProduct()->getData('min_price');

            if($customPrice >= $defaultCp){

                $item->setCustomPrice($customPrice);
                $item->setOriginalCustomPrice($customPrice);
                $item->getProduct()->setIsSuperMode(true);
            }

    }

该probleme是:

1 - 添加产品1 $ 1000; 2 - 添加产品2 $ 2000; 工作正常3 - 添加产品2 $ 2000; 现在它擦除产品1和产品2放数量= 2总$ 4000(ERROR !!!!)

车现在是:

         - Product2 ------ Qty: 2 ---------- total: 4000$ (Where is Product1 i didn't remove it O_O!!)
         - Product2 ------ Qty: 1 ---------- total: 2000$

这是描述probleme图像:

如何解决这个问题?

文章来源: magento : modify the quantity of specific product in cart
标签: magento