创建产品(可配置且简单)并设置自定义选项

时间:2014-04-28 22:17:48

标签: php magento

我尝试创建可配置产品,将其与简单产品相关联,并为简单产品添加自定义选项。 我已经成功创建了产品和关联,但我还没有能够为他们添加自定义选项。

我看到有人使用Mage::getModel('catalog/product_option_api')发布了一个问题,但我似乎无法找到该模型。 关于如何完成这项工作的任何想法?

我的代码如下,自定义选项部分位于底部附近。

$parentProduct = Mage::getModel('catalog/product');
$parentProduct->setAttributeSetId(4)
    ->setTypeId('configurable')
    ->setStockData(array(
        'manage_stock'  => 0,
        'min_sale_qty'  => 0,
        'max_sale_qty'  => 0,
    ))
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
    ->setTaxClassId(1)
    ->setCreatedAt(time())
    ->setName($row['name'])
    ->setSku($sku)
    ->setPrice($row['price'])
    ->setCategoryIds(array(2))
    ->setStatus(1)
    ->setWeight(1)
    ->setWebsiteIDs(array(1))
    ->setDescription($row['description'])
    ->setShortDescription($row['short_desc']);

try
{
    $parentProduct->save();

    $parent_id = $parentProduct->getId();
    echo 'Product created: ' . $parent_id . "\n";

    if (!empty($parent_id))
    {
        $color = 0;
        $child_ids = array();

        foreach ($row['children'] as $child_sku => $child)
        {
            $product = Mage::getModel('catalog/product');
            $product->setAttributeSetId(4)
                ->setTypeId('simple')
                ->setStockData(array(
                    'manage_stock'  => 0,
                    'min_sale_qty'  => 0,
                    'max_sale_qty'  => 0,
                ))
                ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                ->setTaxClassId(12)
                ->setCreatedAt(time())
                ->setName($row['name'] . ' ' . (($color == 0) ? 'Silver' : 'Gold'))
                ->setSku($sku . $child_sku)
                ->setPrice($row['price'])
                ->setcategoryIds(array(3))
                ->setStatus(1)
                ->setWeight(1)
                ->setWebsiteIDs(array(1))
                ->setDescription($row['description'])
                ->setShortDescription($row['short_desc'])
                ->setCanSaveCustomOptions(true);

            foreach ($child['options'] as $select_id => $selectrow)
            {
                $options = array(
                    'type'      => 'select',
                    'title'     => array(
                        'title' => $sku . '_CLASSIC',
                    ),
                );

                foreach ($selectrow['lines'] as $linetitle => $line)
                {
                    $options['type_title'][]['title'] = $linetitle;
                    $options['type_value'][]['min_chars'] = $line[0];
                    $options['type_value'][]['max_chars'] = $line[1];
                }

                $product->setCustomOptions($options);
            }

            $product->save();

            $color = ($color == 0) ? 1 : 0;
            $product_id = $product->getId();
            $child_ids[] = $product_id;
            echo 'Simple product created: ' . $product_id . "\n";
        }


        /** @var Mage_Catalog_Model_Product_Type_Configurable $resource */
        $resource = Mage::getResourceModel('catalog/product_type_configurable')
            ->saveProducts($parent_id, $child_ids);

1 个答案:

答案 0 :(得分:0)

试试这个并在$ value_arrays数组中传递你的选项值。

 $product->setAttributeSetId(4)
            ->setTypeId('simple')
            ->setStockData(array(
                'manage_stock'  => 0,
                'min_sale_qty'  => 0,
                'max_sale_qty'  => 0,
            ))
            ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
            ->setTaxClassId(12)
            ->setCreatedAt(time())
            ->setName($row['name'] . ' ' . (($color == 0) ? 'Silver' : 'Gold'))
            ->setSku($sku . $child_sku)
            ->setPrice($row['price'])
            ->setcategoryIds(array(3))
            ->setStatus(1)
            ->setWeight(1)
            ->setWebsiteIDs(array(1))
            ->setDescription($row['description'])
            ->setShortDescription($row['short_desc'])
            ->setHasOptions(1); 

$value_arrays = array();
$opt = Mage::getModel('catalog/product_option');
$opt->setProduct($product);
$value_arrays[] =  array('is_delete'     => 0,
             'title'         => 'title',
             'price_type'    => 'fixed',
             'price'         => 'price',
             'sku'           => 'product sku',
             'option_type_id'=> -1,);
$optionArray = array(
        'is_delete' => 0,
        'title' => 'title',
        'previous_group' => '',
        'previous_type' => '',
        'type' => 'drop_down',  //can be radio, drop_down, file, area...
        'is_require' => 0,
        'sort_order' => 0,
        'values' => $value_arrays );

$opt->addOption($optionArray);
$opt->saveOptions();
$product->Save();