简体   繁体   English

将选项值添加到产品,然后使用Magento添加到购物车

[英]Add option value to product, then to cart with Magento

I searched around for a while and only came up wit solutions that added whole new option sets to products in a Magento store. 我搜索了一会儿,才发现机智的解决方案为Magento商店的产品添加了全新的选项集。

What I'm trying to accomplish is a way to add a Simple Product to the cart. 我想要完成的是一种将简单产品添加到购物车的方法。 This Simple Product has some predifined custom options (free text fields) that has to be filled by a php function. 此简单产品具有一些预定义的自定义选项(自由文本字段),这些选项必须由php函数填充。

So, how can I do this? 那么,我该怎么做呢? Let's say I have a product with the ID "111" and a one custom option. 假设我有一个ID为“ 111”的产品,并且有一个自定义选项。

$qty = '1';
$product = Mage::getModel('catalog/product')->load("111");
// set option value in product model?

$cart = Mage::helper('checkout/cart')->getCart();
$cart->addProduct($product, $qty);
// set option value while passing product to car?

$cart->save();

Thanks in advance for any hinds. 预先感谢您的种种障碍。

BTW: setting option values via QueryString is relativly easy as seen here . BTW:通过查询字符串设置选项值是relativly容易,因为看到这里

You don't set the custom option on the product model, you pass it in through the second argument to $cart->addProduct($product, $params) . 您无需在产品模型上设置自定义选项,而是将其通过第二个参数传递给$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: 我们需要一个外部应用程序添加到Magento购物车中的项目的设置是,使用以下格式的$params数组:

$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. $params['options']包含自定义选项信息。 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. 这些键是自定义选项ID,如果您使用Firebug或类似工具检查产品屏幕的“自定义选项”部分,则可以看到它们。

The $params['product'] may be redundant, I wrote this script a while ago for a much earlier version of Magento. $params['product']可能是多余的,我前一段时间为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. 可能会有副作用。

In Magento 1.7 you have to wrap the params array in a Varien Object. 在Magento 1.7中,您必须将params数组包装在Varien对象中。

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

You should write the input parameter for addproduct as the following format, it is tested by myself: 您应该以以下格式编写addproduct的输入参数,该参数由我自己测试:

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

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. 当前答案的问题是,如果SKU相同,但magento不会添加第二个订单项,但选项与第一个不同。 If you want a 3" apple and a 4" apple you would like to have separate line items. 如果您想要3“苹果和4”苹果,则需要单独的订单项。 Or at least I do. 或者至少我愿意。

A HTTP call to the following URL 对以下URL的HTTP调用

/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. 那么以下PHP代码将告诉您。 And since we are using an HTTP call the code will return javascript ready JSON. 由于我们使用的是HTTP调用,因此代码将返回可使用javascript的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);
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM