Author Topic: Generate auto SKU number  (Read 3523 times)

Zilvermeeuw

  • Beginner
  • *
  • Posts: 26
Generate auto SKU number
« on: February 04, 2015, 19:20:29 pm »
A customer wants to have an automatic generated SKU number filled in, when creating a new product.

I solved this to copy the file "administrator/components/com_virtuemart/views/product/tmpl/product_edit_information.php" to "templates/droomdecor5/html/com_virtuemart/invoice/invoice_order.php". Or another template if you use one. This will create an override of the virtuemart backend template file.

In this file, on line 64, just before
Code: [Select]
<input type="text" class="inputbox" name="product_sku" id="product_sku" value="<?php echo $this->product->product_sku?>" size="32" maxlength="64" />
I added:

Code: [Select]
<?php // Added by Richard Vinke for auto SKU
  // Only if product_sku is empty
  
if ( empty($this->product->product_sku) ) {

    
// Connect to database
    
$db JFactory::getDbo();
    
$q 'SELECT product_sku FROM `#__virtuemart_products`';
    
$db->setQuery($q);

    
$allSKUs $db->loadAssocList();
    
$maxSKU $allSKUs['product_sku'];
    
//echo "allSKUs:<pre>"; print_r($allSKUs); echo "end</pre>";
    
foreach ($allSKUs as $skuArray) {
      
$sku $skuArray['product_sku'];
      
//echo "SKU:". $sku['product_sku'] .".";

      // Check if sku is numeric
      
if (!is_numeric($sku)) {
        echo 
"sku \""$sku ."\" is NOT numeric.<br>";
      }

      
// Set maxSKU if needed
      
if ( $sku $maxSKU ) { $maxSKU $sku; }

    }

    
// Set next available sku in correct variable
    
$this->product->product_sku $maxSKU 1;

    
// Format in correct number of digits
    
$this->product->product_sku sprintf('%05d'$this->product->product_sku);

    
// display message
    
echo "<span style='color:#f00;'>Automatic generated SKU:</span>";

  }
?>

It gives a warning if a SKU is not numerical.

That's it!

GJC Web Design

  • 3rd party VirtueMart Developer
  • Super Hero
  • *
  • Posts: 9222
  • Virtuemart, Joomla & php developer
    • GJC Web Design
  • VirtueMart Version: 3.6.8
Re: Generate auto SKU number
« Reply #1 on: February 04, 2015, 22:18:38 pm »
nice!
GJC Web Design
VirtueMart and Joomla Developers - php developers http://www.gjcwebdesign.com
VM3 AusPost Shipping Plugin - e-go Shipping Plugin - VM3 Postcode Shipping Plugin - Radius Shipping Plugin - VM3 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
http://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 9997
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: Generate auto SKU number
« Reply #2 on: February 04, 2015, 22:20:50 pm »
You should add it to the table check function.

and use the provided function, which is already there. Means you set it unique.
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Zilvermeeuw

  • Beginner
  • *
  • Posts: 26
Re: Generate auto SKU number
« Reply #3 on: February 05, 2015, 06:10:28 am »
You should add it to the table check function.

and use the provided function, which is already there. Means you set it unique.
Yes,... that sounds better.

Can you help me to find the function names?

GJC Web Design

  • 3rd party VirtueMart Developer
  • Super Hero
  • *
  • Posts: 9222
  • Virtuemart, Joomla & php developer
    • GJC Web Design
  • VirtueMart Version: 3.6.8
Re: Generate auto SKU number
« Reply #4 on: February 05, 2015, 11:23:01 am »
Maybe he means administrator\components\com_virtuemart\helpers\vmtable.php  ?

function checkCreateUnique($tbl_name, $name) {

used in products model

e.g. $prodTable->checkCreateUnique('#__virtuemart_products_' . VmConfig::$vmlang,'slug');
GJC Web Design
VirtueMart and Joomla Developers - php developers http://www.gjcwebdesign.com
VM3 AusPost Shipping Plugin - e-go Shipping Plugin - VM3 Postcode Shipping Plugin - Radius Shipping Plugin - VM3 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
http://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation