首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以编程方式创建可配置产品-未保存pricing_value

以编程方式创建可配置产品-未保存pricing_value
EN

Stack Overflow用户
提问于 2014-12-17 20:11:01
回答 3查看 4.1K关注 0票数 0

我有一个自定义的xml文件,并且我进行了大量的产品导入。我在配置项下有一些简单的产品。一切正常,可配置的产品是通过简单的“相关产品”标签创建的,但最后一件事仍然不起作用:每种产品的价格。

实际上,每个简单的产品都有自己的价格,正确的值保存在它的属性中,但是在“超级产品属性配置”面板中,这些值是空的。

当我手动填充价格字段时,它可以工作,但显然必须通过脚本以编程方式完成。

下面是我创建可配置产品的函数:

代码语言:javascript
复制
protected function createConfigurableProductFromSimpleProduct($product, $flagshipID)
{
    $configurableProduct = $product->duplicate();
    $configurableProduct->getResource()->save($configurableProduct);
    $configurableProduct= Mage::getModel('catalog/product')->load($configurableProduct->getId());

    $configurableProduct->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
                     ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                     ->setSku($flagshipID)
                     ->setDefRef($product_id)
                     ->getTypeInstance()->setUsedProductAttributeIds(array($this->getAttributeId('root_colors'))); //attribute ID of attribute 'root_colors' in my store

    $configurableProduct->setName($configurableProduct->getName());
    $configurableProduct->setStatus(1);
    $configurableProduct->setStockData(array(
        'is_in_stock' => 1
    ));
    $configurableProduct->setUrlKey($configurableProduct->getName());
    $configurableProduct->save();

    return $configurableProduct;
}

下面是将简单产品链接到此可配置产品的代码:

代码语言:javascript
复制
protected function linkSimpleProductsToConfigurableProduct($simpleProducts, $configurableProduct)
{
    $configurableProductsData = array();
    foreach ($simpleProducts as $_product) {
        $_product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
        $_product->getResource()->save($_product);
        $configurableProductsData[$_product->getId()] = array( //[id] = id of a simple product associated with this configurable
            '0' => array(
                'label'         => $this->getAttributeRawValue($this->getAttributeId('root_colors'), $_product->getRoot()), //attribute label
                'attribute_id'  => $this->getAttributeId('root_colors'), //attribute ID of discriminator attribute in my store
                'value_index'   => $_product->getColor(),
                'pricing_value' => $_product->getPrice(),
                'is_percent'    => '0'
            )
        );
    }
    $configurableAttributesData = $configurableProduct->getTypeInstance()->getConfigurableAttributesAsArray();
    $configurableProduct->setCanSaveConfigurableAttributes(true);
    $configurableProduct->setConfigurableAttributesData($configurableAttributesData);
    $configurableProduct->setConfigurableProductsData($configurableProductsData);
    var_dump('saving configurable product after having link some simple products');
    $configurableProduct->save();
}

欢迎您的帮助,谢谢!

EN

回答 3

Stack Overflow用户

发布于 2015-05-20 23:19:17

大家好!在阅读了上千个答案后,我解决了这个问题!

代码语言:javascript
复制
    $cProduct->setCanSaveConfigurableAttributes(true); 
    $cProduct->setCanSaveCustomOptions(true);
    $cProductTypeInstance = $cProduct->getTypeInstance();
    $cProductTypeInstance->setUsedProductAttributeIds(array($attribute_id));

    $attributes_array = $cProductTypeInstance->getConfigurableAttributesAsArray();
    foreach($attributes_array as $key => $attribute_array) {
         $attributes_array[$key]['use_default'] = 0; 
         $attributes_array[$key]['position'] = 0;   
         if (isset($attribute_array['frontend_label'])) {
             $attributes_array[$key]['label'] = $attribute_array['frontend_label']; 
         } else {
             $attributes_array[$key]['label'] = $attribute_array['attribute_code']; 
         }

    }   

    $dataArray = array();
    foreach ($simpleProducts as $simpleArray) {
         $dataArray[$simpleArray['id']] = array( 
            "label" => $simpleArray['label'],
            "attribute_id" => $simpleArray['attr_id'], 
            "value_index" => $simpleArray['value'],
            "is_percent" => '0', 
            "pricing_value" => $simpleArray['price'] 
        ); 
    }   


