/**
  * 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;
 }
Exemple #2
0
 /**
  * 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();
 }
Exemple #3
0
 /**
  * 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);
 }
Exemple #4
0
 /**
  * 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;
 }
Exemple #5
0
 /**
  * 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);
 }
Exemple #6
0
 /**
  * @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());
 }
 /**
  * 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())->addFieldsToFilter($this->_registry->registry('advanced_search_conditions'))->setStore($this->storeManager->getStore())->addMinimalPrice()->addTaxPercents()->addStoreFilter()->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());
 }