Magento 2.2.5按价格排序:从低到高和从高到低

时间:2018-09-20 12:11:14

标签: magento2

需求:-在产品列表magento2.2.5中按价格从低到高和从高到低排序

我们遵循此Tuto,但不起作用 Sort by price low to high and high to low in product listing magento2

有人帮我吗?

1 个答案:

答案 0 :(得分:0)

正如您提到的,我遵循的示例与您发现的示例相同,但对我不起作用,我对roundingSetCollection函数做了很小的改动,如下所示:

public function aroundSetCollection(
  \Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
  \Closure $proceed,
  $collection
) {
  $currentOrder     = $subject->getCurrentOrder();
  $result            = $proceed($collection);

  if($currentOrder) {
    if($currentOrder == 'high_to_low') {
      $collection->setOrder('price', 'desc');
    } else if($currentOrder == 'low_to_high') {
      $collection->setOrder('price', 'asc');
    } else if ($currentOrder == 'created_at') {
      $collection->setOrder('created_at', 'desc');
    }
  }

  return $collection;

}