以编程方式创建可配置产品-未保存price_value [英] Creating configurable products programmatically - pricing_value not saved

查看:48
本文介绍了以编程方式创建可配置产品-未保存price_value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义xml文件,并且大量导入了产品.我在可配置产品下有一些简单产品.一切运行良好,可配置产品是通过关联产品"标签中的简单产品创建的,但最后一件事仍然行不通:每种产品的价格.

I have a custom xml file and I make a massive import of the products. I have some simples products under configurables ones. All is working well, configurable products are created with the simple products the "associated products" tab but one last thing still doesn't work : the price of each product.

实际上,每个简单产品都有其自己的价格,正确的值已很好地保存在其属性中,但是在超级产品属性配置"面板中,这些值是空的.

Actually, Each simple product has its own price and the correct value is well saved in its attribute but in the "super product attribute configuration" panel, the values are empty.

当我手动填写价格字段时,它可以工作,但是显然必须由脚本以编程方式完成.

When I fill the price fields manually, it works but it obviously must be done by the script, programmatically.

这是我创建可配置产品的功能:

Here is my function to create the configurable product :

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;
}

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

And here is the code for linking simple products to this configurable product :

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();
}

欢迎任何帮助,谢谢!

推荐答案

大家好!在阅读了数千个答案后,我解决了它!

Hy all! After reading thousand of answer i solved it!

    $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);

我没有发布所有代码,但是我发现通过稍加修改就可以解决问题!

I dont post all the codes, but i see that with these little modification it solve the problem!

这篇关于以编程方式创建可配置产品-未保存price_value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