Question

I imported some non-visible simple products (from csv) with an empty url key field.

If I save one of these manually in the back end the url key is generated OK.

If I bulk update an attribute the url key is not generated so I suspect it's not doing a full save.

I have over 1600 of these products so I need to be able to programmatically save products where sku starts with a certain string such that the url key is generated. Any pointers would be greatly appreciated.

Was it helpful?

Solution

Please Create product-save.php file at magento root directory, add below code and run url :http://yourdomain/product-save.php or you can run script via command line

<?php
error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;

Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
ini_set('memory_limit', '600M');
ini_set('max_execution_time', 1800);
umask(0);
Mage::app('admin');

$products = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('type_id', 'configurable');
$productModel = Mage::getModel('catalog/product');

foreach ($products as $product) {
    $productNameLowercase = strtolower($product->getName());
    $productKey = newurlkey($productNameLowercase);
    $productUpdate = $productModel->load($product->getId());
    $productUpdate->setUrlKey($productKey);
    $productUpdate->save();
}

function newurlkey($string)
{
    $string = str_replace(' ', '-', $string); // remove spaces with hyphens.
    return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top