可配置产品的价格不变


11

我创建了带有color属性的可配置产品“ bag”。颜色:蓝色,红色

在此处输入图片说明 我错过了在创建产品本身时增加价格的机会。

因此,当我更改选项的颜色时,前端价格不会改变。

在此处输入图片说明

请任何人帮助我通过代码解决。


您是否已添加价格并重新索引了目录并清除了缓存?
Amasty 2014年

我想添加更多1500产品。我想写一个自动化脚本来增加价格。
Manikandan 2014年

您可以通过自定义脚本或导入添加产品,然后为我认为的基本目录和价格运行重新索引。
Amasty 2014年

Answers:


6
<?php

class Websanity_Cataloginventory_Adminhtml_StockController extends Mage_Adminhtml_Controller_action
{
    /* 
     * initial layout
    */
    private function _init(){
        return $this->loadLayout();
    }

    /* 
     *
    */
    public function indexAction() {
        $this->_init();
        $this->renderLayout();

    }
    public $productIdsExcel = array();  
    public $productsExcel = array();
    public $updatedSku = array();
    public $unUpdatedSku = array();

    /* 
     *  Import product with csv file
    */
    public function importCatalogInventoryAction(){

        try
        {       
            $path = Mage::getBaseDir() . DS . 'var' . DS . 'websanity' . DS . 'tmp' . DS ;  //desitnation directory     
            $fname = $_FILES['stockfile']['name'];

            $arrayData = array();
            $file_path= $path . $fname;
            $arrayData = $this->excelToArray($file_path);

            foreach($arrayData as $row)
            {
                $productSku = $row["sku"];
                $attribute = $row["attribute"];
                $attributeValue = $row['attribute_value'];
                $price = $row['attribute_price'];

                $attributeId = $this->getAttributeId($attribute);   

                $productId = $this->getProductId($productSku);

                //Product-id collection from excel

                $this->productIdsExcel[] = $productId;

                //Price for each product and it attrivbutevalues collection from excel

                $this->productsExcel[$productId][$attribute][$attributeValue] = $price;


                $optionId = $this->attributeOption($attributeId, $attributeValue);


                $superAttrbuteId = $this->getSuperAttributeId($productId, $attributeId);

                $addPrices = $this->addPrices($superAttrbuteId, $optionId, $price);

            }


            $this->productIdsExcel = array_unique($this->productIdsExcel);
            $productIds = $this->productIdsExcel;

            foreach($productIds as $pid){

                $_product = Mage::getModel("catalog/product")->load($pid);
                $prod = $this->getAssociatedProducts($_product);

                foreach($prod as $simpleprod)
                {

                    if($simpleprod->getData('LIGHTSPEED_PRODUCT_SIZE'))
                    {
                        $sizeId = $simpleprod->getData('LIGHTSPEED_PRODUCT_SIZE');
                        $sizeAttributeValues = $this->getAttributeValues($sizeId);
                        foreach($sizeAttributeValues as $size){
                            $sizeValue = $size['value'];
                        }
                        $sizeAddtional = $this->productsExcel[$pid]['size'][$sizeValue];
                        $updatedSku[] = $simpleprod->getSku() ."- Size";
                    }
                    else
                    { 
                        $unUpdatedSku[] = $simpleprod->getSku() ."- Size";
                    }

                    if($simpleprod->getData('LIGHTSPEED_PRODUCT_COLOR'))
                    {
                        $colorId =$simpleprod->getData('LIGHTSPEED_PRODUCT_COLOR');
                        $colorAttributeValues = $this->getAttributeValues($colorId);
                        foreach($colorAttributeValues as $color){
                            $colorValue = $color['value'];
                        }
                        $colorAddtional = $this->productsExcel[$pid]['color'][$colorValue];
                            $updatedSku[] = $simpleprod->getSku() ."- Color";
                    }

                    else
                    { 
                        $unUpdatedSku[] = $simpleprod->getSku() ."- Color";
                    }

                    $base = $_product->getPrice();
                    $basePriceAddition = $base + $colorAddtional + $sizeAddtional;
                    $simpleProductPrice = $simpleprod->setPrice($basePriceAddition)->save();

                }
            }
            $update = "";
            $update .= "<h2>Records Updated</h2>";
            foreach($updatedSku as $updated)
            {
                $update .= $updated ."<br/>";

            }

            $update .= "<h2>Records Not Updated</h2>";
            foreach($unUpdatedSku as $unUpdated)
            {
                $update .= $unUpdated ."<br/>";

            }
            Mage::getSingleton('adminhtml/session')->setMyValue($update);
            $this->_redirectUrl( Mage::helper('adminhtml')->getUrl('*/adminhtml_stock/') );
        }
        catch (Exception $e)
            {
                Mage::getSingleton('adminhtml/session')->addError( $e->getMessage() );
            }
    }


    public function getAssociatedProducts($productId)
    {
        try{
            $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($productId);
            $prod = $conf->getUsedProductCollection()
            ->addAttributeToSelect('*')
            ->addFilterByRequiredOptions();
                return $prod;
        }
        catch(Exception $e){
            echo $e->getMessage();
        }
    }


    public function getAttributeValues($attributeId)
    {
        try{
            $resource = Mage::getSingleton('core/resource');
            $readConnection = $resource->getConnection('core_read');
            $table = $resource->getTableName('eav/attribute_option');
            $query = 'SELECT value FROM xyzeav_attribute_option_value WHERE option_id=' .$attributeId;

            $results = $readConnection->fetchAll($query);
            return $results;
        }
        catch(Exception $e){
            echo $e->getMessage();

        }
    }




