简体   繁体   English

如何将自定义产品添加到magento购物车-请说明这些选项

[英]How to add a custom product to magento cart - Please explain these options

Can someone please explain how to properly configure and where to get the ID's, names, options, etc of this code? 有人可以解释如何正确配置以及在何处获得此代码的ID,名称,选项等吗?

I've been searching but other questions only leads to the solution of some mistake, but I need first to know where to get the numbers that are requered. 我一直在搜索,但是其他问题只会导致某些错误的解决,但是我首先需要知道从哪里获取需要查询的数字。

It's supposed to get a custom product and add it to the cart. 它应该获得定制产品并将其添加到购物车中。

$product_id = 12;
$id_opt_value = 12;
$final_opt_value = 12;

$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $product_id,
    'qty'     => 1,
    'options' => array(         
        $id_opt_value => '123456',
        $final_opt_value => 'black gloss finish',
     )
);

This is the complete code: 这是完整的代码:

require_once '../app/Mage.php';
umask(0);
/* not Mage::run(); */
Mage::app('default');

Mage::getSingleton("core/session", array("name" => "frontend"));

$product_id = 12;
$id_opt_value = 12;
$final_opt_value = 12;

$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $product_id,
    'qty'     => 1,
    'options' => array(         
        $id_opt_value => '123456',
        $final_opt_value => 'black gloss finish',
     )
);

$cart->addProduct($product, $params);
$cart->save();

Any help is appreciated. 任何帮助表示赞赏。

I guess I made it, but I'm still unsure how to check the custom ID. 我想我做到了,但是我仍然不确定如何检查自定义ID。

$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $product_id,
    'qty'     => 1,
    'options' => array(
        1 => 1, //(custom option ID like color) => (custom value for color, like red)
        2 => 3 //
     )
);

If anyone could inform me how to know exactly the numbers of options I'll chose as correct answer. 如果有人能告诉我如何确切知道options的数量,我会选择正确的答案。

The logic says that 1 => 1 is (first custom attribute => first value) but after that it's not the same because the 2 => 3 returned me as (second custom attribute => first value of that attribute). 逻辑上说1 => 1是(第一个自定义属性=>第一个值),但此后就不一样了,因为2 => 3返回我为(第二个自定义属性=>该属性的第一个值)。

This is probably because the fist attribute has 2 values so the second attribute gets the 3rd place and so on... 这可能是因为fist属性具有2个值,因此第二个属性获得了第三名,依此类推...

---EDIT--- - -编辑 - -

So I finally found the answer on this other question (if anyone needs it in the future) :) Magento - Get Custom Option Value details from Option Value ID 因此,我终于找到了另一个问题的答案(如果将来有人需要):) Magento-从“期权价值ID”获取自定义“期权价值”详细信息

暂无
暂无

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

相关问题 Magento 2使用自定义选项以编程方式将产品添加到购物车 - Magento 2 add product to cart programmatically with custom options 马杰托。 如何使用自定义选项以编程方式在购物车中添加简单产品 - Magento. How to add simple product in cart programmatically with custom options 如何以编程方式将带有自定义选项的产品添加到Magento中的购物车? - How to programmatically add a product with custom options to cart in Magento? Magento Soap API在购物车中添加具有自定义选项值的产品 - Magento soap api add product with custom options value in shopping cart Magento 2.3.5:使用自定义选项和价格将产品添加到购物车 - Magento 2.3.5: Add product to cart with custom options and price Magento。 如何以编程方式为自定义选项(复选框)设置数据并在购物车中添加简单产品? - Magento. How to set data for custom options(checkbox) programatically and add simple product in cart? 如何在可配置产品页面上添加自定义空购物车按钮:Magento - How to add a custom empty cart button on configurable product page:Magento Magento | 我如何在自定义模块中的购物车中添加产品? - Magento | How i can add a product in shopping cart in a custom module? 添加到购物车中Magento中的产品自定义选项 - Add to cart with product custom option in Magento 添加到购物车中带有magento虚拟产品的自定义选项 - add to cart with custom option for virtual product in magento
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM