2

the following observer script looks for selected configurable options, to work out the qty needed and adds a simple product (#9236) to the cart.

However, it only works for simple products. How can I get it to also get configurable products selected custom options?

class My_Observer
{
    public function add_product_qty(Varient_Event_Observer $obs)
    {
        $quote = $obs->getEvent()->getQuote();
        $getquoteitems = $obs->getQuoteItem();

        $custom = $obs->getQuoteItem();
        if ($custom->getParentItem()) {
            $custom = $custom->getParentItem();
        }
        $product_id=$custom->getProductId();

        $helper = Mage::helper('catalog/product_configuration');
        $optionList = $helper->getCustomOptions($getquoteitems);
        if (!empty($optionList[1])) {
            //Custom Options
            $qty = 0;
            $Onelogo = '1x Logo (Setup Required)';
            $Twologo = '2x Logo (Setup Required)';
            $Threelogo = '3x Logo (Setup Required)';

            if (in_array($Onelogo, $optionList[1])) {
                $qty = 1;
            }
            if (in_array($Twologo, $optionList[1])) {
                $qty = 2;
            }
            if (in_array($Threelogo, $optionList[1])) {
                $qty = 3;
            }
            if ($qty > 0) {
                // Mage init
                require_once(Mage::getBaseDir().'/app/Mage.php');
                umask(0);
                Mage::app('default');
                Mage::getSingleton('core/session', array('name' => 'frontend'));

                // Get customer session
                $session = Mage::getSingleton('customer/session');

                // Get cart instance
                $cart = Mage::getSingleton('checkout/cart');
                $cart->init();

                // Add a product (simple); id:12,  qty: 3
                $cart->addProduct(9236, $qty);
                $session->setCartWasUpdated(true);
            }
        }
    }
}
0

you can get custom option

$quote = $obs->getEvent()->getQuote();
foreach ($quote->getAllVisibleItems() as $item) {
    print_r($item->getOptions());
}
  • Thanks, but how would apply that to the above script? – windywah Oct 20 '15 at 13:17
  • What you want to achieve? – Qaisar Satti Oct 20 '15 at 13:21
  • The above script works for simple products, I just want it to also work for configurable products too. – windywah Oct 20 '15 at 13:29
  • That print_r will often lead to recursion. – Goose Mar 15 '17 at 19:14

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.