Установка url-keys для товаров на основе их названия в Magento
Код для установки url-keys для товаров на основе их названия в Magento..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<?php die('Disabled in script code to prevent unwanted changes'); require 'app/Mage.php'; Mage::app(); function generateUrlKey( $productName ) { $read = Mage::getSingleton('core/resource')->getConnection('core_read'); $urlKey = Mage::getModel('catalog/product_url')->formatUrlKey($productName); $sql = 'SELECT * FROM catalog_product_entity_varchar WHERE attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = "url_key" AND entity_type_id = 4) AND value = ? and store_id = 0'; $row = $read->fetchRow($sql, array($urlKey)); $idx = 0; while ($row != false) { $prefix = ''; if ($idx) $prefix = '-' . $idx; $urlKey = Mage::getModel('catalog/product_url')->formatUrlKey($productName) . $prefix; $idx++; $row = $read->fetchRow($sql, array($urlKey)); } return $urlKey; } set_time_limit(0); ?> <html> <head> <title>Url-key updater</title> <style type="text/css"> body {font-family:Tahome; font-size:11px;} </style> </head> <body> <?php $keysSet = 0; $keysExists = 0; $serial = 1; $model = Mage::getModel('catalog/product'); $products = $model->getCollection(); foreach ($products as $product) { if (!empty($_REQUEST['skip']) AND $serial<intval($_REQUEST['skip'])) { $serial++; continue; } $model->load($product->getId()); echo 'SKU '.$model->getSku().'] '.$model->getName().' = '; if (empty($_REQUEST['updateall']) AND $key = $model->getUrlKey() AND preg_match('~^[a-z0-9-]+$~', $key)) { $keysExists++; echo 'key exists: <span style="color:green">'.htmlspecialchars($key).'</span>';; } else { $keysSet++; $key = generateUrlKey($model->getName()); try{ $product->setUrlKey($key)->save(); } catch (Exception $e) { echo 'key save err: <span style="color:red">'.htmlspecialchars($key).'</span>';; } echo 'new key: <span style="color:blue">'.htmlspecialchars($key).'</span>';; } $websiteIds = $product->getWebsiteIds(); if (is_array($websiteIds)) $websiteIds = implode(',',$websiteIds); echo '(serial:'.$serial.'; websiteIds:'.$websiteIds.')'; $serial++; echo '<br />'; } ?> <hr /> Keys exists:<?php echo $keysExists; ?> | Keys set:<?php echo $keysSet; ?> </body> </html> |
Author: | Tags: /
| Rating:
Leave a Reply