使用EAN +产品ID更新Magento SKU

时间:2015-06-08 10:01:44

标签: php mysql magento skus

我正在尝试使用由EAN(破折号)产品ID组成的字符串来批量更新我的产品SKU。

E.g。

EAN=1234 & ID=44

Result=1234-44

我很高兴使用PHP或MySQL语句,我可以将其复制/粘贴到phpMyAdmin。

请在处理初学者时尽可能具体。

3 个答案:

答案 0 :(得分:0)

在magento根文件夹中创建文件Updatesku.php 并在该文件中写下以下代码,然后使用以下URL http://www.yourdomain.com/Updatesku.php

执行它
<?php
require_once('app/Mage.php'); 
umask(0);
Mage::app();
$_productCollection = Mage::getModel('catalog/product')
               ->getCollection()->addAttributeToSelect('*');
foreach($_productCollection as $_product)
{   
     try
     {
          $ean = $_product->getAttEan();
          $id  =  $_product->getEntityId();
          $newSku = $ean . '-' . $id;
          $_product->setSku($newSku);
          $_product->save();
     }
     catch(Exception $e){
          echo $e->getMessage();
     }
}

答案 1 :(得分:0)

为了更新所有空白的SKUs,请在文件中进行以下更改。

<?php
    require_once('app/Mage.php'); 
    umask(0);
    Mage::app();
    $_productCollection = Mage::getModel('catalog/product')
                   ->getCollection()
                   ->addAttributeToFilter('sku',array('null' => true))
                   ->load();
    foreach($_productCollection as $_product)
    {   
         try
         {
              $ean = $_product->getAttEan();
              $id  =  $_product->getEntityId();
              $newSku = $ean . '-' . $id;
              $_product->setSku($newSku);
              $_product->save();
         }
         catch(Exception $e){
              echo $e->getMessage();
         }
    }

答案 2 :(得分:-1)

以下代码应该......根据自己的喜好调整它..

已更新

<?php

require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();

// Loading products that satisfies the criteria
$productList = Mage::getModel('catalog/product')
                ->getCollection()
                ->addAttributeToSelect('*')
            //  ->addAttributeToFilter('sku', array('like' => '%-T')) //if criteria
                ; 

//Writing to Magento Log
$totalString = count($productList)." Products were found.";         
Mage::log($totalString, null, 'listProducts.log');
echo $totalString;

if(count($productList)){
    foreach($productList as $product){
        //updating products now
        try{
            $EAN = $product->getAttEan(); // sample EAN, eg $EAN = '1234';
            $ID = $product->getEntityId(); // sample  ID, eg, $ID = $product->getSku();
            $newSku = $EAN . '-' . $ID;//build your new custom sku as per need
            $product->setSku($newSku);
            //update other if you want

            $product->save();
            $skuString = "Product with SKU : ".$product->getSku()." updated!!";
            Mage::log($skuString, null, 'listProducts.log');
            echo $skuString;
        }catch(Exception $ex){
            Mage::log($ex->getMessage(), null, 'listProducts.log');
            echo $ex->getMessage();
        }
    }
    echo "Custom Sku has been updated Succesfully";
}

?>