如何以编程方式将带有自定义选项的产品添加到Magento的购物车中?

时间:2014-05-23 21:27:19

标签: php magento

将需要自定义选项,它是下拉类型: 标题是捐赠 价格是1 价格类型是固定的

现在,它正在将该产品添加到购物车,但没有自定义选项。以下是自定义选项的屏幕截图。

https://s3.amazonaws.com/uploads.hipchat.com/62230/429611/n0AxrLBapiJZo3t/Screen%20Shot%202014-05-23%20at%206.09.59%20PM.png

   $id = '67'; // Replace id with your product id
                $qty = '1'; // Replace qty with your qty
                $_product = Mage::getModel('catalog/product')->load($id);

                $cart = Mage::getModel('checkout/cart');
                $cart->init();

                $params = array(
                    'product'=>$id,
                    'qty' => $qty,
                    'options' => array (
                        183 => array(
                            array(
                                'price'=> 1.00,
                                'title'=>'$1.00 Donation',
                                'price_type' => 'fixed',
                                'sku' => '',
                                'sort_order' => 10
                            )
                        )
                    )
                );

                $request = new Varien_Object();
                $request->setData($params);

                $cart->addProduct($_product, $request );
                $cart->save();
                Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

产品变量转储中的内容

 [_resourceName:protected] => catalog/product_option_value
                                [_resource:protected] => 
                                [_resourceCollectionName:protected] => catalog/product_option_value_collection
                                [_cacheTag:protected] => 
                                [_dataSaveAllowed:protected] => 1
                                [_isObjectNew:protected] => 
                                [_data:protected] => Array
                                    (
                                        [option_type_id] => 641
                                        [option_id] => 183
                                        [sku] => 
                                        [sort_order] => 10
                                        [default_title] => $1.00 Donation
                                        [store_title] => 
                                        [title] => $1.00 Donation
                                        [default_price] => 1.0000
                                        [default_price_type] => fixed
                                        [store_price] => 
                                        [store_price_type] => 
                                        [price] => 1.0000
                                        [price_type] => fixed

3 个答案:

答案 0 :(得分:2)

如果您已经使用了引用ID,则使用

下面..

要获取选项ID和选项值,请选中以下链接:Magento - Get Product options from $item

您需要在此处获取产品下拉选项值 如果您需要创建新报价,请使用以下添加

$QuoteId= Mage::getModel('checkout/cart_api')->create('default');

此处defaultstore code。 如果您已经有一个引用ID,则不需要添加上层代码

$arrProducts = array(
    array(
        "product_id" => $productId,
        "qty" => 5,
        "options" => array(         
                $optionId => $optinValueId
                 )
    )
);
Mage::getModel('checkout/cart_product_api')->add($QuoteId,$arrProducts,$storeId);

答案 1 :(得分:0)

我在下面的代码中完成了自定义选项。可能会帮助您设置自定义选项:

<?php 


        $productSku = "330471";
        $product = Mage::getModel('catalog/product');
        $productId = $product->getIdBySku( $productSku );
        $product->load($productId);

        if ($product->getId()) {
            if ($product->hasCustomOptions()) {
                foreach ($product->getOptions() as $o) {
                    $optionType = $o->getType();
                    Mage::log("Option Type = $optionType;");

                    if ($optionType == 'drop_down') {
                        $values = $o->getValues();
                        Mage::log("List of Drop down Custom Options:-");

                        foreach ($values as $k => $v) {
                            Mage::log("Array Key = $k;");
                            Mage::log("Array Values:-");
                            Mage::log($v);
                        }
                    }
                    else {
                        Mage::log("List of General Custom Options:-");
                        Mage::log($o);
                    }
                }
            }
        }
        else {
            Mage::log('This Product does not exist.');
        }

        $product = Mage::getModel("catalog/product")->load(939); //product id 1
        $i = 1;
        echo "<pre>";
        foreach ($product->getOptions() as $o) {
            echo "<strong>Custom Option:" . $i . "</strong><br/>";
            echo "Custom Option TITLE: " . $o->getTitle() . "<br/>"; //Colors
            echo "Custom Option TYPE: " . $o->getType() . "<br/>"; //drop_down
            echo "Custom Option Values: <br/>";
            $values = $o->getValues();
            foreach ($values as $v) {
                print_r($v->getData());
            }
            $i++;
            echo "<br/>";
        }

?>

答案 2 :(得分:0)

使用您的代码尝试以下代码

&#13;
&#13;
$params = array(
    'product' => 164,
    'related_product' => null,
    'bundle_option' => array(
        21 => 58,
        20 => 55,
        11 => 28,
        12 => array(
            0 => 31,
        ),
        13 => array(
            0 => 32,
            1 => 35,
        ),
    ),
    'options' => array(
        3 => 'olaaaaaaaa',
    ),
    'qty' => 2,
);
 
$cart = Mage::getSingleton('checkout/cart');
 
$product = new Mage_Catalog_Model_Product();
$product->load(164);
 
$cart->addProduct($product, $params);
$cart->save();
 
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
 
$message = $this->__('Custom message: %s was successfully added to your shopping cart.', $product->getName());
Mage::getSingleton('checkout/session')->addSuccess($message);
&#13;
&#13;
&#13;

&#13;
&#13;
$params = array(
    'product' => 164,
    'qty' => 2,
);
 
$cart = Mage::getSingleton('checkout/cart');
 
$product = new Mage_Catalog_Model_Product();
$product->load(164);
 
$cart->addProduct($product, $params);
$cart->save();
 
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
 
$message = $this->__('Custom message: %s was successfully added to your shopping cart.', $product->getName());
Mage::getSingleton('checkout/session')->addSuccess($message);
&#13;
&#13;
&#13;

如果您有任何疑问,请告诉我