I want to add a custom product(eg :cooler) to cart programmatically to cart when a product has custom option(as ice packing) or the cart items have custom option(as ice packing) and update the quantity of the custom added product if the total quantity of the products that have custom option is greater than some limit.

In my case if a product has ice packing as custom option and if the product as added to cart and add a product cooler to the cart and if the quantity is less than 12 then add 12 pint cooler and id it is greater then 12 then add 24 pint cooler .if it is greater than 24 the add a 12 and 24 pint cooler(ie 2 products) to cart, if greater than 36 remove 12 pintcooler and update the 24 pint cooler quantity to 2 and if greater than 48 tha add one 12 pint to the 24 pint cooler(2 qty) and so on..

Please help me to do this and please provide the code if you can.

1 Answer 1

  1. add product attribute required_ice_pack in the install script
if (!$this->getAttribute('catalog_product', 'required_ice_pack')) {
$this->addAttribute('catalog_product', 'required_ice_pack', array(
        'group' => 'General',
        'type' => 'int',
        'input' => 'select',
        'label' => 'Required Ice Pack',
        'global'=> Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible' => true,
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'user_defined' => false,
        'required' => false,
        'attribute_set' => 'Default',
        'source'  => 'eav/entity_attribute_source_boolean',
        'default' => false,
        'visible_on_front' => true,
        'used_in_product_listing' => true,

  1. add this in config.xml, section global. for access to attribute from quote
  1. add observer for event checkout_cart_save_after
 public function observerMethod()
        $quote = Mage::getSingleton('checkout/cart')->getQuote();

        $qty =0
        //calculate Qty
        foreach ($quote->getAllVisibleItems() as $item) {
            if ($item->getProduct()->getIceStorage())) {
                // add code for calculate qty, for product required ice pack 

        // calculate qty ice pack

        $icepackAQty = $icepackBQty = 0;
        foreach ($quote->getAllVisibleItems() as $item) {
            if ($item->getProduct()->getSku()=='sku_icepack_a_product') {

            if ($item->getProduct()->getSku()=='sku_icepack_b_product') {

  1. for add product to cart you can use
Mage::getSingleton('checkout/cart')->addProduct($product, array('qty' => $qty));
  1. for remove icepack

Your Answer

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

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