<?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();
}
}
}
?>
- मैंने इस कोड को अपने कस्टम मॉड्यूल में आज़माया है। मैंने एक्सेल स्कू, विशेषता, लेबल, मूल्य के माध्यम से डेटा अपलोड किया है।
- मैंने मान प्राप्त करने के लिए एक्सेल रीडर का उपयोग किया है