Add option value to product, then to cart with Magento

后端 未结 4 1120
暗喜
暗喜 2021-01-02 12:33

I searched around for a while and only came up wit solutions that added whole new option sets to products in a Magento store.

What I\'m trying to accomplish is a wa

4条回答
  •  暖寄归人
    2021-01-02 13:05

    The problem with the current answer is that magento will not add a second line item if the SKU is the same but the options are distinct from the first. If you want a 3" apple and a 4" apple you would like to have separate line items. Or at least I do.

    A HTTP call to the following URL

    /store/checkout/cart/add?product=23&qty=1&options[41]=4
    

    followed by

    /store/checkout/cart/add?product=23&qty=1&options[41]=3
    

    will add two line items.

    But still this is just half of the battle, what do these option codes stand for?? Well the following PHP code will tell you. And since we are using an HTTP call the code will return javascript ready JSON.

    getIdBySku($sku);
        $Product = Mage::getModel('catalog/product')->load($ProductID);
    
        $config = array();
        $config['ProductID'] = $ProductID;
    
        foreach ($Product->getOptions() as $option) {
              // @var $option Mage_Catalog_Model_Product_Option 
            if ($option->getGroupByType() == Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) {
                $_tmpValues = array();
                foreach ($option->getValues() as $value) {
                    // @var $value Mage_Catalog_Model_Product_Option_Value 
                    $_tmpValues[$value->getTitle()] = $value->getId();
                }
    
                $config[$option->getTitle().'list'] = $option->getId();
                $optionValue = $_tmpValues;
            } else {
                $optionValue = $option->getId();
            }
            $config[$option->getTitle()] = $optionValue;
        }
        return json_encode($config);
    }
    ?>
    

提交回复
热议问题