Multivendor Add product code duplicate issue

  • Posts: 61
  • Thank you received: 2
11 years 3 months ago #115332

I am having an issue with the code value on product upload. We are just in the middle of bug testing our site and came across this.

Scenario.

Add Product Form.

I have excluded Code from the ACL , I don't want clients to get too overwhelmed and confused.
Entry of the title, example. Test
There is already a product named Test existing. If gives the following error

A product with the same code already exists

Any simple way to add a sequential number to code section only of the product being uploaded to avoid this?

Last edit: 11 years 3 months ago by xsbucks.

Please Log in or Create an account to join the conversation.

  • Posts: 26156
  • Thank you received: 4028
  • MODERATOR
11 years 3 months ago #115339

Hi,

What do you mean with the "product being uploaded" ?

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 61
  • Thank you received: 2
11 years 3 months ago #115342

Under 'Add New Product' in the front end upload.

on save of product with the same title as existing product, Is there something i can do to have duplicate titles to have a different code?
ie..
Product 1
Title = Test Code = Test

Product 2
Title = Test Code = Test1

Product 3
Title = Test Code = Test2

Product 4
Title = Test Code = Test3

and so on ......

Last edit: 11 years 3 months ago by xsbucks.

Please Log in or Create an account to join the conversation.

  • Posts: 26156
  • Thank you received: 4028
  • MODERATOR
11 years 3 months ago #115363

Hi,

For the same vendor or for different vendors ?
Do you think that an option to set automatically a prefix for the vendor product codes could be interesting ?

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 61
  • Thank you received: 2
11 years 3 months ago #115372

This was happening with different vendors with the same [Title]. I think that would be beneficial, however i think a sequential number after the code would be a bit less buggy, and allow one vendor to have multiple titles the same. In my instance anyway that would be.

Your guys support is absolutely amazing i am going to say. Top Shelf

Please Log in or Create an account to join the conversation.

  • Posts: 61
  • Thank you received: 2
11 years 3 months ago #115375

Tried SQL Manipulate , Already an AI in the the Table.

Last edit: 11 years 3 months ago by xsbucks.

Please Log in or Create an account to join the conversation.

  • Posts: 26156
  • Thank you received: 4028
  • MODERATOR
11 years 3 months ago #115481

Hi,

I am working on a patch in the same time than the patch for the template data during saving.
I talked with Nicolas, we will see how to improve this product_code generation for HikaShop and HikaMarket.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 61
  • Thank you received: 2
11 years 3 months ago #115611

Thanks Jerome.

Please Log in or Create an account to join the conversation.

  • Posts: 26156
  • Thank you received: 4028
  • MODERATOR
11 years 3 months ago #115698

Hi,

Still working on the algorithm.
Creating an incremental suffix could be complicated because we have to load every product codes which have the same "pattern" and make a loop to find an available number.
So I am trying to just loading the last product code with the same "pattern", the product code with the higher number but you can have false positives.

For example if your product code is "test" and you have "test_1", "test_2" in your database. The last element will be "test_2" so the algorithm will work.
But if you have a product code "test_test" in your database, it will be retrieve as the last element during the sort, so we won't be able to find the right prefix.

I don't want to use a SQL "regex" because it will be too much. So if I don't find a number suffix for the last element in my query, I use the "product id" as the number in the suffix.

I have to make more test but it should abort the error message (and save the product).
I wondering if showing a notice message telling that the product code was already used so a new one has been generated, could be interesting.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 61
  • Thank you received: 2
11 years 3 months ago #115952

That actually would be perfect as there is a unique product id anyway. Use that as a prefix for the code. Brilliant. I am testing that out as well where its not too heavy on the sql. If i come up with it soon i will let you know.

Please Log in or Create an account to join the conversation.

  • Posts: 26156
  • Thank you received: 4028
  • MODERATOR
11 years 3 months ago #115965

Hi,

I have finish to test the patch for the HikaMarket Product class.

You can add into the class file "administrator/components/com_hikamarket/classes/product.php" this new function:

	/**
	 *
	 */
	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:
	public function save(&$product) {
		JPluginHelper::importPlugin('hikamarket');
		$productClass = hikamarket::get('shop.class.product');
		$status = $productClass->save($product);
		return $status;
	}
By this:
	public function save(&$product) {
		$this->checkProductCode($product);

		JPluginHelper::importPlugin('hikamarket');
		$productClass = hikamarket::get('shop.class.product');
		$status = $productClass->save($product);
		return $status;
	}
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")
INSERT INTO `joom25`.`#__hikamarket_config` (`config_namekey`,`config_value`,`config_default`) VALUES ('abort_duplicate_product_code','1','0')

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.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 61
  • Thank you received: 2
11 years 3 months ago #116029

Awesome! Thanks Jerome, One question, In which file is the "After that, you have to modify the "save" function. So replace:" code?

Please Log in or Create an account to join the conversation.

  • Posts: 82823
  • Thank you received: 13370
  • MODERATOR
11 years 3 months ago #116047

in the same file, "administrator/components/com_hikamarket/classes/product.php"

Please Log in or Create an account to join the conversation.

  • Posts: 61
  • Thank you received: 2
11 years 3 months ago #116139

ahh Thanks Nicolas, I was for some reason trying to find them in Hikashop.... not hikamarket.....

Parse error: syntax error, unexpected T_PROTECTED in /home/*******/public_html/******/administrator/components/com_hikamarket/classes/product.php on line 93

Is the error I am getting on the site now

Please Log in or Create an account to join the conversation.

  • Posts: 26156
  • Thank you received: 4028
  • MODERATOR
11 years 3 months ago #116177

Hi,

I think you paste the function in the wrong place in the file.
You have to paste it just after:

	public function save(&$product) {
		$this->checkProductCode($product);
 
		JPluginHelper::importPlugin('hikamarket');
		$productClass = hikamarket::get('shop.class.product');
		$status = $productClass->save($product);
		return $status;
	}
Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.
The following user(s) said Thank You: xsbucks

Please Log in or Create an account to join the conversation.

  • Posts: 61
  • Thank you received: 2
11 years 3 months ago #116313

Brilliant!

Please Log in or Create an account to join the conversation.

  • Posts: 61
  • Thank you received: 2
11 years 3 months ago #116318

That worked perfectly! You guys are unbelievable!

The following user(s) said Thank You: Jerome

Please Log in or Create an account to join the conversation.

  • Posts: 26156
  • Thank you received: 4028
  • MODERATOR
11 years 3 months ago #116320

Hi,

Good to know.
The feature/function will be include in the next HikaMarket release, with the other new features.
Options will be introduce in the configuration part in order to activate it easily.

Thanks :)


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 61
  • Thank you received: 2
11 years 3 months ago #116327

One thing I am not sure we can fix or not, When i refresh on the Successfully Saved page after creating a new product, It adds the same product again but with the next sequential number ( As a new product)

Please Log in or Create an account to join the conversation.

  • Posts: 26156
  • Thank you received: 4028
  • MODERATOR
11 years 3 months ago #116387

Hi,

The solution would be to redirect the user to the product edition page just after the product creation.
So when the user will try to refresh the page, it will just refresh the product edition page, not the previous creation form.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

Moderators: Obsidev
Time to create page: 0.085 seconds
Powered by Kunena Forum