6

I used a third party service to transfer products from woocommerce to magento, but many of the products don't have SKUs.

I attempted to do this using a CSV file containing a column of IDs and a column of SKUS. Help me make this happen.

<?php
umask(0);
if (($handle = fopen("skus.csv", "r")) !== FALSE) { 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product_id = $data[0];
        $newsku = $data[1];
        $product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);
        if($product) { 
        echo 'working';
            $product->setSku($newsku);
            $product->save();
            echo  $product->getName() . ' SKU updated';
            echo "\n";
            }
    }
}
?>
3
  • Where does that CSV file come from? Did you do it own your own? What is the objective behind that? Maybe another solution is possible.
    – Alex
    Commented Aug 19, 2016 at 20:09
  • 1
    I based this code around a solution for this problem in older magento versions hummingbirduk.com/how-to-change-skus-in-bulk-in-magento The CSV just contains two columns one with product IDS and the other SKUs. However when trying to use your solutions I just fed example values to the function I didn't use the CSV. The csvs job can also be done with loops
    – Ian Wright
    Commented Aug 19, 2016 at 20:15
  • You might be able to do this on the Magento 2 "Import" screen, which allows you to add/update products in bulk from a CSV: alexcorradi.org/blog/…
    – thaddeusmt
    Commented Mar 16, 2017 at 20:53

3 Answers 3

6

I solved this by creating an extension using this function

protected $_productRepository;

public function __construct(
    \Magento\Backend\Block\Template\Context $context,        
    \Magento\Catalog\Model\ProductRepository $productRepository,
    array $data = []
)
{
    $this->_productRepository = $productRepository;
    parent::__construct($context, $data);
}

public function getProductById($id)
{
    return $this->_productRepository->getById($id);
}

}

then in my PHTML

$numofproducts = 1088;
$id = 1;
$sku = 1000;
while($id < $numofproducts) {
    $_product = $block->getProductById($id);
    echo $_product->getEntityId();
    echo '<br />';
    echo $_product->getName();
    echo $_product->getSKU();
    $_product->setSKU($sku)->save();
    echo $_product->getSKU();
    $id = $id + 1;
    $sku = $sku + 1;
}
2
  • Wowowo changing SKU in a template??? NOT a good idea. Commented Feb 3 at 11:17
  • I asked this question when I was 18 and trying to get something done for work with no programming experience. Happy to delete this answer if you think it's dangerous, but it'd be helpful if you gave more context. I'm a professional software engineer now, but I don't work with Magento/PHP and completely forgot that I wrote this question in the first place.
    – Ian Wright
    Commented Feb 25 at 4:31
4

In such a quick-and-dirty script this might work:

<?php
umask(0);

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$loader = $objectManager->get('Magento\Catalog\Model\ProductFactory');

if (($handle = fopen("skus.csv", "r")) !== FALSE) { 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $product_id = $data[0];
        $newsku = $data[1];
        $product = $loader->create()->load($product_id);
        if($product) { 
            echo 'working';
            $product->setSku($newsku);
            $product->save();
            echo  $product->getName() . ' SKU updated';
            echo "\n";
        }
    }
}

As using the ObjectManager is discouraged, in a Magento module it would look more like this:

public function __construct(
    \Magento\Catalog\Model\ProductFactory $_productloader
) {
    $this->_productloader = $_productloader;
}

public function changeSku($id, $newSKU)
{
    $product = $this->_productloader->create()->load($id);
    $product->setSku('BAZ')->save();
}
12
  • Appreciate your help, the code didn't run, I'm not sure if the path is correct as I can't locate that specific path on Magento. I am a novice in Magento/PHP and apologize if this sounds naive.
    – Ian Wright
    Commented Aug 19, 2016 at 19:59
  • Hm okay, it is not really standalone code - it is to be used in a Magento module :-/
    – Alex
    Commented Aug 19, 2016 at 20:08
  • I ran the changeSku function with test values to see if I could implement it in my initial code, am I unable to do this via script?
    – Ian Wright
    Commented Aug 19, 2016 at 20:12
  • Updated my answer to more suit your question ... good luck (untested)
    – Alex
    Commented Aug 19, 2016 at 20:15
  • Appreciate your help and patience man!
    – Ian Wright
    Commented Aug 19, 2016 at 20:18
1

Can you try something like this

protected $prodMod;

public function __construct(
  \Magento\Catalog\Model\Product $prodModel
){
   $this->prodModel = $prodModel;
}

public function loadById($productId){
   $prod = $prodModel->getResource()->load( $prodModel, $productId );
   if(!$prod->getId())
   {
       throw new \Exception(__("not found"));
   }

   $prod->setSku('FOO')->save();
}
1
  • I tried to run the loadbyid function for id (1) and it didn't work, I believe the path is wrong. I am actually using Magento 2.1, my apologies. I appreciate your help
    – Ian Wright
    Commented Aug 19, 2016 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.