次のようにCart Controllerを上書きすることにより、カスタムオプションを使用して複数のシンプルな製品を追加できます。
CartController.phpファイルをここに配置しました:https :
//github.com/svlega/Multiple-Products-AddtoCart
//Programatically Adding multiple products to cart
$productArray = array(
array(
'product_id' => 7,
'qty' => 2,
'custom_options' => array(
'size' => 'XL'
)
),
array(
'product_id' => 1,
'qty' => 1,
'custom_options' => array(
'color' => 'Red'
)
)
);
// Prepare cart products
foreach ($productArray as $params) {
if (isset($params['product_id'])) {
// Load product
$product = Mage::getModel('catalog/product')->load($params['product_id']);
if ($product->getId()) {
// If product is configurable and the param options were specified
if (isset($params['custom_options'])) {
// Get options
$options = $product->getOptions();
foreach ($options as $option) {
/* @var $option Mage_Catalog_Model_Product_Option */
if ($option->getGroupByType() == Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) {
$product_options[$option->getTitle()] = $option->getId();
if(array_key_exists($option->getTitle(),$params['custom_options'])){
$option_id = $option->getId();
echo '<br>Did'.$id = $option->getId().'Dlabe'.$option->getTitle();
foreach ($option->getValues() as $value) {
/* @var $value Mage_Catalog_Model_Product_Option_Value */
if($value->getTitle()== $params['custom_options'][$option->getTitle()]){
echo 'id'.$id = $value->getId().'labe'.$value->getTitle();
$params['options'][$option->getId()]=$value->getId();
}
}
}
}
}
}
try{
$cart = Mage::getModel('checkout/cart');
$cart->addProduct($product, $params);
$cart->save();
}catch(Exception $e) {
Mage::log($e->getMessage());
}
}
}
}