magento : modify the quantity of specific product

2019-08-12 07:13发布

问题:

i m developping an extension for custom price this is my code

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;

    }




}

Observer :

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);
            }

    }

the probleme is :

1 - add product1 1000$; 2- add product2 2000$; that works fine 3 - add product2 2000$; now it erase product1 and put product2 qty=2 total 4000$ (ERROR !!!!)

the cart now is :

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

this is an image describing the probleme :

how to solve this ?

标签: magento