├── Rakeshmagento └── Autosku │ ├── etc │ ├── module.xml │ ├── config.xml │ └── adminhtml │ │ ├── di.xml │ │ └── system.xml │ ├── registration.php │ └── Ui │ └── DataProvider │ └── Product │ └── Form │ └── Modifier │ └── Modifysku.php └── README.md /Rakeshmagento/Autosku/etc/module.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Rakeshmagento/Autosku/registration.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 0 7 | SKU000 8 | 0 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Rakeshmagento/Autosku/etc/adminhtml/di.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Rakeshmagento\Autosku\Ui\DataProvider\Product\Form\Modifier\Modifysku 8 | 20 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # magento2-auto-sku-generator 2 | magento2 auto generate sku for new product based on custom confgiuration. 3 | Auto SKU Generator Addon provides the functionality of generating the SKU automatically 4 | 5 | # Installation 6 | 7 | 1.Copy the files to app/code/ directory. 8 | 9 | 2.Execute the following commands 10 | 11 | php bin/magento setup:upgrade 12 | 13 | php bin/magento setup:static-content:deploy 14 | 15 | php bin/magento cache:flush 16 | 17 | php bin/magento cache:clean 18 | 19 | # Configuration 20 | 21 | #### 1.Navigation 22 | Store->Configuration->AUTO SKU->Configuration 23 | 24 | #### 2.Prefix : 25 | 26 | Here you need to specify the prefix for the sku for example : D, AAA, DAC 27 | 28 | #### 3.Start From : 29 | 30 | Here you need to configure the start integer for example start from : 1 31 | 32 | #### 4.SKU Length : 33 | 34 | This is the length of the sku, for example for length 5 -> D0001. 35 | 36 | # ScreenShot 37 | ![ScreenShot](http://octonism.com/sgit/magento2-auto-sku-generation.png) 38 | 39 | 40 | # Examples :- 41 | #### 1 : Prefix -> D, Start From -> 1, SKU Length -> 5 42 | D00001, D0002, D0003 ,........ 43 | 44 | #### 3 : Prefix -> SKU, Start From -> 1, SKU Length -> 7 45 | SKU0001, SKU0002, SKU0003, SKU0004 ,.................... 46 | 47 | -------------------------------------------------------------------------------- /Rakeshmagento/Autosku/etc/adminhtml/system.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | separator-top 9 | 10 | autosku 11 | Rakeshmagento_Autosku::config 12 | 13 | 14 | 15 | 16 | Magento\Config\Model\Config\Source\Yesno 17 | 18 | 19 | 20 | SKU prefix i.e ABC, DD. Put only characters 21 | 22 | 23 | 24 | SKU prefix i.e 0, 1. Put only integres. 25 | 26 | 27 | 28 | SKU Length i.e 4, 5 29 | 30 | 31 |
32 |
33 |
-------------------------------------------------------------------------------- /Rakeshmagento/Autosku/Ui/DataProvider/Product/Form/Modifier/Modifysku.php: -------------------------------------------------------------------------------- 1 | create('Magento\Framework\App\Config\ScopeConfigInterface'); 13 | 14 | $enabled = $scopeConfig->getValue( 15 | 'autosku_section/general/enabled', 16 | \Magento\Store\Model\ScopeInterface::SCOPE_STORE 17 | ); 18 | $prefix = $scopeConfig->getValue( 19 | 'autosku_section/general/prefix', 20 | \Magento\Store\Model\ScopeInterface::SCOPE_STORE 21 | ); 22 | $increment = $scopeConfig->getValue( 23 | 'autosku_section/general/increment', 24 | \Magento\Store\Model\ScopeInterface::SCOPE_STORE 25 | ); 26 | $length = $scopeConfig->getValue( 27 | 'autosku_section/general/sku_length', 28 | \Magento\Store\Model\ScopeInterface::SCOPE_STORE 29 | ); 30 | 31 | if($enabled){ 32 | 33 | $defaultSku = $this->_buildSku($prefix, $increment, $length); 34 | 35 | $meta['product-details'] 36 | ['children'] 37 | ['container_sku'] 38 | ['children'] 39 | ['sku'] 40 | ['arguments'] 41 | ['data'] 42 | ['config'] 43 | ['value'] = $defaultSku; 44 | } 45 | return $meta; 46 | } 47 | 48 | 49 | public function modifyData(array $data) 50 | { 51 | return $data; 52 | } 53 | 54 | private function _buildSku($_prefix, $_increment, $_length){ 55 | 56 | $skuToassign = ''; 57 | $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 58 | 59 | /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */ 60 | $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection'); 61 | /** Apply filters here */ 62 | $collection = $productCollection 63 | ->addAttributeToFilter( 64 | [ 65 | ['attribute' => 'sku', 'like' => $_prefix.'%'] 66 | ] 67 | ) 68 | ->setOrder('entity_id','DESC') 69 | ->setPageSize(1) 70 | ->load(); 71 | 72 | 73 | if($collection->getSize() > 0){ 74 | 75 | $product = $collection->getFirstItem(); 76 | $lastMacthedSku = $product->getSku(); 77 | $findLastIncrement = (int) preg_replace('/[^0-9]/', '', $lastMacthedSku); 78 | $nextInrement = ++$findLastIncrement; 79 | $incrementLength = strlen($findLastIncrement) + strlen($_prefix);// 80 | $skuToassign = $_prefix.str_pad( 81 | $skuToassign, 82 | ($_length - $incrementLength ), 83 | 0 84 | ).$nextInrement; 85 | 86 | }else{ 87 | 88 | $subtractLength = strlen($_prefix) + strlen($_increment); 89 | $skuToassign = $_prefix.str_pad( 90 | $skuToassign, 91 | ($_length - $subtractLength ), 92 | 0 93 | ).$_increment; 94 | 95 | } 96 | return $skuToassign; 97 | } 98 | } --------------------------------------------------------------------------------