News:

Support the VirtueMart project and become a member

Main Menu

Generate auto SKU number

Started by Zilvermeeuw, February 04, 2015, 19:20:29 PM

Previous topic - Next topic

Zilvermeeuw

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
<input type="text" class="inputbox" name="product_sku" id="product_sku" value="<?php echo $this->product->product_sku?>" size="32" maxlength="64" />

I added:

<?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!
If I never done it, I think I am able to do it.

GJC Web Design

GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 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
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

Milbo

You should add it to the table check function.

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

Zilvermeeuw

Quote from: Milbo 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.
Yes,... that sounds better.

Can you help me to find the function names?
If I never done it, I think I am able to do it.

GJC Web Design

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 https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 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
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation