VirtueMart Forum

VirtueMart 2 + 3 + 4 => Product creation => Topic started by: Zilvermeeuw on February 04, 2015, 19:20:29 PM

Title: Generate auto SKU number
Post by: Zilvermeeuw 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
<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!
Title: Re: Generate auto SKU number
Post by: GJC Web Design on February 04, 2015, 22:18:38 PM
nice!
Title: Re: Generate auto SKU number
Post by: 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.
Title: Re: Generate auto SKU number
Post by: Zilvermeeuw on February 05, 2015, 06:10:28 AM
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?
Title: Re: Generate auto SKU number
Post by: GJC Web Design 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');