    public function excelToArray($file_path)
    {
        try{
            chmod($file_path,0777);
            error_reporting(E_ALL ^ E_NOTICE);
            require_once Mage::getBaseDir().'/excel_reader2.php';

            $excel_reader = new Spreadsheet_Excel_Reader();
            $excel_reader->setUTFEncoder('iconv');
            $excel_reader->setOutputEncoding('CP1251');
            $file=$excel_reader->read($file_path,"UTF-16");
            $file_row=2;
            $column_count=$excel_reader->sheets[0]['numCols'];
            $row_count=$excel_reader->sheets[0]['numRows'];     
            $excelData = array();
            for($file_row;$file_row<=$excel_reader->sheets[0]['numRows'];$file_row++) 
            {   

                $excelData[$file_row]["sku"] = $excel_reader->sheets[0]['cells'][$file_row][1];
                $excelData[$file_row]["attribute"] = $excel_reader->sheets[0]['cells'][$file_row][2];
                $excelData[$file_row]["attribute_value"] = $excel_reader->sheets[0]['cells'][$file_row][3];
                $excelData[$file_row]["attribute_price"] = $excel_reader->sheets[0]['cells'][$file_row][4];
                if($excelData == "")
                {
                    break;
                }

            }
            return $excelData;
        }
        catch(Exception $e){
            echo $e->getMessage();
        }
    }


    public function attributeOption($attributeId, $color)
    {
        try{
            $resource = Mage::getSingleton('core/resource');
            $readConnection = $resource->getConnection('core_read');
            $table = $resource->getTableName('eav/attribute_option');

            $query = 'SELECT option_id FROM xyzeav_attribute_option_value WHERE value="'.$color.'" AND option_id in (SELECT option_id FROM ' . $table . ' WHERE attribute_id = '
            . (int)$attributeId.')';

            $results = $readConnection->fetchAll($query);
            return $results[0]['option_id'];
        }
        catch(Exception $e){
            echo $e->getMessage();
        }
    }


    public function getSuperAttributeId($productId, $attr1)
    {
        try{
            $getSuperAttributeId = Mage::getModel('catalog/product_type_configurable_attribute')->getCollection()
            ->addFieldToFilter('product_id', $productId)
            ->addFieldToFilter('attribute_id', $attr1)->getData();

            return  $getSuperAttributeId[0]['product_super_attribute_id'];                 
        }
        catch(Exception $e){
            echo $e->getMessage();
        }

    }


    public function addPrices($superAttrbuteId, $optionId, $price)
    {
        try{
            $resource = Mage::getSingleton('core/resource');
            $readConnection = $resource->getConnection('core_read');
            $writeConnection = $resource->getConnection('core_write');

            $query = 'SELECT * FROM xyzcatalog_product_super_attribute_pricing WHERE product_super_attribute_id='.$superAttrbuteId.' AND value_index='.$optionId;
            $results = $readConnection->fetchAll($query);

            $getExistingPrice = $results[0]['value_id'];

            if(!$getExistingPrice)
            {
                $insertQuery = 'INSERT INTO xyzcatalog_product_super_attribute_pricing (product_super_attribute_id, value_index, is_percent, pricing_value, website_id) VALUES('.$superAttrbuteId.','.$optionId.',"0",'.$price.',"0")';
                $QueryValue = $writeConnection->query($insertQuery);
                if(!$QueryValue)
                {
                    $result = "Mismatch Values";
                }
                else
                {
                    $result = "Inserted";
                }
            }
            else{

                $new_query = 'UPDATE xyzcatalog_product_super_attribute_pricing SET pricing_value='.$price.' WHERE product_super_attribute_id='.$superAttrbuteId.' AND value_index='.$optionId;
                $QueryValue = $writeConnection->query($new_query);

                if($QueryValue)
                {
                    $result = "Mismatch Values";
                }
                else
                {
                    $result = "Updated";
                }
            }
            return $result;
        }
        catch(Exception $e){
            echo $e->getMessage(addPrices,'Mismatch Values');
        }
    }

    function printError($fromFunction, $message){
        echo "<hr />";
            echo $fromFunction;
            echo $message;
        echo "<hr />";
    }

    public function getProductId($productSku)
    {
        try{
            $productId = Mage::getModel('catalog/product')->getCollection()
            ->addFieldToFilter('type_id', 'configurable')                           
            ->addFieldToFilter('sku', $productSku)->getFirstItem()->getId();
                return $productId;

        }
        catch(Exception $e){
            echo $e->getMessage();
        }
    }


    public function getAttributeId($attribute)
    {
        try{
            $resource = Mage::getSingleton('core/resource');
            $readConnection = $resource->getConnection('core_read');
            $superQuery ='select b.attribute_id from xyzcatalog_product_super_attribute_label a inner join xyzcatalog_product_super_attribute b on a.product_super_attribute_id  = b.product_super_attribute_id where a.value ="'.$attribute.'"' ;

            $superAttribute = $readConnection->fetchAll($superQuery);
            foreach($superAttribute as $super)
            {
                $superId=$super['attribute_id'];
            }
            return $superId;
        }
        catch(Exception $e){
            echo $e->getMessage();
        }
    }


    public function getParentProductId()
    {
        try{
            $resource = Mage::getSingleton('core/resource');
            $readConnection = $resource->getConnection('core_read');
            $childQuery = 'select product_id from xyzcatalog_category_product_index  where is_parent = 1';
            $childResults = $readConnection->fetchAll($childQuery);
                return $childResults;
        }
        catch(Exception $e){
            echo $e->getMessage();
        }
    }

}           

?>
  • 我已经在我的自定义模块中尝试了此代码。我通过Excel sku,属性,标签,价格上传了数据。
  • 我已经使用Excel阅读器来检索值

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.