public function duplicateProduct($productId, $namePattern = 'copy of %s') { //TODO : use the $namePattern var to input translated version of 'copy of %s', if translation requested. $product = new \ProductCore($productId); if (!\ValidateCore::isLoadedObject($product)) { throw new \Exception('AdminProductDataUpdater->duplicateProduct() received an unknown ID.', 5005); } $id_product_old = $product->id; if (empty($product->price) && \ShopCore::getContext() == \ShopCore::CONTEXT_GROUP) { $shops = \ShopGroupCore::getShopsFromGroup(\ShopCore::getContextShopGroupID()); foreach ($shops as $shop) { if ($product->isAssociatedToShop($shop['id_shop'])) { $product_price = new \ProductCore($id_product_old, false, null, $shop['id_shop']); $product->price = $product_price->price; } } } unset($product->id); unset($product->id_product); $product->indexed = 0; $product->active = 0; // change product name to prefix it foreach ($product->name as $langKey => $oldName) { if (!preg_match('/^' . str_replace('%s', '.*', preg_quote($namePattern, '/') . '$/'), $oldName)) { $newName = sprintf($namePattern, $oldName); if (mb_strlen($newName, 'UTF-8') <= 127) { $product->name[$langKey] = $newName; } } } if ($product->add() && \CategoryCore::duplicateProductCategories($id_product_old, $product->id) && \ProductCore::duplicateSuppliers($id_product_old, $product->id) && ($combination_images = \ProductCore::duplicateAttributes($id_product_old, $product->id)) !== false && \GroupReductionCore::duplicateReduction($id_product_old, $product->id) && \ProductCore::duplicateAccessories($id_product_old, $product->id) && \ProductCore::duplicateFeatures($id_product_old, $product->id) && \ProductCore::duplicateSpecificPrices($id_product_old, $product->id) && \PackCore::duplicate($id_product_old, $product->id) && \ProductCore::duplicateCustomizationFields($id_product_old, $product->id) && \ProductCore::duplicateTags($id_product_old, $product->id) && \ProductCore::duplicateDownload($id_product_old, $product->id)) { if ($product->hasAttributes()) { \ProductCore::updateDefaultAttribute($product->id); } if (!\Image::duplicateProductImages($id_product_old, $product->id, $combination_images)) { throw new DataUpdateException('product', $id_product_old, 'An error occurred while copying images.', 5008); } else { $this->hookDispatcher->dispatchForParameters('actionProductAdd', array('id_product' => (int) $product->id, 'product' => $product)); if (in_array($product->visibility, array('both', 'search')) && \Configuration::get('PS_SEARCH_INDEXATION')) { \SearchCore::indexation(false, $product->id); } return $product->id; } } else { throw new \Exception('An error occurred while creating an object.', 5009); } }