/**
  * @param int $productId
  * @return array
  */
 protected function formItemOptions($productId)
 {
     $itemOptions = [];
     $parentId = $this->productIndexer->getRelationsByChild($productId);
     if (!$parentId) {
         $itemOptions[] = ['product_id' => $productId, 'code' => 'info_buyRequest', 'value' => serialize(['product' => $productId, 'qty' => 1, 'related_product' => ''])];
         return $itemOptions;
     }
     $parentId = array_shift($parentId);
     /** @var \Magento\Catalog\Model\Product $product */
     $product = $this->productFactory->create();
     $product->load($productId);
     /** @var \Magento\Catalog\Model\Product $parentProduct */
     $parentProduct = $this->productFactory->create();
     $parentProduct->load($parentId);
     $superAttribute = [];
     if ($parentProduct->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
         $itemOptions[] = ['product_id' => $productId, 'code' => 'simple_product', 'value' => $productId];
         $itemOptions[] = ['product_id' => $productId, 'code' => 'product_qty_' . $productId, 'value' => 1];
         /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $productType */
         $productType = $parentProduct->getTypeInstance();
         foreach ($productType->getConfigurableAttributes($parentProduct) as $attribute) {
             $attributeCode = $attribute->getProductAttribute()->getAttributeCode();
             $superAttribute[$attribute->getAttributeId()] = $product->getData($attributeCode);
         }
         $itemOptions[] = ['product_id' => $parentId, 'code' => 'info_buyRequest', 'value' => serialize(['product' => $productId, 'qty' => 1, 'related_product' => '', 'super_attribute' => $superAttribute])];
         $itemOptions[] = ['product_id' => $parentId, 'code' => 'attributes', 'value' => serialize($superAttribute)];
     }
     return $itemOptions;
 }
Esempio n. 2
0
 /**
  * @param \Magento\Wishlist\Model\Wishlist $wishlist
  * @param array $productSkuList
  * @return void
  */
 public function addProductsToWishlist(\Magento\Wishlist\Model\Wishlist $wishlist, $productSkuList)
 {
     $shouldSave = false;
     foreach ($productSkuList as $productSku) {
         /** @var \Magento\Catalog\Model\Product $product */
         $product = $this->productFactory->create();
         $productId = $product->getIdBySku($productSku);
         $product->load($productId);
         if (empty($productId)) {
             continue;
         } elseif (!$shouldSave) {
             $shouldSave = true;
         }
         $buyRequest = ['product' => $productId, 'qty' => 1];
         if (!$product->isVisibleInSiteVisibility()) {
             $parentIds = $this->productIndexer->getRelationsByChild($productId);
             if ($parentIds) {
                 $buyRequest['product'] = $parentIds[0];
                 /** @var \Magento\Catalog\Model\Product $parentProduct */
                 $parentProduct = $this->productFactory->create();
                 $parentProduct->load($buyRequest['product']);
                 $configurableCode = \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE;
                 if ($parentProduct->getTypeId() == $configurableCode) {
                     /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $productType */
                     $productType = $parentProduct->getTypeInstance();
                     $buyRequest['super_attribute'] = [];
                     foreach ($productType->getConfigurableAttributes($parentProduct) as $attribute) {
                         $attributeCode = $attribute->getProductAttribute()->getAttributeCode();
                         $buyRequest['super_attribute'][$attribute->getAttributeId()] = $product->getData($attributeCode);
                     }
                     $product = $parentProduct;
                 } else {
                     continue;
                 }
             } else {
                 continue;
             }
         }
         $wishlist->addNewItem($product, $buyRequest, true);
     }
     if ($shouldSave) {
         $wishlist->save();
     }
 }