1

I found a problem using a custom "gift" extension for magento. It let the user choose a gift product in the checkout cart page. So the user will find under the cart a product grid and can select max 1 product to get as gift and then add it to cart.

My problem is that after the user add product he's still able to edit the qty. So he can order 10 items of product as gift.

I need to force user to don't edit the product quantity for the "gift" products in cart.

The products listed as "Gifts" can be also purchased as regular products so that rule has to be applied on the "Gift" situation.

Actually the function "add cart" of the extension is this one:

public function addAction()
{   
    if ($this->getRequest()->isGet()) {
        $gift_id = $this->getRequest()->getParam('gift', 0);
        if($gift_id){
            if(!Mage::helper('gifts')->isGiftUsed() && in_array($gift_id, Mage::helper('gifts')->getGiftsIds())){
                $quote = Mage::getSingleton('checkout/session')->getQuote();
                $cart = Mage::getModel('checkout/cart');
                $product = new Mage_Catalog_Model_Product();
                $product->load($gift_id);

                $cart->addProduct($product, 1);
                $cart->save();

                foreach ($quote->getAllItems() as $item) {

                    if ($item->getProductId() == $gift_id) {
                        $item->setCustomPrice(0);
                        $item->setOriginalCustomPrice(0);
                        //$item->setOption('inventory_max_sale_qty',1);
                    }
                }

                $cart->init();
                $cart->save();          

                Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
            }
        }
    }
    $this->_redirect('checkout/cart');
}

thank you

1 Answer 1

1

Try editing the product that is the gift.

On the 'Inventory' tab check the 'Maximum Qty Allowed in Shopping Cart' value. Try setting this value to '1' for the gift product.

UPDATE:

To only restrict the quantity if it is a gift:

if ($item->getProductId() == $gift_id) {
    $item->setMaxSaleQty(1);
    $item->setCustomPrice(0);
    $item->setOriginalCustomPrice(0);
}

UPDATE It occurred to me you may also need to modify the setUseConfigMaxSaleQty parameter like this (although I haven't tested so I'm not sure):

$item->setUseConfigMaxSaleQty(1);
$item->setMaxSaleQty(1);
3
  • thank you Tim but the "Gift" product can be also purchased as regular product. So the rule has to be applied only if it is used as gift May 18, 2015 at 14:54
  • Please see update above and let me know if that works for you. @LucaCoppola May 18, 2015 at 15:10
  • it works thank you. Do you if there is a chance to lock the buttons to increase or decrease like in the following image? [image] (s12.postimg.org/mluwrrhrh/…). May 19, 2015 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.