根据final_price获取Magento 2系列

时间:2018-03-03 13:40:01

标签: collections magento2

我有以下代码来获取Magento 2产品系列:

<?php namespace Qxs\Related\Block;

class Related extends \Magento\Framework\View\Element\Template
{
  public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
    \Magento\Catalog\Model\Product\Visibility $productVisibility,
    \Magento\Framework\Registry $registry,
    array $data = []
  )
  {
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->productStatus = $productStatus;
    $this->productVisibility = $productVisibility;
    $this->_registry = $registry;
    parent::__construct($context, $data);
  }

  public function getProductCollection()
  {
    try {

      $product = $this->_registry->registry('current_product');

      $range_percentage = 35;
      $price_temp = round($product->getFinalPrice() / 100 * $range_percentage);
      $price_from = $product->getFinalPrice() - $price_temp;
      $price_to = $product->getFinalPrice() + $price_temp;

      $categories = $product->getCategoryIds();

      $collection = $this->_productCollectionFactory->create();
      $collection->addAttributeToSelect('*')
        ->addCategoriesFilter(['in' => $categories])
        ->addPriceDataFieldFilter('%s >= %s', ['min_price', $price_from])
        ->addPriceDataFieldFilter('%s <= %s', ['min_price', $price_to])
        ->addMinimalPrice()
        ->addAttributeToFilter('entity_id', ['neq' => $product->getId()])
        ->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()])
        ->setVisibility($this->productVisibility->getVisibleInSiteIds())
        ->setPageSize(5);

      return $collection;

    } catch (\Exception $e) {

      var_dump($e->getMessage());
    }
  }
}

上面的代码更新了一个工作示例

它将使用addtofieldfilter&#39; price&#39;返回结果。但它不适用于final_price属性。我需要根据final_price进行排序,因为可配置的产品没有价格。代码返回:无效的属性名称。

如何在final_price属性中过滤价格范围?

谢谢,

1 个答案:

答案 0 :(得分:4)

final_price是价格索引表的一部分,因此您无法像使用字段和属性一样使用它。您需要加入价格指数才能根据final_price进行过滤和排序。幸运的是,Magento为我们添加了一些漂亮的功能,用于产品系列; addPriceDataFieldFilter()addFinalPrice()

<强>解决方案

为了能够实现上述逻辑,您需要将代码更改为:

$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*')
    ->addCategoriesFilter(['in' => $categories])
    ->addPriceDataFieldFilter('%s >= %s', ['final_price', $price_from])
    ->addPriceDataFieldFilter('%s <= %s', ['final_price', $price_to])
    ->addFinalPrice()
    ->addAttributeToFilter('entity_id', ['neq' => $product->getId()])
    ->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()])
    ->setVisibility($this->productVisibility->getVisibleInSiteIds())
    ->setPageSize(5);

注意功能的顺序。您必须始终在所有addFinalPrice()后致电addPriceDataFieldFilter(),否则将不会应用过滤器。

<强>加成

如果您想按final_price排序,可以在addFinalPrice()之后添加以下代码:

$collection->getSelect()->order('price_index.final_price ASC');

<强>参考

https://github.com/magento/magento2/blob/2.2/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php#L1420 https://github.com/magento/magento2/blob/2.2/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php#L2197