// MAIN MOD! Here i prepare an array for attributesData.
        $valuesArray = array();
        foreach($dataArray as $data){
            $valuesArray[] = $data; 
        }

    // MAIN MOD! 
    // this is not the best, but in my case I've only one attribute.
    $attributes_array[0]['values'] = $valuesArray;
    $cProduct->setConfigurableProductsData($dataArray);
    $cProduct->setConfigurableAttributesData($attributes_array);

我没有张贴所有的代码,但我看到,通过这些小的修改,它解决了问题!

票数 1
EN

Stack Overflow用户

发布于 2015-02-02 20:19:33

我遇到了完全相同的问题,我怀疑这是由于Magento 1.9中的某些东西造成的。我已经尝试了几个代码变通方法,但它们都还不起作用。嗯,和你一样的郁闷。

我不会提供更多的评论,而是只提供一个答案;)

我深入研究了一下,发现setConfigurableProductsData()调用旨在设置产品信息,但是"pricing_value“标志根本不起作用(至少它在Magento1.9中不起作用)。该调用仅用于标识属于此可配置产品的简单产品。相反,需要使用setConfigurableAttributesData()调用来定义简单产品和定价之间的关系。

下面的代码适用于我:它将特定选项值(用value_index标识)的定义与特定价格(pricing_value)一起作为属性数据而不是产品数据的一部分。

代码语言:javascript
复制
$configurableAttributeSizeValues = array();
foreach($simpleProducts as $simpleProduct) {
    $configurableAttributeSizeValues[] = array(
        'label' => $simpleProduct->getAttributeText('size'),
        'value_index' => $simpleProduct->getSize(),
        'is_percent' => false,
        'pricing_value' => $simpleProduct->getPrice(),
    );
}
$configurableAttributeSize = array(
    'id' => null,
    'label' => 'Size',
    'frontend_label' => 'Size',
    'attribute_id' => $attribute->getId(),
    'attribute_code' => 'size',
    'values' => $configurableAttributeSizeValues,
    'position' => 0,
);
$configurableAttributesData = array($configurableAttributeSize);

$configurableProductsIds = array();
foreach($simpleProducts as $simpleProduct) {
    $configurableProductsIds[$simpleProduct->getId()] = $simpleProduct->getId();
}

$product->setConfigurableProductsData($configurableProductsIds);
$product->setConfigurableAttributesData($configurableAttributesData);
$product->setCanSaveConfigurableAttributes(true);

在这段代码投入使用之前,我已经加载了一个名为$simpleProducts的产品集合和一个属性$attribute。如果要将多个属性加载到同一产品,可以将它们添加到$configurableAttributesData数组中。

您在代码中创建的原始$configurableProductsData仍然可以使用,但是因为它的大部分用途被移到了attributes数组中,所以您也可以只创建一个简单的Product ID数组。这就是我对$configurableProductsIds所做的。

希望这对你也有帮助。

票数 0
EN

Stack Overflow用户

发布于 2015-12-19 20:53:31

以下是对我有效的方法(为了清晰起见,重复了一些代码)。要保存pricing_value,需要同时使用setConfigurableProductsDatasetConfigurableAttributesData

代码语言:javascript
复制
$configurableAttributesData = $product->getTypeInstance()->getConfigurableAttributesAsArray();
$product->getTypeInstance()->setUsedProductAttributeIds(array($attribute->getId()));
$product->setCanSaveConfigurableAttributes(true);
$configurableProductsData[$childId] = array(
    $childProductId => array(
        'label' => $childProduct->getAttributeText($attribute->getAttributeCode()),
        'attribute_id' => $attribute->getId(),
        'value_index' => $optionId, 
        'is_percent' => false, 
        'pricing_value' => $childProduct->getPrice() 
    )
);
$configurableAttributesData[0]['values'][] = array(
    'label' => $childProduct->getAttributeText($attribute->getAttributeCode()),
    'attribute_id' => $attribute->getId(),
    'value_index' => $optionId,
    'is_percent' => false, 
    'pricing_value' => $childProduct->getPrice() 
);
$configurableAttributesData[0]['attribute_id'] = $attribute->getId();
$product->setConfigurableProductsData($configurableProductsData);
$product->setConfigurableAttributesData($configurableAttributesData);
$product->save();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27525336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档