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

1 Answer 1

0

you can get custom option

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

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.