/** * @return array */ protected function prepareProducts($price) { $this->product->load(1); $productSecond = clone $this->product; $productSecond->setId(null)->setUrlKey(null)->setSku(uniqid($this->product->getSku() . '-'))->setName(uniqid($this->product->getName() . '-'))->setWebsiteIds([1]); $productSecond->save(); $productSecond->setPrice($price)->save(); $productThird = clone $this->product; $productThird->setId(null)->setUrlKey(null)->setSku(uniqid($this->product->getSku() . '-'))->setName(uniqid($this->product->getName() . '-'))->setWebsiteIds([1])->save(); $productThird->setPrice($price)->save(); return [$productSecond->getId(), $productThird->getId()]; }
/** * @magentoAppArea adminhtml */ public function testDuplicateSkuGeneration() { $this->_model->load(1); $this->assertEquals('simple', $this->_model->getSku()); /** @var \Magento\Catalog\Model\Product\Copier $copier */ $copier = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Catalog\\Model\\Product\\Copier'); $duplicate = $copier->copy($this->_model); $this->assertEquals('simple-3', $duplicate->getSku()); }
/** * @param \Magento\Catalog\Model\Product $product */ private function assertProductInfo($product) { $data = [1 => ['sku' => 'simple', 'name' => 'Simple Product', 'price' => '10', 'qty' => '1', 'position' => '1'], 21 => ['sku' => 'virtual-product', 'name' => 'Virtual Product', 'price' => '10', 'qty' => '2', 'position' => '2']]; $productId = $product->getId(); $this->assertEquals($data[$productId]['sku'], $product->getSku()); $this->assertEquals($data[$productId]['name'], $product->getName()); $this->assertEquals($data[$productId]['price'], $product->getPrice()); $this->assertEquals($data[$productId]['qty'], $product->getQty()); $this->assertEquals($data[$productId]['position'], $product->getPosition()); }
/** * Load specific layout handles by product type id * * @param CatalogProduct $product * @return \Magento\Framework\View\Result\Page */ protected function getProductPage($product) { /** @var \Magento\Framework\View\Result\Page $resultPage */ $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE); if ($product->getPageLayout()) { $resultPage->getConfig()->setPageLayout($product->getPageLayout()); } $urlSafeSku = rawurlencode($product->getSku()); $resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku, 'type' => $product->getTypeId()]); $resultPage->addUpdate($product->getCustomLayoutUpdate()); return $resultPage; }
/** * Validate SKU * * @param Product $object * @return bool * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\LocalizedException */ public function validate($object) { $attrCode = $this->getAttribute()->getAttributeCode(); $value = $object->getData($attrCode); if ($this->getAttribute()->getIsRequired() && strlen($value) === 0) { throw new \Magento\Framework\Exception\LocalizedException(__('The value of attribute "%1" must be set', $attrCode)); } if ($this->string->strlen($object->getSku()) > self::SKU_MAX_LENGTH) { throw new \Magento\Framework\Exception\LocalizedException(__('SKU length should be %1 characters maximum.', self::SKU_MAX_LENGTH)); } return true; }
/** * @magentoDbIsolation disabled * @magentoAppIsolation enabled * @magentoDataFixture Magento/Catalog/_files/price_row_fixture.php */ public function testProductsUpdate() { /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Catalog\\Model\\Product'); /** @var \Magento\Catalog\Api\CategoryLinkManagementInterface $linkManagment */ $linkManagment = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Catalog\\Api\\CategoryLinkManagementInterface'); $this->_product = $product->load(1); $linkManagment->assignProductToCategories($this->_product->getSku(), [9]); $this->_processor->reindexList([$this->_product->getId()]); $categoryFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Catalog\\Model\\CategoryFactory'); $listProduct = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Catalog\\Block\\Product\\ListProduct'); $category = $categoryFactory->create()->load(9); $layer = $listProduct->getLayer(); $layer->setCurrentCategory($category); $productCollection = $layer->getProductCollection(); $this->assertEquals(1, $productCollection->count()); /** @var $product \Magento\Catalog\Model\Product */ foreach ($productCollection as $product) { $this->assertEquals($this->_product->getId(), $product->getId()); $this->assertEquals($this->_product->getPrice(), $product->getPrice()); } }
/** * Load specific layout handles by product type id * * @param CatalogProduct $product * @return void */ protected function _initProductLayout($product) { $this->_view->getPage()->initLayout(); if ($product->getPageLayout()) { /** @var \Magento\Framework\View\Page\Config $pageConfig */ $pageConfig = $this->_objectManager->get('Magento\\Framework\\View\\Page\\Config'); $pageConfig->setPageLayout($product->getPageLayout()); } $update = $this->_view->getLayout()->getUpdate(); $this->_view->addPageLayoutHandles(array('id' => $product->getId(), 'sku' => $product->getSku(), 'type' => $product->getTypeId())); $this->_view->loadLayoutUpdates(); $update->addUpdate($product->getCustomLayoutUpdate()); $this->_view->generateLayoutXml(); $this->_view->generateLayoutBlocks(); }
/** * Load specific layout handles by product type id * * @param CatalogProduct $product * @return void */ protected function _initProductLayout($product) { $update = $this->_view->getLayout()->getUpdate(); $update->addHandle('default'); $this->_view->addPageLayoutHandles(array('id' => $product->getId(), 'sku' => $product->getSku(), 'type' => $product->getTypeId())); if ($product->getPageLayout()) { $this->_objectManager->get('Magento\\Theme\\Helper\\Layout')->applyHandle($product->getPageLayout()); } $this->_view->loadLayoutUpdates(); if ($product->getPageLayout()) { $this->_objectManager->get('Magento\\Theme\\Helper\\Layout')->applyTemplate($product->getPageLayout()); } $update->addUpdate($product->getCustomLayoutUpdate()); $this->_view->generateLayoutXml(); $this->_view->generateLayoutBlocks(); }
/** * Initialize grouped product links * * @param \Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $subject * @param \Magento\Catalog\Model\Product $product * @param array $links * * @return \Magento\Catalog\Model\Product * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function beforeInitializeLinks(\Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $subject, \Magento\Catalog\Model\Product $product, array $links) { if ($product->getTypeId() === TypeGrouped::TYPE_CODE && !$product->getGroupedReadonly()) { $links = isset($links[self::TYPE_NAME]) ? $links[self::TYPE_NAME] : $product->getGroupedLinkData(); if (!is_array($links)) { $links = []; } if ($product->getGroupedLinkData()) { $links = array_merge($links, $product->getGroupedLinkData()); } $newLinks = []; $existingLinks = $product->getProductLinks(); foreach ($links as $linkRaw) { /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */ $productLink = $this->productLinkFactory->create(); if (!isset($linkRaw['id'])) { continue; } $productId = $linkRaw['id']; if (!isset($linkRaw['qty'])) { $linkRaw['qty'] = 0; } $linkedProduct = $this->productRepository->getById($productId); $productLink->setSku($product->getSku())->setLinkType(self::TYPE_NAME)->setLinkedProductSku($linkedProduct->getSku())->setLinkedProductType($linkedProduct->getTypeId())->setPosition($linkRaw['position'])->getExtensionAttributes()->setQty($linkRaw['qty']); if (isset($linkRaw['custom_attributes'])) { $productLinkExtension = $productLink->getExtensionAttributes(); if ($productLinkExtension === null) { $productLinkExtension = $this->productLinkExtensionFactory->create(); } foreach ($linkRaw['custom_attributes'] as $option) { $name = $option['attribute_code']; $value = $option['value']; $setterName = 'set' . ucfirst($name); // Check if setter exists if (method_exists($productLinkExtension, $setterName)) { call_user_func([$productLinkExtension, $setterName], $value); } } $productLink->setExtensionAttributes($productLinkExtension); } $newLinks[] = $productLink; } $existingLinks = $this->removeUnExistingLinks($existingLinks, $newLinks); $product->setProductLinks(array_merge($existingLinks, $newLinks)); } }
/** * @param array $options * @param \Magento\Catalog\Model\Product $product * @return \Magento\Catalog\Api\Data\ProductCustomOptionInterface[] */ protected function prepareOptions($options, $product) { $preparedOptions = []; if (!$this->customOptionFactory) { $this->customOptionFactory = $this->objectManager->create('Magento\\Catalog\\Api\\Data\\ProductCustomOptionInterfaceFactory'); } foreach ($options as $option) { $option = $this->customOptionFactory->create(['data' => $option]); $option->setProductSku($product->getSku()); $preparedOptions[] = $option; } return $preparedOptions; }
/** * @param \Magento\Catalog\Model\Product $product * @return void */ protected function processDynamicOptionsData(\Magento\Catalog\Model\Product $product) { if ((int) $product->getPriceType() !== \Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC) { return; } if ($product->getOptionsReadonly()) { return; } $product->setCanSaveCustomOptions(true); $customOptions = $product->getProductOptions(); if (!$customOptions) { return; } foreach (array_keys($customOptions) as $key) { $customOptions[$key]['is_delete'] = 1; } $newOptions = $product->getOptions(); foreach ($customOptions as $customOptionData) { if ((bool) $customOptionData['is_delete']) { continue; } $customOption = $this->customOptionFactory->create(['data' => $customOptionData]); $customOption->setProductSku($product->getSku()); $customOption->setOptionId(null); $newOptions[] = $customOption; } $product->setOptions($newOptions); }
/** * @param OptionModel $option * @param Product $product * @return Option */ public function createDataFromModel(OptionModel $option, Product $product) { $this->builder->populateWithArray($option->getData())->setId($option->getId())->setTitle(is_null($option->getTitle()) ? $option->getDefaultTitle() : $option->getTitle())->setSku($product->getSku()); return $this->builder->create(); }
/** * Get product price with all tax settings processing * * @param \Magento\Catalog\Model\Product $product * @param float $price inputted product price * @param bool $includingTax return price include tax flag * @param null|\Magento\Customer\Model\Address\AbstractAddress $shippingAddress * @param null|\Magento\Customer\Model\Address\AbstractAddress $billingAddress * @param null|int $ctc customer tax class * @param null|string|bool|int|\Magento\Store\Model\Store $store * @param bool $priceIncludesTax flag what price parameter contain tax * @param bool $roundPrice * @return float * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function getTaxPrice($product, $price, $includingTax = null, $shippingAddress = null, $billingAddress = null, $ctc = null, $store = null, $priceIncludesTax = null, $roundPrice = true) { if (!$price) { return $price; } $store = $this->_storeManager->getStore($store); if ($this->_taxConfig->needPriceConversion($store)) { if ($priceIncludesTax === null) { $priceIncludesTax = $this->_taxConfig->priceIncludesTax($store); } $shippingAddressDataObject = null; if ($shippingAddress === null) { $shippingAddressDataObject = $this->convertDefaultTaxAddress($this->_customerSession->getDefaultTaxShippingAddress()); } elseif ($shippingAddress instanceof \Magento\Customer\Model\Address\AbstractAddress) { $shippingAddressDataObject = $shippingAddress->getDataModel(); } $billingAddressDataObject = null; if ($billingAddress === null) { $billingAddressDataObject = $this->convertDefaultTaxAddress($this->_customerSession->getDefaultTaxBillingAddress()); } elseif ($billingAddress instanceof \Magento\Customer\Model\Address\AbstractAddress) { $billingAddressDataObject = $billingAddress->getDataModel(); } $taxClassKey = $this->_taxClassKeyFactory->create(); $taxClassKey->setType(TaxClassKeyInterface::TYPE_ID)->setValue($product->getTaxClassId()); if ($ctc === null && $this->_customerSession->getCustomerGroupId() != null) { $ctc = $this->customerGroupRepository->getById($this->_customerSession->getCustomerGroupId())->getTaxClassId(); } $customerTaxClassKey = $this->_taxClassKeyFactory->create(); $customerTaxClassKey->setType(TaxClassKeyInterface::TYPE_ID)->setValue($ctc); $item = $this->_quoteDetailsItemFactory->create(); $item->setQuantity(1)->setCode($product->getSku())->setShortDescription($product->getShortDescription())->setTaxClassKey($taxClassKey)->setIsTaxIncluded($priceIncludesTax)->setType('product')->setUnitPrice($price); $quoteDetails = $this->_quoteDetailsFactory->create(); $quoteDetails->setShippingAddress($shippingAddressDataObject)->setBillingAddress($billingAddressDataObject)->setCustomerTaxClassKey($customerTaxClassKey)->setItems([$item])->setCustomerId($this->_customerSession->getCustomerId()); $storeId = null; if ($store) { $storeId = $store->getId(); } $taxDetails = $this->_taxCalculationService->calculateTax($quoteDetails, $storeId, $roundPrice); $items = $taxDetails->getItems(); $taxDetailsItem = array_shift($items); if ($includingTax !== null) { if ($includingTax) { $price = $taxDetailsItem->getPriceInclTax(); } else { $price = $taxDetailsItem->getPrice(); } } else { switch ($this->_taxConfig->getPriceDisplayType($store)) { case Config::DISPLAY_TYPE_EXCLUDING_TAX: case Config::DISPLAY_TYPE_BOTH: $price = $taxDetailsItem->getPrice(); break; case Config::DISPLAY_TYPE_INCLUDING_TAX: $price = $taxDetailsItem->getPriceInclTax(); break; default: break; } } } if ($roundPrice) { return $this->priceCurrency->round($price); } else { return $price; } }
/** * {@inheritdoc} */ public function convert(\Magento\Catalog\Model\Product $product) { return [ProductLink::TYPE => $product->getTypeId(), ProductLink::SKU => $product->getSku(), ProductLink::POSITION => $product->getPosition()]; }
/** * Save image information to DB. * * @param \Magento\Catalog\Model\Product $product * @param array $images * @return void */ protected function storeImage($product, $images) { $baseImage = ''; $i = 1; $mediaAttribute = $this->eavConfig->getAttribute('catalog_product', 'media_gallery'); foreach ($images as $image) { if (empty($image)) { $this->errors[] = $product->getSku(); continue; } if (strpos($image, '_main') !== false) { $baseImage = $image; } $id = $this->galleryResource->insertGallery(['attribute_id' => $mediaAttribute->getAttributeId(), 'entity_id' => $product->getId(), 'value' => $image]); $this->galleryResource->insertGalleryValueInStore(['value_id' => $id, 'store_id' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, 'entity_id' => $product->getId(), 'label' => 'Image', 'position' => $i, 'disables' => 0]); $this->galleryResource->bindValueToEntity($id, $product->getId()); $i++; } if (empty($baseImage)) { $baseImage = $images[0]; } if ($baseImage) { $imageAttribute = $product->getResource()->getAttribute('image'); $smallImageAttribute = $product->getResource()->getAttribute('small_image'); $thumbnailAttribute = $product->getResource()->getAttribute('thumbnail'); $adapter = $product->getResource()->getConnection(); foreach ([$imageAttribute, $smallImageAttribute, $thumbnailAttribute] as $attribute) { $table = $imageAttribute->getBackend()->getTable(); /** @var \Magento\Framework\DB\Adapter\AdapterInterface $adapter*/ $data = [$attribute->getBackend()->getEntityIdField() => $product->getId(), 'attribute_id' => $attribute->getId(), 'value' => $baseImage]; $adapter->insertOnDuplicate($table, $data, ['value']); } } }
/** * {@inheritdoc} */ public function convert(\Magento\Catalog\Model\Product $product) { return ['type' => $product->getTypeId(), 'sku' => $product->getSku(), 'position' => $product->getPosition()]; }
/** * Set current attribute to entry (for specified product) * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Framework\Gdata\Gshopping\Entry $entry * @return \Magento\Framework\Gdata\Gshopping\Entry * @throws \Magento\Framework\Exception\LocalizedException */ public function convertAttribute($product, $entry) { $entry->cleanTaxes(); if ($this->_taxData->getConfig()->priceIncludesTax()) { return $entry; } $defaultCustomerTaxClassId = $this->_getDefaultCustomerTaxClassId($product->getStoreId()); $rates = $this->_taxRateManagement->getRatesByCustomerAndProductTaxClassId($defaultCustomerTaxClassId, $product->getTaxClassId()); $targetCountry = $this->_config->getTargetCountry($product->getStoreId()); $ratesTotal = 0; foreach ($rates as $rate) { $countryId = $rate->getTaxCountryId(); $postcode = $rate->getTaxPostcode(); if ($targetCountry == $countryId) { $regions = $this->_getRegionsByRegionId($rate->getTaxRegionId(), $postcode); $ratesTotal += count($regions); if ($ratesTotal > self::RATES_MAX) { throw new \Magento\Framework\Exception\LocalizedException(__('Google shopping only supports %1 tax rates per product', self::RATES_MAX)); } foreach ($regions as $region) { $adjustments = $product->getPriceInfo()->getAdjustments(); if (array_key_exists('tax', $adjustments)) { $taxIncluded = true; } else { $taxIncluded = false; } $quoteDetailsItemDataArray = ['code' => $product->getSku(), 'type' => 'product', 'tax_class_key' => [TaxClassKeyInterface::KEY_TYPE => TaxClassKeyInterface::TYPE_ID, TaxClassKeyInterface::KEY_VALUE => $product->getTaxClassId()], 'unit_price' => $product->getPrice(), 'quantity' => 1, 'tax_included' => $taxIncluded, 'short_description' => $product->getName()]; $billingAddressDataArray = ['country_id' => $countryId, 'region' => ['region_id' => $rate->getTaxRegionId()], 'postcode' => $postcode]; $shippingAddressDataArray = ['country_id' => $countryId, 'region' => ['region_id' => $rate->getTaxRegionId()], 'postcode' => $postcode]; $quoteDetailsDataArray = ['billing_address' => $billingAddressDataArray, 'shipping_address' => $shippingAddressDataArray, 'customer_tax_class_key' => [TaxClassKeyInterface::KEY_TYPE => TaxClassKeyInterface::TYPE_ID, TaxClassKeyInterface::KEY_VALUE => $defaultCustomerTaxClassId], 'items' => [$quoteDetailsItemDataArray]]; $quoteDetailsObject = $this->_quoteDetailsFactory->create(); $this->dataObjectHelper->populateWithArray($quoteDetailsObject, $quoteDetailsDataArray, '\\Magento\\Tax\\Api\\Data\\QuoteDetailsInterface'); $taxDetails = $this->_taxCalculationService->calculateTax($quoteDetailsObject, $product->getStoreId()); $taxRate = $taxDetails->getTaxAmount() / $taxDetails->getSubtotal() * 100; $entry->addTax(['tax_rate' => $taxRate, 'tax_country' => $countryId, 'tax_region' => $region]); } } } return $entry; }
/** * {@inheritdoc} */ public function convert(\Magento\Catalog\Model\Product $product) { return [ProductLink::TYPE => $product->getTypeId(), ProductLink::SKU => $product->getSku(), ProductLink::POSITION => $product->getPosition(), ProductLink::CUSTOM_ATTRIBUTES_KEY => [[AttributeValue::ATTRIBUTE_CODE => 'qty', AttributeValue::VALUE => $product->getQty()]]]; }
/** * {@inheritdoc} */ public function convert(\Magento\Catalog\Model\Product $product) { return ['type' => $product->getTypeId(), 'sku' => $product->getSku(), 'position' => $product->getPosition(), 'custom_attributes' => [['attribute_code' => 'qty', 'value' => $product->getQty()]]]; }
/** * {@inheritdoc} */ public function getSku() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getSku'); if (!$pluginInfo) { return parent::getSku(); } else { return $this->___callPlugins('getSku', func_get_args(), $pluginInfo); } }
/** * Get product price with all tax settings processing * * @param \Magento\Catalog\Model\Product $product * @param float $price inputted product price * @param bool $includingTax return price include tax flag * @param null|Address $shippingAddress * @param null|Address $billingAddress * @param null|int $ctc customer tax class * @param null|string|bool|int|Store $store * @param bool $priceIncludesTax flag what price parameter contain tax * @param bool $roundPrice * @return float */ public function getTaxPrice($product, $price, $includingTax = null, $shippingAddress = null, $billingAddress = null, $ctc = null, $store = null, $priceIncludesTax = null, $roundPrice = true) { if (!$price) { return $price; } $store = $this->_storeManager->getStore($store); if ($this->_taxConfig->needPriceConversion($store)) { if (is_null($priceIncludesTax)) { $priceIncludesTax = $this->_taxConfig->priceIncludesTax($store); } $shippingAddressDataObject = null; if ($shippingAddress instanceof \Magento\Customer\Model\Address\AbstractAddress) { $shippingAddressDataObject = $this->_addressConverter->createAddressFromModel($shippingAddress, null, null); } $billingAddressDataObject = null; if ($billingAddress instanceof \Magento\Customer\Model\Address\AbstractAddress) { $billingAddressDataObject = $this->_addressConverter->createAddressFromModel($billingAddress, null, null); } $item = $this->_quoteDetailsItemBuilder->setQuantity(1)->setCode($product->getSku())->setShortDescription($product->getShortDescription())->setTaxClassKey($this->_taxClassKeyBuilder->setType(TaxClassKey::TYPE_ID)->setValue($product->getTaxClassId())->create())->setTaxIncluded($priceIncludesTax)->setType('product')->setUnitPrice($price)->create(); $quoteDetails = $this->_quoteDetailsBuilder->setShippingAddress($shippingAddressDataObject)->setBillingAddress($billingAddressDataObject)->setCustomerTaxClassKey($this->_taxClassKeyBuilder->setType(TaxClassKey::TYPE_ID)->setValue($ctc)->create())->setItems([$item])->setCustomerId($this->_customerSession->getCustomerId())->create(); $storeId = null; if ($store) { $storeId = $store->getId(); } $taxDetails = $this->_taxCalculationService->calculateTax($quoteDetails, $storeId); $items = $taxDetails->getItems(); $taxDetailsItem = array_shift($items); if (!is_null($includingTax)) { if ($includingTax) { $price = $taxDetailsItem->getPriceInclTax(); } else { $price = $taxDetailsItem->getPrice(); } } else { switch ($this->_taxConfig->getPriceDisplayType($store)) { case Config::DISPLAY_TYPE_EXCLUDING_TAX: case Config::DISPLAY_TYPE_BOTH: $price = $taxDetailsItem->getPrice(); break; case Config::DISPLAY_TYPE_INCLUDING_TAX: $price = $taxDetailsItem->getPriceInclTax(); break; default: break; } } } if ($roundPrice) { return $store->roundPrice($price); } else { return $price; } }
/** * @see \Magento\Catalog\Model\Product\Type\SimpleTest */ public function testGetSku() { $this->assertEmpty($this->_model->getSku()); $this->_model->setSku('sku'); $this->assertEquals('sku', $this->_model->getSku()); }
/** * Populate global product * * @param \Magento\Catalog\Model\Product $product * @return $this */ protected function populateGlobalProduct($product) { foreach ($this->import->getProductWebsites($product->getSku()) as $websiteId) { foreach ($this->websitesToStoreIds[$websiteId] as $storeId) { $this->storesCache[$storeId] = true; if (!$this->isGlobalScope($storeId)) { $this->addProductToImport($product, $storeId); $this->entityStoresToCheckOverridden[] = $this->connection->quoteInto('(store_id = ?', $storeId) . $this->connection->quoteInto(' AND entity_id = ?)', $product->getId()); } } } return $this; }
/** * Creates a product dataset which is filtered for the attributes the recommender system requires * * @param Product $product * @return array */ public function create(Product $product) { $data = array_intersect_key($product->getData(), $this->getAttributes()); return ['sku' => $product->getSku(), 'data' => $data]; }
/** * Populate global product * * @param \Magento\Catalog\Model\Product $product * @return $this */ protected function populateGlobalProduct($product) { foreach ($this->import->getProductWebsites($product->getSku()) as $websiteId) { foreach ($this->websitesToStoreIds[$websiteId] as $storeId) { $this->storesCache[$storeId] = true; if (!$this->isGlobalScope($storeId)) { $this->addProductToImport($product, $storeId); } } } return $this; }
/** * set product * * @return $this */ public function setProduct(\Magento\Catalog\Model\Product $product) { $this->setProductName($product->getName()); $this->setProductSku($product->getSku()); return $this; }
/** * Process bundle-related attributes of product after its creation. * * @param ProductModel $product * @param Product $productData * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @return string */ public function afterCreate(ProductModel $product, Product $productData) { /** @var string $productSku */ $productSku = $product->getSku(); if ($product->getTypeId() != ProductType::TYPE_BUNDLE) { return $productSku; } /** @var Option[] $bundleProductOptions */ /* @var AttributeValue $bundleProductOptionsAttrValue */ $bundleProductOptionsAttrValue = $productData->getCustomAttribute('bundle_product_options'); if (is_null($bundleProductOptionsAttrValue) || !is_array($bundleProductOptionsAttrValue->getValue())) { $bundleProductOptions = array(); } else { $bundleProductOptions = $bundleProductOptionsAttrValue->getValue(); } if (is_array($bundleProductOptions)) { foreach ($bundleProductOptions as $option) { $this->optionWriteService->add($productSku, $option); } } return $productSku; }