简体   繁体   English

以编程方式添加可配置产品并在Magento中指定尺寸

[英]Programmatically adding configurable product and specifying size in Magento

I've written some code in magento which will add a product to the cart if the total is above a certain amount. 我用magento编写了一些代码,如果总数超过一定数量,它将添加产品到购物车中。 At the moment, it's only working for Simple Products. 目前,它仅适用于简单产品。 I want to add a configurable product, and just tell it which simple product (which size) to select. 我想添加一个可配置的产品,然后告诉它要选择哪种简单产品(哪种尺寸)。

The following code is my params, as I've seen people use around the net: 以下代码是我的参数,因为我已经看到人们在网上使用:

$params = array(
     'product' => 369,
     'super_attribute' => array(
            22 => 12 ,
      ),
     'qty' => 1,
);

Where '22' is the id of an attribute, and '12' would be the specific attribute value. 其中“ 22”是属性的ID,“ 12”是特定的属性值。 I have no idea how to find my version of '22', I don't know whether a Simple Product counts as an attribute of a configurable product or not. 我不知道如何找到我的'22'版本,也不知道简单产品是否可配置产品的属性。

The code I call at the end is: 我在结尾处调用的代码是:

$product = new Mage_Catalog_Model_Product();
$product->load(369); 
$cart->addProduct($product, $params);                               
$cart->save();

Obvious once you know where to look. 一旦知道了要看的地方就很明显。 If you visit the configurable product's page, and check the source on the drop down menu, you will find both the attribute ID and the value ID. 如果您访问可配置产品的页面,并在下拉菜单上检查源,则将找到属性ID和值ID。

Happy programming :) 快乐编程:)

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

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