/** * Set inventory data to custom attribute * * @param Product $object * @return $this */ public function afterLoad($object) { $stockItem = $this->stockRegistry->getStockItem($object->getId(), $object->getStore()->getWebsiteId()); $object->setData($this->getAttribute()->getAttributeCode(), ['is_in_stock' => $stockItem->getIsInStock(), 'qty' => $stockItem->getQty()]); return parent::afterLoad($object); }
/** * 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 */ public function convertAttribute($product, $entry) { $url = $product->getProductUrl(false); if ($url) { $isStoreInUrl = $this->_scopeConfig->isSetFlag(\Magento\Store\Model\Store::XML_PATH_STORE_IN_URL, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); if (!$isStoreInUrl) { $urlInfo = parse_url($url); $store = $product->getStore()->getCode(); if (isset($urlInfo['query']) && $urlInfo['query'] != '') { $url .= '&___store=' . $store; } else { $url .= '?___store=' . $store; } } $links = $entry->getLink(); if (!is_array($links)) { $links = []; } $link = $entry->getService()->newLink(); $link->setHref($url); $link->setRel('alternate'); $link->setType('text/html'); if ($product->getName()) { $link->setTitle($product->getName()); } $links[0] = $link; $entry->setLink($links); } return $entry; }
/** * Copy product inventory data (used for product duplicate functionality) * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Catalog\Model\Product $duplicate * @return void */ public function build(\Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product $duplicate) { $stockData = ['use_config_min_qty' => 1, 'use_config_min_sale_qty' => 1, 'use_config_max_sale_qty' => 1, 'use_config_backorders' => 1, 'use_config_notify_stock_qty' => 1]; $currentStockItemDo = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId()); if ($currentStockItemDo->getItemId()) { $stockData += ['use_config_enable_qty_inc' => $currentStockItemDo->getUseConfigEnableQtyInc(), 'enable_qty_increments' => $currentStockItemDo->getEnableQtyIncrements(), 'use_config_qty_increments' => $currentStockItemDo->getUseConfigQtyIncrements(), 'qty_increments' => $currentStockItemDo->getQtyIncrements()]; } $duplicate->setStockData($stockData); }
/** * Assign stock status information to product * * @param Product $product * @param int $stockStatus * @return void */ public function assignStatusToProduct(Product $product, $stockStatus = null) { if ($stockStatus === null) { $websiteId = $product->getStore()->getWebsiteId(); $stockStatus = $this->stockRegistryProvider->getStockStatus($product->getId(), $websiteId); $status = $stockStatus->getStockStatus(); } $product->setIsSalable($status); }
/** * Returns url to product image * * @param \Magento\Catalog\Model\Product $product * * @return string|false */ public function getUrl($product) { $image = $product->getData($this->getAttribute()->getAttributeCode()); $url = false; if (!empty($image)) { $url = $this->_storeManager->getStore($product->getStore())->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product/' . $image; } return $url; }
/** * Assign stock status information to product * * @param \Magento\Catalog\Model\Product $product * @param int $stockStatus * @return void */ public function assignStatusToProduct(\Magento\Catalog\Model\Product $product, $stockStatus = null) { if ($stockStatus === null) { $websiteId = $product->getStore()->getWebsiteId(); $stockStatus = $this->stockRegistry->getStockStatus($product->getId(), $websiteId); $status = $stockStatus->getStockStatus(); } $product->setIsSalable($status); }
/** * @param \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product * @param array $modelData * @param bool $isUnlimited * @return \Magento\Downloadable\Model\Link|\PHPUnit_Framework_MockObject_MockObject */ private function createLinkkModel($product, array $modelData, $isUnlimited) { $link = $this->getMockBuilder('\\Magento\\Downloadable\\Model\\Link')->disableOriginalConstructor()->setMethods(['setData', 'setLinkType', 'setProductId', 'setStoreId', 'setWebsiteId', 'setProductWebsiteIds', 'setPrice', 'setNumberOfDownloads', 'setSampleUrl', 'setSampleType', 'setLinkFile', 'setSampleFile', 'save', 'getIsUnlimited'])->getMock(); $link->expects($this->once())->method('setData')->with($modelData)->will($this->returnSelf()); $link->expects($this->once())->method('setLinkType')->with($modelData['type'])->will($this->returnSelf()); $link->expects($this->once())->method('setProductId')->with($product->getId())->will($this->returnSelf()); $link->expects($this->once())->method('setStoreId')->with($product->getStoreId())->will($this->returnSelf()); $link->expects($this->once())->method('setWebsiteId')->with($product->getStore()->getWebsiteId())->will($this->returnSelf()); $link->expects($this->once())->method('setPrice')->with(0); $link->expects($this->any())->method('setNumberOfDownloads')->with(0); $link->expects($this->once())->method('getIsUnlimited')->will($this->returnValue($isUnlimited)); return $link; }
/** * Retrieve product stock qty * * @param Product $product * @return float */ public function getProductStockQty($product) { return $this->stockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId()); }
/** * Apply special price for product if not return price that was before * * @param Product $product * @param float $finalPrice * @return float */ protected function _applySpecialPrice($product, $finalPrice) { return $this->calculateSpecialPrice($finalPrice, $product->getSpecialPrice(), $product->getSpecialFromDate(), $product->getSpecialToDate(), $product->getStore()); }
/** * Returns amount to display * * @param \Magento\Catalog\Model\Product $product * @return int */ public function getAmountForDisplay($product) { /** @var \Magento\Store\Model\Store $store */ $store = $product->getStore(); if ($this->isEnabled($store)) { return $this->_weeeTax->getWeeeAmount($product, null, null, null, $this->typeOfDisplay(1)); } return 0; }
/** * @param ComponentInterface $model * @param array $data * @param Product $product * @return void */ protected function setDataToModel(ComponentInterface $model, array $data, Product $product) { $model->setData($data)->setLinkType($data['type'])->setProductId($product->getData($this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()))->setStoreId($product->getStoreId())->setWebsiteId($product->getStore()->getWebsiteId())->setProductWebsiteIds($product->getWebsiteIds()); if (null === $model->getPrice()) { $model->setPrice(0); } if ($model->getIsUnlimited()) { $model->setNumberOfDownloads(0); } }
/** * @param Product $product * @return float */ public function getProductStockQty(Product $product) { return $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId())->getQty(); }
/** * Get downloadable product links * * @param \Magento\Catalog\Model\Product $product * @return array */ public function getLinks($product) { if (is_null($product->getDownloadableLinks())) { $_linkCollection = $this->_linksFactory->create()->addProductToFilter($product->getId())->addTitleToResult($product->getStoreId())->addPriceToResult($product->getStore()->getWebsiteId()); $linksCollectionById = array(); foreach ($_linkCollection as $link) { /* @var \Magento\Downloadable\Model\Link $link */ $link->setProduct($product); $linksCollectionById[$link->getId()] = $link; } $product->setDownloadableLinks($linksCollectionById); } return $product->getDownloadableLinks(); }
/** * Assign Stock Status to Product * * @param \Magento\Catalog\Model\Product $product * @param int $stockId * @param int $stockStatus * @return $this */ public function assignProduct(\Magento\Catalog\Model\Product $product, $stockId = Stock::DEFAULT_STOCK_ID, $stockStatus = null) { if (is_null($stockStatus)) { $websiteId = $product->getStore()->getWebsiteId(); $status = $this->getProductStockStatus($product->getId(), $websiteId, $stockId); $stockStatus = isset($status[$product->getId()]) ? $status[$product->getId()] : null; } $product->setIsSalable($stockStatus); return $this; }
/** * Get downloadable product links * * @param \Magento\Catalog\Model\Product $product * @return \Magento\Downloadable\Model\Link[] */ public function getLinks($product) { if ($product->getDownloadableLinks() === null) { /** @var \Magento\Downloadable\Model\Resource\Link\Collection $linkCollection */ $linkCollection = $this->_linksFactory->create()->addProductToFilter($product->getId())->addTitleToResult($product->getStoreId())->addPriceToResult($product->getStore()->getWebsiteId()); $this->extensionAttributesJoinProcessor->process($linkCollection); $linksCollectionById = []; foreach ($linkCollection as $link) { /* @var \Magento\Downloadable\Model\Link $link */ $link->setProduct($product); $linksCollectionById[$link->getId()] = $link; } $product->setDownloadableLinks($linksCollectionById); } return $product->getDownloadableLinks(); }
protected function handlePrice(Product &$product, $sub_products, &$customData) { $store = $product->getStore(); $type = $product->getTypeId(); $fields = $this->getFields($store); $customer_groups_enabled = $this->config->isCustomerGroupsEnabled($product->getStoreId()); $currencies = $this->currencyHelper->getConfigAllowCurrencies(); $baseCurrencyCode = $store->getBaseCurrencyCode(); $groups = []; if ($customer_groups_enabled) { $groups = $this->objectManager->create('Magento\\Customer\\Model\\ResourceModel\\Group\\Collection'); } foreach ($fields as $field => $with_tax) { $customData[$field] = []; foreach ($currencies as $currency_code) { $customData[$field][$currency_code] = []; $price = (double) $this->catalogHelper->getTaxPrice($product, $product->getPrice(), $with_tax, null, null, null, $product->getStore(), null); $price = $this->currencyDirectory->currencyConvert($price, $baseCurrencyCode, $currency_code); $customData[$field][$currency_code]['default'] = $price; $customData[$field][$currency_code]['default_formated'] = $this->formatPrice($price, false, $currency_code); $special_price = (double) $this->catalogHelper->getTaxPrice($product, $product->getFinalPrice(), $with_tax, null, null, null, $product->getStore(), null); $special_price = $this->currencyDirectory->currencyConvert($special_price, $baseCurrencyCode, $currency_code); if ($customer_groups_enabled) { // If fetch special price for groups foreach ($groups as $group) { $group_id = (int) $group->getData('customer_group_id'); $product->setCustomerGroupId($group_id); $discounted_price = $product->getPriceModel()->getFinalPrice(1, $product); $discounted_price = $this->currencyDirectory->currencyConvert($discounted_price, $baseCurrencyCode, $currency_code); if ($discounted_price !== false) { $customData[$field][$currency_code]['group_' . $group_id] = (double) $this->catalogHelper->getTaxPrice($product, $discounted_price, $with_tax, null, null, null, $product->getStore(), null); $customData[$field][$currency_code]['group_' . $group_id] = $this->currencyDirectory->currencyConvert($customData[$field][$currency_code]['group_' . $group_id], $baseCurrencyCode, $currency_code); $customData[$field][$currency_code]['group_' . $group_id . '_formated'] = $this->formatPrice($customData[$field][$currency_code]['group_' . $group_id], false, $currency_code); } else { $customData[$field][$currency_code]['group_' . $group_id] = $customData[$field][$currency_code]['default']; $customData[$field][$currency_code]['group_' . $group_id . '_formated'] = $customData[$field][$currency_code]['default_formated']; } } $product->setCustomerGroupId(null); } $customData[$field][$currency_code]['special_from_date'] = strtotime($product->getSpecialFromDate()); $customData[$field][$currency_code]['special_to_date'] = strtotime($product->getSpecialToDate()); if ($customer_groups_enabled) { foreach ($groups as $group) { $group_id = (int) $group->getData('customer_group_id'); if ($special_price && $special_price < $customData[$field][$currency_code]['group_' . $group_id]) { $customData[$field][$currency_code]['group_' . $group_id] = $special_price; $customData[$field][$currency_code]['group_' . $group_id . '_formated'] = $this->formatPrice($special_price, false, $currency_code); } } } else { if ($special_price && $special_price < $customData[$field][$currency_code]['default']) { $customData[$field][$currency_code]['default_original_formated'] = $customData[$field][$currency_code]['default_formated']; $customData[$field][$currency_code]['default'] = $special_price; $customData[$field][$currency_code]['default_formated'] = $this->formatPrice($special_price, false, $currency_code); } } if ($type == 'configurable' || $type == 'grouped' || $type == 'bundle') { $min = PHP_INT_MAX; $max = 0; if ($type == 'bundle') { $_priceModel = $product->getPriceModel(); list($min, $max) = $_priceModel->getTotalPrices($product, null, $with_tax, true); } if ($type == 'grouped' || $type == 'configurable') { if (count($sub_products) > 0) { foreach ($sub_products as $sub_product) { $price = (double) $this->catalogHelper->getTaxPrice($product, $sub_product->getFinalPrice(), $with_tax, null, null, null, $product->getStore(), null); $min = min($min, $price); $max = max($max, $price); } } else { $min = $max; } // avoid to have PHP_INT_MAX in case of no subproducts (Corner case of visibility and stock options) } if ($min != $max) { $min = $this->currencyDirectory->currencyConvert($min, $baseCurrencyCode, $currency_code); $max = $this->currencyDirectory->currencyConvert($max, $baseCurrencyCode, $currency_code); $dashed_format = $this->formatPrice($min, false, $currency_code) . ' - ' . $this->formatPrice($max, false, $currency_code); if (isset($customData[$field][$currency_code]['default_original_formated']) === false || $min <= $customData[$field][$currency_code]['default']) { $customData[$field][$currency_code]['default_formated'] = $dashed_format; //// Do not keep special price that is already taken into account in min max unset($customData['price']['special_from_date']); unset($customData['price']['special_to_date']); unset($customData['price']['default_original_formated']); $customData[$field][$currency_code]['default'] = 0; // will be reset just after } if ($customer_groups_enabled) { foreach ($groups as $group) { $group_id = (int) $group->getData('customer_group_id'); if ($min != $max && $min <= $customData[$field][$currency_code]['group_' . $group_id]) { $customData[$field][$currency_code]['group_' . $group_id] = 0; $customData[$field][$currency_code]['group_' . $group_id . '_formated'] = $dashed_format; } } } } if ($customData[$field][$currency_code]['default'] == 0) { $customData[$field][$currency_code]['default'] = $min; if ($min === $max) { $min = $this->currencyDirectory->currencyConvert($min, $baseCurrencyCode, $currency_code); $customData[$field][$currency_code]['default'] = $min; $customData[$field][$currency_code]['default_formated'] = $this->formatPrice($min, false, $currency_code); } } if ($customer_groups_enabled) { foreach ($groups as $group) { $group_id = (int) $group->getData('customer_group_id'); if ($customData[$field][$currency_code]['group_' . $group_id] == 0) { $customData[$field][$currency_code]['group_' . $group_id] = $min; if ($min === $max) { $customData[$field][$currency_code]['group_' . $group_id . '_formated'] = $customData[$field][$currency_code]['default_formated']; } } } } } } } }
/** * Initialize collection parent product and add limitation join * * @param \Magento\Catalog\Model\Product $product * @return $this */ public function setProduct(\Magento\Catalog\Model\Product $product) { $this->_product = $product; if ($product && $product->getId()) { $this->_hasLinkFilter = true; $this->setStore($product->getStore()); } return $this; }
/** * Prepare product to save * * @param \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject * @param \Magento\Catalog\Model\Product $product * * @return \Magento\Catalog\Model\Product * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function afterInitialize(\Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject, \Magento\Catalog\Model\Product $product) { if ($downloadable = $this->request->getPost('downloadable')) { $product->setDownloadableData($downloadable); $extension = $product->getExtensionAttributes(); if (isset($downloadable['link']) && is_array($downloadable['link'])) { $links = []; foreach ($downloadable['link'] as $linkData) { if (!$linkData || isset($linkData['is_delete']) && (bool) $linkData['is_delete']) { continue; } else { unset($linkData['link_id']); // TODO: need to implement setLinkFileContent() $link = $this->linkFactory->create(['data' => $linkData]); if (isset($linkData['type'])) { $link->setLinkType($linkData['type']); } if (isset($linkData['file'])) { $link->setLinkFile($linkData['file']); } if (isset($linkData['file_content'])) { $link->setLinkFileContent($linkData['file_content']); } $link->setId(null); if (isset($linkData['sample']['type'])) { $link->setSampleType($linkData['sample']['type']); } if (isset($linkData['sample']['file'])) { $link->setSampleFile($linkData['sample']['file']); } if (isset($linkData['sample']['url'])) { $link->setSampleUrl($linkData['sample']['url']); } if (isset($linkData['sample']['file_content'])) { $link->setSampleFileContent($linkData['file_content']); } $link->setStoreId($product->getStoreId()); $link->setWebsiteId($product->getStore()->getWebsiteId()); $link->setProductWebsiteIds($product->getWebsiteIds()); if (!$link->getSortOrder()) { $link->setSortOrder(1); } if (null === $link->getPrice()) { $link->setPrice(0); } if ($link->getIsUnlimited()) { $link->setNumberOfDownloads(0); } $links[] = $link; } } $extension->setDownloadableProductLinks($links); } if (isset($downloadable['sample']) && is_array($downloadable['sample'])) { $samples = []; foreach ($downloadable['sample'] as $sampleData) { if (!$sampleData || isset($sampleData['is_delete']) && (bool) $sampleData['is_delete']) { continue; } else { unset($sampleData['sample_id']); $sample = $this->sampleFactory->create(['data' => $sampleData]); $sample->setId(null); $sample->setStoreId($product->getStoreId()); if (isset($sampleData['type'])) { $sample->setSampleType($sampleData['type']); } if (isset($sampleData['sample_url'])) { $sample->setSampleUrl($sampleData['sample_url']); } if (!$sample->getSortOrder()) { $sample->setSortOrder(1); } $samples[] = $sample; } } $extension->setDownloadableProductSamples($samples); } $product->setExtensionAttributes($extension); if ($product->getLinksPurchasedSeparately()) { $product->setTypeHasRequiredOptions(true)->setRequiredOptions(true); } else { $product->setTypeHasRequiredOptions(false)->setRequiredOptions(false); } } return $product; }
/** * Get Product Weee attributes for price renderer * * @param \Magento\Catalog\Model\Product $product * @param null|false|\Magento\Framework\DataObject $shipping Shipping Address * @param null|false|\Magento\Framework\DataObject $billing Billing Address * @param null|Website $website * @param bool $calculateTaxes * @return \Magento\Framework\DataObject[] */ public function getProductWeeeAttributesForRenderer($product, $shipping = null, $billing = null, $website = null, $calculateTaxes = false) { /** @var \Magento\Store\Model\Store $store */ if ($website) { $store = $this->_storeManager->getWebsite($website)->getDefaultGroup()->getDefaultStore(); } else { $store = $product->getStore(); } if ($this->isEnabled($store)) { return $this->getProductWeeeAttributes($product, $shipping, $billing, $website, $calculateTaxes ? $calculateTaxes : $this->typeOfDisplay(1)); } return []; }
/** * {@inheritdoc} */ public function getStore() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getStore'); if (!$pluginInfo) { return parent::getStore(); } else { return $this->___callPlugins('getStore', func_get_args(), $pluginInfo); } }
/** * Gets minimal sales quantity * * @param \Magento\Catalog\Model\Product $product * @return int|null */ public function getMinimalQty($product) { $stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId()); $minSaleQty = $stockItem->getMinSaleQty(); return $minSaleQty > 0 ? $minSaleQty : null; }