/**
*
*/
protected function checkProductCode(&$product) {
$config = hikamarket::config();
//
// Vendor Prefix
//
$vendor_id = hikamarket::loadVendor(false, false);
$vendor_prefix = $config->get('prefix_product_code', null);
if(!empty($vendor_prefix) && $vendor_id > 1) {
$prefix = 'v' . $vendor_id . '_';
if(is_string($vendor_prefix)) {
$prefix = $vendor_prefix . $vendor_id . '_';
if(strpos($vendor_prefix, '%d') !== false)
$prefix = sprintf($vendor_prefix, $vendor_id);
}
if(substr($product->product_code, 0, strlen($prefix)) != $prefix)
$product->product_code = $prefix . $product->product_code;
}
//
// Duplicate product code check
//
if(!$config->get('abort_duplicate_product_code', 0))
return;
// Get the current product_id or determine which value it will be
//
if(!empty($product->product_id)) {
$futur_product_id = $product->product_id;
} else {
$query = 'SELECT MAX(`product_id`) FROM '.hikamarket::table('shop.product');
$this->db->setQuery($query);
$futur_product_id = (1 + (int)$this->db->loadResult());
// Generate a product code based on what we have
//
if(empty($product->product_code)) {
$test = '';
if(!empty($product->product_name)) {
$search = explode(',','ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,e,i,ø,u');
$replace = explode(',','c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,e,i,o,u');
$test = str_replace($search, $replace, $product->product_name);
$test = preg_replace('#[^a-z0-9_-]#i', '', $test);
}
if(empty($test)) {
$product->product_code = 'product_' . $futur_product_id;
} else {
$test = str_replace($search, $replace, $product->product_name);
$product->product_code = preg_replace('#[^a-z0-9_-]#i', '_', $test);
}
}
}
if(!empty($product->product_code)) {
if(empty($product->product_id)) {
$query = 'SELECT COUNT(*) FROM '.hikamarket::table('shop.product').' WHERE product_code = '.$this->db->Quote($product->product_code);
$this->db->setQuery($query);
$isSame = ((int)$this->db->loadResult() > 0);
} else {
$query = 'SELECT COUNT(*) FROM '.hikamarket::table('shop.product').' WHERE product_code = '.$this->db->Quote($product->product_code).' AND product_id != '.(int)$product->product_id;
$this->db->setQuery($query);
$isSame = ((int)$this->db->loadResult() > 0);
}
if($isSame) {
if(HIKASHOP_J30)
$productCodeEscaped = "'" . $this->db->escaped($product->product_code . '_', true) . "%'";
else
$productCodeEscaped = "'" . $this->db->getEscaped($product->product_code . '_', true) . "%'";
$query = 'SELECT product_code FROM '.hikamarket::table('shop.product').' WHERE product_code LIKE '.$productCodeEscaped;
if(!empty($product->product_id)) {
$query .= ' AND product_id != '.(int)$product->product_id;
}
$query .= ' ORDER BY product_code DESC';
$this->db->setQuery($query, 0, 1);
$last_product_code = $this->db->loadResult();
$suffix = substr($last_product_code, 0, strlen($product->product_code) + 1);
if(!empty($suffix) && (int)$suffix > 0)
$product->product_code .= '_' . ((int)$suffix + 1);
else
$product->product_code .= '_' . $futur_product_id;
$warning_message = JText::_('WARNING_DUPLICATE_PRODUCT_CODE_MODIFIED');
if(!empty($warning_message)) {
$app = JFactory::getApplication();
$app->enqueueMessage($warning_message);
}
}
}
}
After that, you have to modify the "save" function. So replace:
In order to activate the feature, you have to create a new entry in the HikaMarket configuration.
You can use the new SQL query (replace "#_" by your database prefix. Like: "jos_hikamarket_config")
You can add a translation for the text "WARNING_DUPLICATE_PRODUCT_CODE_MODIFIED". If the translation is empty, no message will be displayed when a duplicate product code will be detect/modified.