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 "catego
ry_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,