2

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";
            }
    }
}
?>
  • 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 Aug 19 '16 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 Aug 19 '16 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 Mar 16 '17 at 20:53
3

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

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();
}
  • 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 Aug 19 '16 at 19:59
  • Hm okay, it is not really standalone code - it is to be used in a Magento module :-/ – Alex Aug 19 '16 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 Aug 19 '16 at 20:12
  • Updated my answer to more suit your question ... good luck (untested) – Alex Aug 19 '16 at 20:15
  • Appreciate your help and patience man! – Ian Wright Aug 19 '16 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();
}
  • 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 Aug 19 '16 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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