我有一个用Magento 2.4创建的多商店网站的问题。现在,类别页面和搜索页面中的产品按名称排列。排序的选项有:字母A-Z和Z-A,以及从低到高和从高到低的价格。当我试图按字母顺序对产品进行排序时,一切都很好,但当我尝试按价格排序时,这就像一个随机排序。我还放入了一个自定义模块,但结果相同。相反,如果我按产品id或重量对它们进行分类,那没关系。
if ($currentOrder) {
if ($currentOrder == 'price_asc') {
$subject->getCollection()->setOrder('price', 'asc');
} elseif ($currentOrder == 'price_desc') {
$subject->getCollection()->setOrder('price', 'desc');
} elseif ($currentOrder == 'name_asc') {
$subject->getCollection()->setOrder('name', 'asc');
} elseif ($currentOrder == 'name_desc') {
$subject->getCollection()->setOrder('name', 'desc');
}
}
我也把->getStoreId(1)
放在getCollection()
之后,但我得到了相同的结果。编辑:从我看到的不是按价格排序,而是按产品id 排序
添加Magento 2的步骤按价格从低到高排序&从高到低选项:
步骤1:在app\code\Vendor\Extension 中创建registration.php文件
步骤2:在app\code\Vendor\Extension\etc 中创建module.xml文件
步骤3:在app\code\Vendor\Extension\etc 中创建di.xml文件
步骤4:在app\code\Vendor\Extension\Plugin\Catalog\Plock 中创建Toolbar.php文件
步骤5:在app\code\Vendor\Extension\Plugin\Catalog\Model 中创建Config.php文件
您可以在此处查看详细信息。
It适用于Magento 2.4版本我假设你已经创建了一个自定义模块Vendor/module_sort
- 在vendor/module_sort等中创建di.xml文件/
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoCatalogModelConfig">
<plugin name="Vendor_module_sort::addCustomOptions" type="Vendormodule_sortPluginModelConfig" />
</type>
<preference for="MagentoCatalogBlockProductProductListToolbar" type="Vendormodule_sortPluginCatalogBlockToolbar" />
</config>
- 在Vendor/module_sort/plugin/Model中创建Config.php
<?php
namespace Vendormodule_sortPluginModel;
class Config {
public function afterGetAttributeUsedForSortByArray(MagentoCatalogModelConfig $catalogConfig, $options)
{
unset($options['name']);
unset($options['price']);
//New sorting options
$customOption['high_to_low'] = __( 'Price High to Low' );
$customOption['low_to_high'] = __( 'Price Low to High' );
$customOption['name_Z_to_A'] = __( 'Product Title:Z-A' );
$customOption['name_A_to_Z'] = __( 'Product Title:A-Z' );
// $customOption['name'] = $default_options['name'];
//Merge default sorting options with custom options
$options = array_merge($customOption, $options);
return $options;
}
}
- 在Vendor/module_sort/plugin/Catalog/Block中创建Toolbar.php/
<?php
namespace Vendormodule_sortPluginCatalogBlock;
class Toolbar extends MagentoCatalogBlockProductProductListToolbar
{
/**
* Set collection to pager
*
* @param MagentoFrameworkDataCollection $collection
* @return MagentoCatalogBlockProductProductListToolbar
*/
public function setCollection($collection)
{
$this->_collection = $collection;
$this->_collection->setCurPage($this->getCurrentPage());
// we need to set pagination only if passed value integer and more that 0
$limit = (int)$this->getLimit();
if ($limit) {
$this->_collection->setPageSize($limit);
}
if ($this->getCurrentOrder()) {
if (($this->getCurrentOrder()) == 'position') {
$this->_collection->addAttributeToSort(
$this->getCurrentOrder(),
$this->getCurrentDirection()
);
} else {
if ($this->getCurrentOrder() == 'high_to_low') {
$this->_collection->setOrder('price', 'desc');
} elseif ($this->getCurrentOrder() == 'low_to_high') {
$this->_collection->setOrder('price', 'asc');
}elseif ($this->getCurrentOrder() == 'name_Z_to_A') {
$this->_collection->setOrder('name', 'desc');
}elseif ($this->getCurrentOrder() == 'name_A_to_Z') {
$this->_collection->setOrder('name', 'asc');
}
}
}
return $this;
}
}