Пример #1
0
 /**
  * Create product duplicate
  *
  * @param \Magento\Catalog\Model\Product $product
  * @return \Magento\Catalog\Model\Product
  */
 public function copy(\Magento\Catalog\Model\Product $product)
 {
     $product->getWebsiteIds();
     $product->getCategoryIds();
     $duplicate = $this->productFactory->create();
     $duplicate->setData($product->getData());
     $duplicate->setIsDuplicate(true);
     $duplicate->setOriginalId($product->getId());
     $duplicate->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
     $duplicate->setCreatedAt(null);
     $duplicate->setUpdatedAt(null);
     $duplicate->setId(null);
     $duplicate->setStoreId(\Magento\Store\Model\Store::DEFAULT_STORE_ID);
     $this->copyConstructor->build($product, $duplicate);
     $isDuplicateSaved = false;
     do {
         $urlKey = $duplicate->getUrlKey();
         $urlKey = preg_match('/(.*)-(\\d+)$/', $urlKey, $matches) ? $matches[1] . '-' . ($matches[2] + 1) : $urlKey . '-1';
         $duplicate->setUrlKey($urlKey);
         try {
             $duplicate->save();
             $isDuplicateSaved = true;
         } catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
         }
     } while (!$isDuplicateSaved);
     $product->getOptionInstance()->duplicate($product->getId(), $duplicate->getId());
     $product->getResource()->duplicate($product->getId(), $duplicate->getId());
     return $duplicate;
 }
Пример #2
0
 /**
  * Create product duplicate
  *
  * @param \Magento\Catalog\Model\Product $product
  * @return \Magento\Catalog\Model\Product
  */
 public function copy(\Magento\Catalog\Model\Product $product)
 {
     $product->getWebsiteIds();
     $product->getCategoryIds();
     $duplicate = $this->productFactory->create();
     $duplicate->setData($product->getData());
     $duplicate->setIsDuplicate(true);
     $duplicate->setOriginalId($product->getId());
     $duplicate->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
     $duplicate->setCreatedAt(null);
     $duplicate->setUpdatedAt(null);
     $duplicate->setId(null);
     $duplicate->setStoreId(\Magento\Store\Model\Store::DEFAULT_STORE_ID);
     $this->copyConstructor->build($product, $duplicate);
     $duplicate->save();
     $product->getOptionInstance()->duplicate($product->getId(), $duplicate->getId());
     $product->getResource()->duplicate($product->getId(), $duplicate->getId());
     return $duplicate;
 }