【问题标题】:Programmatically creating configurable products / pricing_value empty以编程方式创建可配置产品/pricing_value 为空
【发布时间】:2015-04-08 17:00:50
【问题描述】:

我正在尝试以编程方式创建可配置产品。我只对每个尺寸属性的价格有疑问。管理面板中的定价值始终为空。这是同样的问题,但没有答案Creating configurable products programmatically - pricing_value not saved我正在使用magento community 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();
}

【问题讨论】:

  • 可能需要定义基础货币?

标签: php magento magento-1.7


【解决方案1】:

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

Creating configurable products programmatically - pricing_value not saved

您需要将可配置产品数据添加到 $configurableAttributesData 中,如图所示。这是我在任何地方都看到的解决方案,直到下一步才对我有用。

将$configurableProductsData的结构改为

$configurableProductsData['11'] = 数组(

            '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

    );

删除第二个数组

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 2023-04-04
    • 2012-05-18
    • 2014-03-04
    • 2015-02-15
    • 2014-08-11
    • 1970-01-01
    相关资源
    最近更新 更多