Duplicate categories and products for new Vendors

  • Posts: 26
  • Thank you received: 1
  • Hikashop Business
9 years 9 months ago #160741

Hello,
i need to duplicate categories and products from "my vendor" each time a new vendor is created.
I've seen that there is a plugin (hikamarket - duplicateproducts) that seems to do exactly what I need but I don't understand how does it work.

Thanks,
Max

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

  • Posts: 25994
  • Thank you received: 4004
  • MODERATOR
9 years 9 months ago #160749

Hi,

The plugin "duplicate products" is not fully functional and his inclusion in the package is a mistake.
The idea of the plugin is to duplicate the category and product structure when a vendor is created.
The plugin should list the products under a specific targeted category (that you can choose in the Joomla plugin interface) and copy these products for the new vendor.

But the plugin is not completely finish and it won't provide the feature. There were some troubles detected during the latest tests and I commented the end of the algorithm.
That's why the plugin shouldn't be in the HikaMarket package but, once finished, this plugin should be the required plugin for your needs.

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: 26
  • Thank you received: 1
  • Hikashop Business
9 years 9 months ago #160782

Ok, nice news!
But i need it now... :)
Can you send me the plugin you are working on, even if incomplete? I'll try to adapt it for my needs.
I also tried to achieve the same result (a sort of) through massaction but I didn't found a way to copy all products and assign them to a vendor.

Thanks

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

  • Posts: 25994
  • Thank you received: 4004
  • MODERATOR
9 years 9 months ago #160790

Hi,

Here the latest version of the code (with the parts which were commented)

<?php
class plgHikamarketDuplicateproducts extends JPlugin {
	/**
	 *
	 * @param string $subject
	 * @param object $config
	 */
	public function __construct(&$subject, $config) {
		parent::__construct($subject, $config);
	}

	/**
	 *
	 * @param object $vendor
	 */
	public function onAfterVendorCreate(&$vendor) {
		$pluginsClass = hikamarket::get('shop.class.plugins');
		$plugin = $pluginsClass->getByName('hikamarket', 'duplicateproducts');
		if(empty($plugin->params['category_id']))
			return;
		$category_id = (int)$plugin->params['category_id'];

		$db = JFactory::getDBO();
		$config = hikamarket::config();
		$productClass = hikamarket::get('shop.class.product');
		$importHelper = hikamarket::get('shop.helper.import');

		$query = 'SELECT c.* FROM '.hikamarket::table('shop.category').' AS c '.
			' INNER JOIN '.hikamarket::table('shop.category').' AS d ON c.category_left >= d.category_left AND c.category_right <= d.category_right '.
			' where d.category_id = ' . (int)$category_id;
		$db->setQuery($query);
		$categories = $db->loadObjectList('category_id');

		if(empty($categories))
			return;

		$categories_id = array_keys($categories);

		$category_translated = array();
		//
		// -- TODO --
		// Create the new categories and store the "id translation" in an array.
		// We have to check the vendor root category before. Otherwise we won't have any category to copy into
		//
		// $category_translated = array(
		//    $old_id => $new_id
		// );
		//
		unset($categories);

		$query = 'SELECT product_id FROM ' . hikamarket::table('shop.product_category') . ' WHERE category_id IN '. implode(',', $categories_id).')';
		$db->setQuery($query);
		if(!HIKASHOP_J25) {
			$product_ids = $db->loadResultArray();
		} else {
			$product_ids = $db->loadColumn();
		}

		// Copy the products
		//
		foreach($product_ids as $product_id) {
			$importHelper->addTemplate($product_id);

			// Loading failed
			if(empty($importHelper->template))
				continue;

			$newProduct = new stdClass();
			$newProduct->product_code = $importHelper->template->product_code.'_copy'.rand();

			// Load correctly the template data into $newProduct
			$importHelper->_checkData($newProduct);

			// Translate categories
			foreach($newProduct->categories as &$cat_id) {
				if(isset($category_translated[$cat_id]))
					$cat_id = $category_translated[$cat_id];
			}

			// Set the vendor
			$newProduct->product_vendor_id = (int)$vendor->vendor_id;

			$products = array($newProduct);
			if(!empty($importHelper->template->variants)) {
				foreach($importHelper->template->variants as $variant) {
					$copy = (!HIKASHOP_PHP5) ? $variant : clone($variant);
					$copy->product_parent_id = $newProduct->product_code;
					$copy->product_code = $copy->product_code.'_copy'.rand();

					// Set the vendor
					$copy->product_vendor_id = (int)$vendor->vendor_id;

					unset($copy->product_id);
					$products[] = $copy;
				}
			}
			$importHelper->_insertProducts($products);

			unset($newProduct);
			unset($products);
			unset($importHelper->template);
		}
	}
}
Please note that there was a mistake in the plugin parameter "category_id" which has to be renamed in the xml too.
This code is completely untested and the part which copy the categories is not done.

If you have to possibility to test and fix it, I will be pleased to integrate your modifications.
This plugin is currently planned for HikaMarket 1.5.x, but we can see to change his priority in order to include it (a finished version) into HikaMarket 1.4.2

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: 26
  • Thank you received: 1
  • Hikashop Business
9 years 9 months ago #160912

Thanks a lot, I'll do my best!
Max

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

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