public function testGetProductAttributes() { $expectedResult = ['attribute_one', 'attribute_two', 'attribute_three']; $this->_catalogConfig->expects($this->once())->method('getProductAttributes')->willReturn(['attribute_one', 'attribute_two']); $this->_attributeConfig->expects($this->once())->method('getAttributeNames')->with('wishlist_item')->willReturn(['attribute_three']); $this->assertEquals($expectedResult, $this->model->getProductAttributes()); }
/** * Append bundles in upsell list for current product * * @param \Magento\Framework\Event\Observer $observer * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function execute(\Magento\Framework\Event\Observer $observer) { /* @var $product \Magento\Catalog\Model\Product */ $product = $observer->getEvent()->getProduct(); /** * Check is current product type is allowed for bundle selection product type */ if (!in_array($product->getTypeId(), $this->bundleData->getAllowedSelectionTypes())) { return $this; } /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection */ $collection = $observer->getEvent()->getCollection(); $limit = $observer->getEvent()->getLimit(); if (is_array($limit)) { if (isset($limit['upsell'])) { $limit = $limit['upsell']; } else { $limit = 0; } } /* @var $resource \Magento\Bundle\Model\ResourceModel\Selection */ $resource = $this->bundleSelection; $productIds = array_keys($collection->getItems()); if ($limit !== null && $limit <= count($productIds)) { return $this; } // retrieve bundle product ids $bundleIds = $resource->getParentIdsByChild($product->getId()); // exclude up-sell product ids $bundleIds = array_diff($bundleIds, $productIds); if (!$bundleIds) { return $this; } /* @var $bundleCollection \Magento\Catalog\Model\ResourceModel\Product\Collection */ $bundleCollection = $product->getCollection()->addAttributeToSelect($this->config->getProductAttributes())->addStoreFilter()->addMinimalPrice()->addFinalPrice()->addTaxPercents()->setVisibility($this->productVisibility->getVisibleInCatalogIds()); if ($limit !== null) { $bundleCollection->setPageSize($limit); } $bundleCollection->addFieldToFilter('entity_id', ['in' => $bundleIds])->setFlag('do_not_use_category_id', true); if ($collection instanceof \Magento\Framework\Data\Collection) { foreach ($bundleCollection as $item) { $collection->addItem($item); } } elseif ($collection instanceof \Magento\Framework\DataObject) { $items = $collection->getItems(); foreach ($bundleCollection as $item) { $items[$item->getEntityId()] = $item; } $collection->setItems($items); } return $this; }
/** * Load Available Orders * * @return $this */ private function loadAvailableOrders() { if ($this->_availableOrder === null) { $this->_availableOrder = $this->_catalogConfig->getAttributeUsedForSortByArray(); } return $this; }
/** * {@inheritdoc} */ public function run() { $this->logger->log("Installing {$this->productType} products:"); $product = $this->productFactory->create(); foreach ($this->fixtures as $file) { /** @var \Magento\SampleData\Helper\Csv\Reader $csvReader */ $fileName = $this->fixtureHelper->getPath($file); $csvReader = $this->csvReaderFactory->create(['fileName' => $fileName, 'mode' => 'r']); foreach ($csvReader as $row) { $attributeSetId = $this->catalogConfig->getAttributeSetId(4, $row['attribute_set']); $this->converter->setAttributeSetId($attributeSetId); $data = $this->converter->convertRow($row); $product->unsetData(); $product->setData($data); $product->setTypeId($this->productType)->setAttributeSetId($attributeSetId)->setWebsiteIds([$this->storeManager->getWebsiteId()])->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)->setStockData(['is_in_stock' => 1, 'manage_stock' => 0])->setStoreId(\Magento\Store\Model\Store::DEFAULT_STORE_ID); if (empty($data['visibility'])) { $product->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH); } $this->prepareProduct($product, $data); $product->save(); $this->gallery->install($product); $this->logger->logInline('.'); } } }
/** * Init Toolbar * * @return null */ protected function _construct() { parent::_construct(); $this->_orderField = $this->_productListHelper->getDefaultSortField(); $this->_availableOrder = $this->_catalogConfig->getAttributeUsedForSortByArray(); $this->_availableMode = $this->_productListHelper->getAvailableViewMode(); }
/** * @param array $productFixtures * @param array $galleryFixtures * @throws \Exception */ public function install(array $productFixtures, array $galleryFixtures) { $this->eavConfig->clear(); $this->setGalleryFixtures($galleryFixtures); $product = $this->productFactory->create(); foreach ($productFixtures as $fileName) { $fileName = $this->fixtureManager->getFixture($fileName); if (!file_exists($fileName)) { continue; } $rows = $this->csvReader->getData($fileName); $header = array_shift($rows); foreach ($rows as $row) { $data = []; foreach ($row as $key => $value) { $data[$header[$key]] = $value; } $row = $data; $attributeSetId = $this->catalogConfig->getAttributeSetId(4, $row['attribute_set']); $this->converter->setAttributeSetId($attributeSetId); $data = $this->converter->convertRow($row); $product->unsetData(); $product->setData($data); $product->setTypeId($this->productType)->setAttributeSetId($attributeSetId)->setWebsiteIds([$this->storeManager->getDefaultStoreView()->getWebsiteId()])->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)->setStockData(['is_in_stock' => 1, 'manage_stock' => 0])->setStoreId(\Magento\Store\Model\Store::DEFAULT_STORE_ID); if (empty($data['visibility'])) { $product->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH); } $this->prepareProduct($product, $data); $product->save(); $this->installGallery($product); } } }
/** * Prepare collection to be displayed in the grid * * @return $this */ protected function _prepareCollection() { $attributes = $this->_catalogConfig->getProductAttributes(); /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */ $collection = $this->_productFactory->create()->getCollection(); $collection->setStore($this->getStore())->addAttributeToSelect($attributes)->addAttributeToSelect('sku')->addStoreFilter()->addAttributeToFilter('type_id', $this->_salesConfig->getAvailableProductTypes())->addAttributeToSelect('gift_message_available'); $this->setCollection($collection); return parent::_prepareCollection(); }
/** * Get select for all products * * @param \Magento\Store\Model\Store $store * @return \Magento\Framework\DB\Select */ protected function getAllProducts(\Magento\Store\Model\Store $store) { if (!isset($this->productsSelects[$store->getId()])) { $statusAttributeId = $this->config->getAttribute(\Magento\Catalog\Model\Product::ENTITY, 'status')->getId(); $visibilityAttributeId = $this->config->getAttribute(\Magento\Catalog\Model\Product::ENTITY, 'visibility')->getId(); $select = $this->getWriteAdapter()->select()->from(['cp' => $this->getTable('catalog_product_entity')], [])->joinInner(['cpw' => $this->getTable('catalog_product_website')], 'cpw.product_id = cp.entity_id', [])->joinInner(['cpsd' => $this->getTable('catalog_product_entity_int')], 'cpsd.entity_id = cp.entity_id AND cpsd.store_id = 0' . ' AND cpsd.attribute_id = ' . $statusAttributeId, [])->joinLeft(['cpss' => $this->getTable('catalog_product_entity_int')], 'cpss.entity_id = cp.entity_id AND cpss.attribute_id = cpsd.attribute_id' . ' AND cpss.store_id = ' . $store->getId(), [])->joinInner(['cpvd' => $this->getTable('catalog_product_entity_int')], 'cpvd.entity_id = cp.entity_id AND cpvd.store_id = 0' . ' AND cpvd.attribute_id = ' . $visibilityAttributeId, [])->joinLeft(['cpvs' => $this->getTable('catalog_product_entity_int')], 'cpvs.entity_id = cp.entity_id AND cpvs.attribute_id = cpvd.attribute_id ' . ' AND cpvs.store_id = ' . $store->getId(), [])->joinLeft(['ccp' => $this->getTable('catalog_category_product')], 'ccp.product_id = cp.entity_id', [])->where('cpw.website_id = ?', $store->getWebsiteId())->where($this->getWriteAdapter()->getIfNullSql('cpss.value', 'cpsd.value') . ' = ?', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)->where($this->getWriteAdapter()->getIfNullSql('cpvs.value', 'cpvd.value') . ' IN (?)', [\Magento\Catalog\Model\Product\Visibility::VISIBILITY_IN_CATALOG, \Magento\Catalog\Model\Product\Visibility::VISIBILITY_IN_SEARCH, \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH])->group('cp.entity_id')->columns(['category_id' => new \Zend_Db_Expr($store->getRootCategoryId()), 'product_id' => 'cp.entity_id', 'position' => new \Zend_Db_Expr($this->getWriteAdapter()->getCheckSql('ccp.product_id IS NOT NULL', 'ccp.position', '0')), 'is_parent' => new \Zend_Db_Expr($this->getWriteAdapter()->getCheckSql('ccp.product_id IS NOT NULL', '1', '0')), 'store_id' => new \Zend_Db_Expr($store->getId()), 'visibility' => new \Zend_Db_Expr($this->getWriteAdapter()->getIfNullSql('cpvs.value', 'cpvd.value'))]); $this->productsSelects[$store->getId()] = $select; } return $this->productsSelects[$store->getId()]; }
protected function generalGetProductCollection() { $this->eventManager->expects($this->once())->method('dispatch')->will($this->returnValue(true)); $this->scopeConfig->expects($this->once())->method('getValue')->withAnyParameters()->willReturn(false); $this->cacheState->expects($this->atLeastOnce())->method('isEnabled')->withAnyParameters()->willReturn(false); $this->catalogConfig->expects($this->once())->method('getProductAttributes')->willReturn([]); $this->localDate->expects($this->any())->method('date')->willReturn(new \DateTime('now', new \DateTimeZone('UTC'))); $this->context->expects($this->once())->method('getEventManager')->willReturn($this->eventManager); $this->context->expects($this->once())->method('getScopeConfig')->willReturn($this->scopeConfig); $this->context->expects($this->once())->method('getCacheState')->willReturn($this->cacheState); $this->context->expects($this->once())->method('getCatalogConfig')->willReturn($this->catalogConfig); $this->context->expects($this->once())->method('getLocaleDate')->willReturn($this->localDate); $this->productCollection = $this->getMockBuilder('Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection')->setMethods(['setVisibility', 'addMinimalPrice', 'addFinalPrice', 'addTaxPercents', 'addAttributeToSelect', 'addUrlRewrite', 'addStoreFilter', 'addAttributeToSort', 'setPageSize', 'setCurPage', 'addAttributeToFilter'])->disableOriginalConstructor()->getMock(); $this->productCollection->expects($this->once())->method('setVisibility')->willReturnSelf(); $this->productCollection->expects($this->once())->method('addMinimalPrice')->willReturnSelf(); $this->productCollection->expects($this->once())->method('addFinalPrice')->willReturnSelf(); $this->productCollection->expects($this->once())->method('addTaxPercents')->willReturnSelf(); $this->productCollection->expects($this->once())->method('addAttributeToSelect')->willReturnSelf(); $this->productCollection->expects($this->once())->method('addUrlRewrite')->willReturnSelf(); $this->productCollection->expects($this->once())->method('addStoreFilter')->willReturnSelf(); $this->productCollection->expects($this->once())->method('addAttributeToSort')->willReturnSelf(); $this->productCollection->expects($this->atLeastOnce())->method('setCurPage')->willReturnSelf(); $this->productCollection->expects($this->any())->method('addAttributeToFilter')->willReturnSelf(); }
/** * Prepare product collection * * @param Collection $collection * @return $this */ public function prepareProductCollection($collection) { $collection->addAttributeToSelect($this->_catalogConfig->getProductAttributes())->setStore($this->_storeManager->getStore())->addMinimalPrice()->addTaxPercents()->addStoreFilter()->setVisibility($this->_catalogProductVisibility->getVisibleInSearchIds()); return $this; }
/** * Retrieve bundle selections collection based on used options * * @param array $optionIds * @param \Magento\Catalog\Model\Product $product * @return \Magento\Bundle\Model\ResourceModel\Selection\Collection */ public function getSelectionsCollection($optionIds, $product) { $keyOptionIds = is_array($optionIds) ? implode('_', $optionIds) : ''; $key = $this->_keySelectionsCollection . $keyOptionIds; if (!$product->hasData($key)) { $storeId = $product->getStoreId(); $selectionsCollection = $this->_bundleCollection->create()->addAttributeToSelect($this->_config->getProductAttributes())->addAttributeToSelect('tax_class_id')->setFlag('require_stock_items', true)->setFlag('product_children', true)->setPositionOrder()->addStoreFilter($this->getStoreFilter($product))->setStoreId($storeId)->addFilterByRequiredOptions()->setOptionIdsFilter($optionIds); if (!$this->_catalogData->isPriceGlobal() && $storeId) { $websiteId = $this->_storeManager->getStore($storeId)->getWebsiteId(); $selectionsCollection->joinPrices($websiteId); } $product->setData($key, $selectionsCollection); } return $product->getData($key); }
/** * @param \Magento\Catalog\Model\Resource\Product\Collection $collection * @param \Magento\Catalog\Model\Category $category * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function filter($collection, \Magento\Catalog\Model\Category $category) { $collection->addAttributeToSelect($this->catalogConfig->getProductAttributes())->addFieldsToFilter($this->_registry->registry('advanced_search_conditions'))->setStore($this->storeManager->getStore())->addMinimalPrice()->addTaxPercents()->addStoreFilter()->setVisibility($this->productVisibility->getVisibleInSearchIds()); }
/** * Retrieve attribute instance * Special for non static flat table * * @param mixed $attribute * @return \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */ public function getAttribute($attribute) { return $this->_catalogConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attribute); }
/** * Add all attributes and apply pricing logic to products collection * to get correct values in different products lists. * E.g. crosssells, upsells, new products, recently viewed * * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection * @return \Magento\Catalog\Model\ResourceModel\Product\Collection */ protected function _addProductAttributesAndPrices(\Magento\Catalog\Model\ResourceModel\Product\Collection $collection) { return $collection->addMinimalPrice()->addFinalPrice()->addTaxPercents()->addAttributeToSelect($this->_catalogConfig->getProductAttributes())->addUrlRewrite(); }
/** * @param \Magento\Catalog\Model\Resource\Product\Collection $collection * @param \Magento\Catalog\Model\Category $category * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function filter($collection, \Magento\Catalog\Model\Category $category) { $collection->addAttributeToSelect($this->catalogConfig->getProductAttributes())->addSearchFilter($this->helper->getQuery()->getQueryText())->setStore($this->storeManager->getStore())->addMinimalPrice()->addFinalPrice()->addTaxPercents()->addStoreFilter()->addUrlRewrite()->setVisibility($this->productVisibility->getVisibleInSearchIds()); }
/** * Filter product collection * * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection * @param \Magento\Catalog\Model\Category $category * @return void */ public function filter($collection, \Magento\Catalog\Model\Category $category) { $collection->addAttributeToSelect($this->catalogConfig->getProductAttributes())->addMinimalPrice()->addFinalPrice()->addTaxPercents()->addUrlRewrite($category->getId())->setVisibility($this->productVisibility->getVisibleInCatalogIds()); }
/** * Get product attributes that need in wishlist * * @return array */ public function getProductAttributes() { $catalogAttributes = $this->_catalogConfig->getProductAttributes(); $wishlistAttributes = $this->_attributeConfig->getAttributeNames('wishlist_item'); return array_merge($catalogAttributes, $wishlistAttributes); }
/** * Retrieve Product Listing Default Sort By * * @return string */ public function getDefaultSortBy() { if (!($sortBy = $this->getData('default_sort_by'))) { $sortBy = $this->_catalogConfig->getProductListDefaultSortBy($this->getStoreId()); } $available = $this->getAvailableSortByOptions(); if (!isset($available[$sortBy])) { $sortBy = array_keys($available); $sortBy = $sortBy[0]; } return $sortBy; }
public function testToOptionalArray() { $except = [['label' => __('Position'), 'value' => 'position'], ['label' => 'testLabel', 'value' => 'testAttributeCode']]; $this->catalogConfig->expects($this->any())->method('getAttributesUsedForSortBy')->will($this->returnValue([['frontend_label' => 'testLabel', 'attribute_code' => 'testAttributeCode']])); $this->assertEquals($except, $this->model->toOptionArray()); }