0

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.

0
  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,
        'is_configurable'=>false,
    ));

}
  1. add this in config.xml, section global. for access to attribute from quote
<sales>
  <quote>
        <item>
            <product_attributes>
              <required_ice_pack/>
            </product_attributes>
        </item>
    </quote>
</sales>
  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 
                $qty+=$item->getQty();
            }
        }

        // calculate qty ice pack

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

            if ($item->getProduct()->getSku()=='sku_icepack_b_product') {
                $icepackBQty+=$item->getQty();        
            }
        }

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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