Magento自动生成产品SKU,有两种方法 Open/app/design/adminhtml/default/default/template/catalog/product/edit.phtmland add the following code to the bottom of the file: <?php $dbread = Mage::getSingleton('core/resource')->getConnection('core_read'); $sql = $dbread->query("SELECT * FROM catalog_product_entity ORDER BY created_at DESC LIMIT 1"); $res = $sql->fetch(); ?> <script type="text/javascript"> if(document.getElementById('sku').value == ""){ document.getElementById('sku').value = <?php echo (int)$res["sku"] + 1; ?>; } </script>
Note:Read the SKU (must be an integer) of the last added product.
方法二: 打开 app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php line 743: /** * Save product action */ public function saveAction() { $storeId = $this->getRequest()->getParam('store'); $redirectBack = $this->getRequest()->getParam('back', false); $productId = $this->getRequest()->getParam('id'); $isEdit = (int)($this->getRequest()->getParam('id') != null); $data = $this->getRequest()->getPost(); //===> 这里开始 if(isset($data['category_ids']) AND $data['category_ids']) $category_ids = array_filter(array_unique(explode(',', $data['category_ids']))); if(!$category_ids) if($productId) $category_ids = array_filter(Mage::getModel('catalog/product')->load($productId)->getCategoryIds()); $_categories = array(); foreach ($category_ids as $category_id) { if($category_id) { $_cat = Mage::getModel('catalog/category')->load($category_id); $_cat->getLevel()*1 == 2 ? $_categories[$_cat->getId()]['name'] = $_cat->getName() : $_categories[$_cat->getParentId()]['son'][$_cat->getId()] = $_cat->getName(); } } $_first_category = current($_categories); if(!$_first_category) exit('<script>alert("Please Choose any Product Category !");history.back();</script>'); if(!$productId OR ($productId AND !$data['product']['sku'])) { $prefix = ''; if(stripos('+'.$_first_category['name'], 'decals')) $prefix .= 'D-'; if(isset($_first_category['son'])) $prefix .= strtoupper(substr($_first_category['name'], 0, 1).substr(current($_first_category['son']), 0, 1)); else $prefix .= strtoupper(substr($_first_category['name'], 0, 2)); $dbread = Mage::getSingleton('core/resource')->getConnection('core_read'); $sql = $dbread->query("SELECT * FROM mgy2_catalog_product_entity WHERE `sku` LIKE '".$prefix."%' ORDER BY `entity_id` DESC LIMIT 1 "); $row = $sql->fetch(); $next_sku_num = abs(substr($row['sku'], -4)*1)+1; $next_sku_len = strlen($next_sku_num)*1; $next_sku = $prefix.($next_sku_len < 4 ? str_repeat('0',(4-$next_sku_len)) : '').$next_sku_num; $_POST['product']['sku'] =& $next_sku; } //===> 这里结束 if ($data) { $this->_filterStockData($data['product']['stock_data']); $product = $this->_initProductSave(); // echo '<pre>'; print_r($data); echo '</pre>'; die; try { $product->save(); $productId = $product->getId(); if (isset($data['copy_to_stores'])) { $this->_copyAttributesBetweenStores($data['copy_to_stores'], $product); } $this->_getSession()->addSuccess($this->__('The product has been saved.')); } catch (Mage_Core_Exception $e) { $this->_getSession()->addError($e->getMessage()) ->setProductData($data); $redirectBack = true; } catch (Exception $e) { Mage::logException($e); $this->_getSession()->addError($e->getMessage()); $redirectBack = true; } } if ($redirectBack) { $this->_redirect('*/*/edit', array( 'id' => $productId, '_current'=>true )); } elseif($this->getRequest()->getParam('popup')) { $this->_redirect('*/*/created', array( '_current' => true, 'id' => $productId, 'edit' => $isEdit )); } else { $this->_redirect('*/*/', array('store'=>$storeId)); } } 原理如下:
原理自己计算吧 (责任编辑:最模板) |