以编程方式创建可配置产品/ pricing_value为空

时间:2015-04-08 17:00:50

标签: php magento magento-1.7

我正在尝试以编程方式创建可配置产品。我只有每个尺寸属性的价格问题。在管理面板中,pricing_value总是空的。这是同样的问题,但没有答案Creating configurable products programmatically - pricing_value not saved我正在使用magento社区1.9.1

try {
    $configProduct = new Mage_Catalog_Model_Product();
    $configProduct->setSku('dp_' . strtotime('now'))
        ->setAttributeSetId(9)
        ->setTypeId('configurable')
        ->setName('Some cool product name')
        ->setCategoryIds(array(3))
        ->setWebsiteIDs(array(1))
        ->setDescription('Full description here')
        ->setShortDescription('Short description here')
        ->setPrice(39.99)
        ->setWeight(1.0000)
        ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
        ->setStatus(1)
        ->setTaxClassId(0)
        ->setMediaGallery (array('images' => array (), 'values' => array ()))
        ->addImageToMediaGallery($path, $mediaAttribute, false, false )
        ->setStockData(array(
            'is_in_stock' => 1,
            'qty' => 999,
            'use_config_manage_stock' => 1,
            'is_salable' => 1
        ))
        ->setCreatedAt(strtotime('now'));

        $configProduct->setCanSaveConfigurableAttributes(true);
        $configProduct->setCanSaveCustomOptions(true);
        $configProduct->getTypeInstance()->setUsedProductAttributeIds(array(132)); //attribute ID of attribute 'size' in my store
        $configurableAttributesData = $configProduct->getTypeInstance()->getConfigurableAttributesAsArray();


        $configProduct->setConfigurableAttributesData($configurableAttributesData);

        $configurableProductsData = array();

        $configurableProductsData['10'] = array(
            '0' => array(
                'label' => (string)Mage::getModel('catalog/product')->load(10)->getName(),
                'attribute_id' => '132', //attribute ID of attribute 'size' in my store
                'value_index' => '3', //value of 'X' index of the attribute 'size'
                'is_percent' => false, //fixed/percent price for this option
                'pricing_value' => '21.00' //value for the pricing
            )
        );
        $configurableProductsData['11'] = array(
            '0' => array(
                'label' => (string)Mage::getModel('catalog/product')->load(11)->getName(),
                'attribute_id' => '132', //attribute ID of attribute 'size' in my store
                'value_index' => '4', //value of 'XL' index of the attribute 'size'
                'is_percent' => false, //fixed/percent price for this option
                'pricing_value' => '21.00' //value for the pricing
            )
        );
        $configProduct->setConfigurableProductsData($configurableProductsData);
        $configProduct->save();
        $createdProductUrl = Mage::getModel('catalog/product')->loadByAttribute('sku', $configProduct->getSku())->getUrlPath();
    echo json_encode(array("createdProductUrl"=>"http://magento.dev/index.php/test/" . $createdProductUrl));
} catch (Exception $ex) {
    echo $ex->getMessage();
}

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,环顾四周,但我找到了解决方案。从这里想出来

Creating configurable products programmatically - pricing_value not saved

您需要将可配置产品数据添加到$ configurableAttributesData,如图所示。这是我到处看到的解决方案,直到下一步才能为我工作。

将$ configurableProductsData的结构更改为

$ configurableProductsData ['11'] = array(

            'label' => (string)Mage::getModel('catalog/product')->load(11)->getName(),
            'attribute_id' => '132', //attribute ID of attribute 'size' in my store
            'value_index' => '4', //value of 'XL' index of the attribute 'size'
            'is_percent' => false, //fixed/percent price for this option
            'pricing_value' => '21.00' //value for the pricing

    );

删除第二个数组