0
votes

I am trying to create order programmatically.

There are configurable products having single base attribute i.e. Size. and some of configurable products have multiple (more than one) associated product having same size value.

Now, attempting to add such configurable product with particular size. But magento assigns the sibling/child/associated product which would encountered first! and hence, deserving SKU/sibling dropped to create order with.

I followed code from below links:

http://www.blog.plazathemes.com/archives/2149

http://pravams.com/2011/11/11/magento-create-order-programmatically/

Is there any way to add config product with particular SKU ? (something like acceptable parameters in "addProduct" method etc).

2

2 Answers

1
votes

I face similar problem when adding configurable product to create an order/quote programmatically. You need to set super_attribute in option array when you add product. The value must be containing attribute which distinguishes the associated products (e.g: color, size). So, it will be something like this:

$buyOptions = array(
    'qty' => 1,
    'super_attribute' => array($attributeId => $attributeValue) //ex: array(131 => 53)
);
$quote->addProduct($product, new Varien_Object($buyOptions));

I don't know how if you want to choose associated product by their sku, but above code will work if you just want to choose particular associated product from configurable one. I believe it is the same in essence.

ps: I answer this here because in magento stackexchange it marked as duplicate (mistakenly, I think).

0
votes

If you have the child's sku, you can do :

$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', 'mysku-123');  

If you have the size :

$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
foreach ($childProducts as $child) {
    if ($child->getSize()==$size){
       //add the product to the order
    }
}

and then you loop in the children to find the size.