Add option value to product, then to cart with Magento

后端 未结 4 1104
暗喜
暗喜 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 12:56

    You should write the input parameter for addproduct as the following format, it is tested by myself:

    $params = array(
        'product' => 1, // This would be $product->getId()
        'qty' => 1,
        'super_attribute' => array(
            34 => "value",
            35 => "other value",
            53 => "some other value"
        )
    );
    
    0 讨论(0)
  • 2021-01-02 13:00

    In Magento 1.7 you have to wrap the params array in a Varien Object.

                    $params = array(
                        'product' => $_fancypack->getId(),
                        'qty' => 1,
                        'options' => array(
                            $this->_getOptionId($_fancypack,'Product SKU') => $product->getId() .'/'. $product->getSku()
                        )
                    );
    
                    $request = new Varien_Object();
                    $request->setData($params);
    
                    $quote->addProduct($_fancypack, $request);
    
    0 讨论(0)
  • 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.

    <?php     
    include_once '../app/Mage.php';
    Mage::app();
    echo getProductOptionsIds($_GET['eventcode']);
    
    function getProductOptionsIds($sku) 
    {
    
        $ProductID = Mage::getModel('catalog/product')->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);
    }
    ?>
    
    0 讨论(0)
  • 2021-01-02 13:11

    You don't set the custom option on the product model, you pass it in through the second argument to $cart->addProduct($product, $params).

    The set up we have for a project, that requires an external app to add to the Magento cart, is to use a $params array of the following format:

    $params = array(
        'product' => 1, // This would be $product->getId()
        'qty' => 1,
        'options' => array(
            34 => "value",
            35 => "other value",
            53 => "some other value"
        )
    );
    

    The $params['options'] contains the custom option information. The keys are the custom option ids, you can see them if you inspect the custom options section of the product screen with Firebug, or similar.

    The $params['product'] may be redundant, I wrote this script a while ago for a much earlier version of Magento.

    Also, I'm fairly sure that the standard add to cart events will fire when you add this way, so you'll need to set them off yourself. There may be side effects.

    0 讨论(0)
提交回复
热议问题