2


Is it possible to generate skus for all products that do not have already one?

2
  • Can you explain the problem in more detail, because by default Magento does not create products without SKU field. Commented Sep 24, 2018 at 7:47
  • In the beginning customer did not want SKUs for his products. Now the customer wants SKUs for all products. There are about 1500 existing products without skus (he set them not required) Commented Sep 24, 2018 at 7:55

2 Answers 2

1

Hey yes you can generate unique sku automatically by using observer please refer below link: Auto generate sku

If you find this answer helpful please mark it as solution or give a upvote thanks.

1

Sku is a mandatory field in the backend. Therefore, I assume you want to improve a custom import that did not create sku for some products.

Below is a script that will crawl your products in your catalog; It is very fast to run and will not suffer from any third-party module getting in the way of updating your sku field either for the fact only the attribute is saved rather than the whole product model.

The below is a script that can run in a browser. The code has some logic and you are welcome to adjust the logic to your needs.

Please read the code and make sure you understand as it will change the catalog data in your system (a backup is welcome of course). I am not able to take responsability for any error you may have once the script has run.

Instead of filtering by product id like I do below in my script, you can filter the collection in many ways. You may try it first with a product (like below) and then on the full collection or by set of 1000 products.. good luck

    <?php
define('MAGENTO_ROOT', getcwd());

$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
$maintenanceFile = 'maintenance.flag';

require_once $mageFilename;

Mage::app();

$ids = [122663];

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('entity_id', $ids);

//$collection->setPageSize($limit);
//$collection->setCurPage($page);
if ($collection->count() > 0) {
    $nextSkuID = 100000;

    foreach ($collection as $product) {
        $sku = $product->getSku();
        if (is_null($sku) or (trim($sku) == '')) {
            $nextSkuID += 1;
            $autogenetedSku = sprintf('%s_%s', 'SK', $nextSkuID);
            $product->setSku($autogenetedSku)->getResource()->saveAttribute($product, 'sku');
        }
    }
}
6
  • Hello and thanks for the reply. It returns the message "This script cannot be run from Browser. This is the shell script." Commented Sep 24, 2018 at 6:58
  • of course, I assume you'd know how to run a php script. There is way to turn in into browser compatible script Commented Sep 24, 2018 at 7:56
  • Should I run it with a cron job? Commented Sep 24, 2018 at 10:06
  • it is now updated Commented Sep 24, 2018 at 19:46
  • thanks very much. It returns errors: /home/xrysotypia/public_html/skus.php: line 1: ?php: No such file or directory /home/xrysotypia/public_html/skus.php: line 2: syntax error near unexpected token E_ALL' /home/xrysotypia/public_html/skus.php: line 2: error_reporting(E_ALL | E_STRICT);' Commented Sep 25, 2018 at 4:47

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.