/** * 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(); /** @var \Magento\Catalog\Model\Product $duplicate */ $duplicate = $this->productFactory->create(); $duplicate->setData($product->getData()); $duplicate->setOptions([]); $duplicate->setIsDuplicate(true); $duplicate->setOriginalId($product->getEntityId()); $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); $this->getOptionRepository()->duplicate($product, $duplicate); $metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class); $product->getResource()->duplicate($product->getData($metadata->getLinkField()), $duplicate->getData($metadata->getLinkField())); return $duplicate; }
/** * @param CatalogProduct $product * @param array $identities * @return string[] */ public function afterGetIdentities(CatalogProduct $product, array $identities) { foreach ($this->type->getParentIdsByChild($product->getEntityId()) as $parentId) { $identities[] = CatalogProduct::CACHE_TAG . '_' . $parentId; } return $identities; }
/** * Generate list of urls for global scope * * @param \Magento\Framework\Data\Collection $productCategories * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ protected function generateForGlobalScope($productCategories) { $urls = []; $productId = $this->product->getEntityId(); foreach ($this->product->getStoreIds() as $id) { if (!$this->isGlobalScope($id) && !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore($id, $productId, Product::ENTITY)) { $urls = array_merge($urls, $this->generateForSpecificStoreView($id, $productCategories)); } } return $urls; }
/** * Method for product filter * * @param \Magento\Catalog\Model\Product|array|integer|null $product * @return $this */ public function addProductToFilter($product) { if (empty($product)) { $this->addFieldToFilter('product_id', ''); } else { $this->join(['cpe' => $this->getTable('catalog_product_entity')], sprintf('cpe.%s = main_table.product_id', $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField())); if ($product instanceof \Magento\Catalog\Model\Product) { $this->addFieldToFilter('cpe.entity_id', $product->getEntityId()); } else { $this->addFieldToFilter('cpe.entity_id', ['in' => $product]); } } return $this; }
/** * Retrieve url for add product to cart * * @param \Magento\Catalog\Model\Product $product * @param array $additional * @return string */ public function getAddUrl($product, $additional = []) { $continueUrl = $this->urlEncoder->encode($this->_urlBuilder->getCurrentUrl()); $urlParamName = \Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED; $routeParams = [$urlParamName => $continueUrl, 'product' => $product->getEntityId(), '_secure' => $this->_getRequest()->isSecure()]; if (!empty($additional)) { $routeParams = array_merge($routeParams, $additional); } if ($product->hasUrlDataObject()) { $routeParams['_scope'] = $product->getUrlDataObject()->getStoreId(); $routeParams['_scope_to_url'] = true; } if ($this->_getRequest()->getRouteName() == 'checkout' && $this->_getRequest()->getControllerName() == 'cart') { $routeParams['in_cart'] = 1; } return $this->_getUrl('checkout/cart/add', $routeParams); }
/** * Retrieve params for adding product to wishlist * * @param \Magento\Catalog\Model\Product|\Magento\Wishlist\Model\Item $item * @param array $params * @return string */ public function getAddParams($item, array $params = array()) { $productId = null; if ($item instanceof \Magento\Catalog\Model\Product) { $productId = $item->getEntityId(); } if ($item instanceof \Magento\Wishlist\Model\Item) { $productId = $item->getProductId(); } $url = $this->_getUrlStore($item)->getUrl('wishlist/index/add'); if ($productId) { $params['product'] = $productId; } return $this->_postDataHelper->getPostData($url, $params); }
/** * Retrieve bundle option collection * * @param \Magento\Catalog\Model\Product $product * @return \Magento\Bundle\Model\ResourceModel\Option\Collection */ public function getOptionsCollection($product) { if (!$product->hasData($this->_keyOptionsCollection)) { /** @var \Magento\Bundle\Model\ResourceModel\Option\Collection $optionsCollection */ $optionsCollection = $this->_bundleOption->create()->getResourceCollection(); $optionsCollection->setProductIdFilter($product->getEntityId()); $this->setStoreFilter($product->getStoreId(), $product); $optionsCollection->setPositionOrder(); $storeId = $this->getStoreFilter($product); if ($storeId instanceof \Magento\Store\Model\Store) { $storeId = $storeId->getId(); } $optionsCollection->joinValues($storeId); $product->setData($this->_keyOptionsCollection, $optionsCollection); } return $product->getData($this->_keyOptionsCollection); }
/** * Get downloadable product samples * * @param \Magento\Catalog\Model\Product $product * @return \Magento\Downloadable\Model\ResourceModel\Sample\Collection */ public function getSamples($product) { if ($product->getDownloadableSamples() === null) { $sampleCollection = $this->_samplesFactory->create()->addProductToFilter($product->getEntityId())->addTitleToResult($product->getStoreId()); $this->extensionAttributesJoinProcessor->process($sampleCollection); $product->setDownloadableSamples($sampleCollection); } return $product->getDownloadableSamples(); }
/** * Retrieve category ids where product is available * * @param \Magento\Catalog\Model\Product $object * @return array */ public function getAvailableInCategories($object) { // is_parent=1 ensures that we'll get only category IDs those are direct parents of the product, instead of // fetching all parent IDs, including those are higher on the tree $select = $this->getConnection()->select()->distinct()->from($this->getTable('catalog_category_product_index'), ['category_id'])->where('product_id = ? AND is_parent = 1', (int) $object->getEntityId())->where('visibility != ?', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE); return $this->getConnection()->fetchCol($select); }
/** * Check availability display product in category * * @param \Magento\Catalog\Model\Product $product * @param int $categoryId * @return string */ public function canBeShowInCategory($product, $categoryId) { $select = $this->getConnection()->select()->from($this->getTable('catalog_category_product_index'), 'product_id')->where('product_id = ?', (int) $product->getEntityId())->where('category_id = ?', (int) $categoryId); return $this->getConnection()->fetchOne($select); }
/** * Retrieve Product URL using UrlDataObject * * @param \Magento\Catalog\Model\Product $product * @param array $params * @return string */ public function getUrl(\Magento\Catalog\Model\Product $product, $params = array()) { $routePath = ''; $routeParams = $params; $storeId = $product->getStoreId(); if (isset($params['_ignore_category'])) { unset($params['_ignore_category']); $categoryId = null; } else { $categoryId = $product->getCategoryId() && !$product->getDoNotUseCategoryId() ? $product->getCategoryId() : null; } if ($product->hasUrlDataObject()) { $requestPath = $product->getUrlDataObject()->getUrlRewrite(); $routeParams['_scope'] = $product->getUrlDataObject()->getStoreId(); } else { $requestPath = $product->getRequestPath(); if (empty($requestPath) && $requestPath !== false) { $idPath = sprintf('product/%d', $product->getEntityId()); if ($categoryId) { $idPath = sprintf('%s/%d', $idPath, $categoryId); } $rewrite = $this->getUrlRewrite(); $rewrite->setStoreId($storeId)->loadByIdPath($idPath); if ($rewrite->getId()) { $requestPath = $rewrite->getRequestPath(); $product->setRequestPath($requestPath); } else { $product->setRequestPath(false); } } } if (isset($routeParams['_scope'])) { $storeId = $this->_storeManager->getStore($routeParams['_scope'])->getId(); } if ($storeId != $this->_storeManager->getStore()->getId()) { $routeParams['_scope_to_url'] = true; } if (!empty($requestPath)) { $routeParams['_direct'] = $requestPath; } else { $routePath = 'catalog/product/view'; $routeParams['id'] = $product->getId(); $routeParams['s'] = $product->getUrlKey(); if ($categoryId) { $routeParams['category'] = $categoryId; } } // reset cached URL instance GET query params if (!isset($routeParams['_query'])) { $routeParams['_query'] = array(); } return $this->getUrlInstance()->setScope($storeId)->getUrl($routePath, $routeParams); }
/** * @param int $websiteId * @param Product|null $product * @return \Zend_Db_Statement_Interface * @throws \Magento\Framework\Exception\LocalizedException */ protected function getRuleProductsStmt($websiteId, Product $product = null) { /** * Sort order is important * It used for check stop price rule condition. * website_id customer_group_id product_id sort_order * 1 1 1 0 * 1 1 1 1 * 1 1 1 2 * if row with sort order 1 will have stop flag we should exclude * all next rows for same product id from price calculation */ $select = $this->connection->select()->from(['rp' => $this->getTable('catalogrule_product')])->order(['rp.website_id', 'rp.customer_group_id', 'rp.product_id', 'rp.sort_order', 'rp.rule_id']); if ($product && $product->getEntityId()) { $select->where('rp.product_id=?', $product->getEntityId()); } /** * Join default price and websites prices to result */ $priceAttr = $this->eavConfig->getAttribute(Product::ENTITY, 'price'); $priceTable = $priceAttr->getBackend()->getTable(); $attributeId = $priceAttr->getId(); $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); $select->join(['e' => $this->getTable('catalog_product_entity')], sprintf('e.entity_id = rp.product_id'), []); $joinCondition = '%1$s.' . $linkField . '=e.' . $linkField . ' AND (%1$s.attribute_id=' . $attributeId . ') and %1$s.store_id=%2$s'; $select->join(['pp_default' => $priceTable], sprintf($joinCondition, 'pp_default', \Magento\Store\Model\Store::DEFAULT_STORE_ID), []); $website = $this->storeManager->getWebsite($websiteId); $defaultGroup = $website->getDefaultGroup(); if ($defaultGroup instanceof \Magento\Store\Model\Group) { $storeId = $defaultGroup->getDefaultStoreId(); } else { $storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID; } $select->joinInner(['product_website' => $this->getTable('catalog_product_website')], 'product_website.product_id=rp.product_id ' . 'AND product_website.website_id = rp.website_id ' . 'AND product_website.website_id=' . $websiteId, []); $tableAlias = 'pp' . $websiteId; $select->joinLeft([$tableAlias => $priceTable], sprintf($joinCondition, $tableAlias, $storeId), []); $select->columns(['default_price' => $this->connection->getIfNullSql($tableAlias . '.value', 'pp_default.value')]); return $this->connection->query($select); }
/** * Retrieve additional searchable data from type instance * Using based on product id and store_id data * * @param \Magento\Catalog\Model\Product $product * @return array */ public function getSearchableData($product) { $searchData = []; if ($product->getHasOptions()) { $searchData = $this->_catalogProductOption->getSearchableData($product->getEntityId(), $product->getStoreId()); } return $searchData; }
/** * {@inheritdoc} */ public function getEntityId() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getEntityId'); if (!$pluginInfo) { return parent::getEntityId(); } else { return $this->___callPlugins('getEntityId', func_get_args(), $pluginInfo); } }