我需要添加一个基于created_at
属性的附加过滤器,以便按最新产品对产品列表进行排序。我试图使用下面的文件来计算
app/design/frontend/Vendor/ThemeName/Magento_Catalog/templates/product/list/toolbar/sorter.phtml
但是如何将我们的实体ID添加到getAvailableOrders()
?
我需要添加一个基于created_at
属性的附加过滤器,以便按最新产品对产品列表进行排序。我试图使用下面的文件来计算
app/design/frontend/Vendor/ThemeName/Magento_Catalog/templates/product/list/toolbar/sorter.phtml
但是如何将我们的实体ID添加到getAvailableOrders()
?
Answers:
如果要使用created_at
admin-> stores->(attribute)产品中不存在的属性,因为admin中定义的属性具有设置Sorting in Product Listing = Yes/No
,则必须使用以下两个文件:
\vendor\magento\module-catalog\Block\Product\ProductList\Toolbar.php
\vendor\magento\module-catalog\Model\Config.php
在Toolbar.php
你可以看到
$this->_availableOrder = $this->_catalogConfig->getAttributeUsedForSortByArray();
它getAttributeUsedForSortByArray()
从Config.php
返回的可用属性数组中进行调用以对列表集合进行排序。
现在,您必须在created_at
此处添加属性。怎么样?我做了一个插件
/**
* Add sort order option created_at to frontend
*/
public function afterGetAttributeUsedForSortByArray(
\Magento\Catalog\Model\Config $catalogConfig,
$options
) {
$options['created_at'] = __('New');
return $options;
}
您插入created_at
了可用属性进行排序,现在只需构建自定义集合即可使用它。在这里,我选择重写 \vendor\magento\module-catalog\Block\Product\ProductList\Toolbar.php
与我Toolbar.php
和覆盖setCollection()
/**
* Set collection to pager
*
* @param \Magento\Framework\Data\Collection $collection
* @return $this
*/
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);
}
// switch between sort order options
if ($this->getCurrentOrder()) {
// create custom query for created_at option
switch ($this->getCurrentOrder()) {
case 'created_at':
if ($this->getCurrentDirection() == 'desc') {
$this->_collection
->getSelect()
->order('e.created_at DESC');
} elseif ($this->getCurrentDirection() == 'asc') {
$this->_collection
->getSelect()
->order('e.created_at ASC');
}
break;
default:
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
break;
}
}
// echo '<pre>';
// var_dump($this->getCurrentOrder());
// var_dump((string) $this->_collection->getSelect());
// die;
return $this;
}
仅此而已,对我而言,它就像是一种魅力。
$block->addOrderToAvailableOrders('created_at', 'New')
在分拣器模板中使用内置的公共功能。
created_at
使用您的自定义价格属性代码更改属性代码
我们可以通过使用插件来实现。请在您的模块中创建以下文件。
app / code / Package / CustomToolbar / etc / di.xml
<type name="Magento\Catalog\Model\Config">
<plugin name="Package_CustomToolbar::addCustomOptions" type="Package\CustomToolbar\Plugin\Model\Config" />
</type>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
<plugin name="Package_CustomToolbar::addPriceDecendingFilterInToolbar" type="Package\CustomToolbar\Plugin\Product\ProductList\Toolbar" />
</type>
应用程序/代码/包/ CustomToolbar /插件/Model/Config.php
namespace Package\CustomToolbar\Plugin\Model;
use Magento\Store\Model\StoreManagerInterface;
class Config
{
protected $_storeManager;
public function __construct(
StoreManagerInterface $storeManager
) {
$this->_storeManager = $storeManager;
}
/**
* Adding custom options and changing labels
*
* @param \Magento\Catalog\Model\Config $catalogConfig
* @param [] $options
* @return []
*/
public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
{
$store = $this->_storeManager->getStore();
$currencySymbol = $store->getCurrentCurrency()->getCurrencySymbol();
//Remove specific default sorting options
unset($options['position']);
unset($options['name']);
unset($options['price']);
//Changing label
$customOption['position'] = __('Relevance');
//New sorting options
$customOption['price_desc'] = __($currencySymbol.' (High to Low)');
$customOption['price_asc'] = __($currencySymbol.' (Low to High)');
//Merge default sorting options with custom options
$options = array_merge($customOption, $options);
return $options;
}
}
应用程序/代码/包/ CustomToolbar /插件/产品/ProductList/Toolbar.php
namespace Package\CustomToolbar\Plugin\Product\ProductList;
class Toolbar
{
/**
* Plugin
*
* @param \Magento\Catalog\Block\Product\ProductList\Toolbar $subject
* @param \Closure $proceed
* @param \Magento\Framework\Data\Collection $collection
* @return \Magento\Catalog\Block\Product\ProductList\Toolbar
*/
public function aroundSetCollection(
\Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
\Closure $proceed,
$collection
) {
$currentOrder = $subject->getCurrentOrder();
$result = $proceed($collection);
if ($currentOrder) {
if ($currentOrder == 'price_desc') {
$subject->getCollection()->setOrder('price', 'desc');
} elseif ($currentOrder == 'price_asc') {
$subject->getCollection()->setOrder('price', 'asc');
}
}
return $result;
}
}
这对我来说很好,无需重写任何Magento类。
如果只想使用“ 创建于”属性,则可以在管理面板的排序选项中激活此属性。
例:
<?php
namespace Vendor\Module\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
class UpgradeData implements UpgradeDataInterface
{
protected $eavSetupFactory;
/**
* UpgradeData constructor.
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(
EavSetupFactory $eavSetupFactory
) {
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*/
public function upgrade(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
if (version_compare($context->getVersion(), '2.1.1', '<')) {
try {
$entityType = $eavSetup->getEntityTypeId('catalog_product');
$label = 'Created At';
$eavSetup->updateAttribute($entityType, 'created_at', 'frontend_label', $label, null);
$eavSetup->updateAttribute($entityType, 'created_at', 'used_for_sort_by', 1, null);
} catch (LocalizedException $e) {
}
}
}
}
此代码来自Setup / UpgradeData.php,但最好改用InstallData.php。
步骤1:首先,您应该创建registration.php
供应商名称:Arun
模块名称:NewSorting
供应商/模块名称/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Arun_NewSorting',
__DIR__
);?>
步骤2:您创建module.xml
供应商/模块名称/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Arun_NewSorting" setup_version="0.0.1">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
步骤3:创建插件
供应商/模块名称/etc/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="Magento\Catalog\Model\Config">
<plugin name="Arun_NewSorting::addCustomOptions" type="Arun\NewSorting\Plugin\Model\Config" />
</type>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
<plugin name="Arun_NewSorting::addPriceDecendingFilterInToolbar" type="Arun\NewSorting\Plugin\Product\ProductList\Toolbar" />
</type>
</config>
步骤4:然后创建config.php
供应商/模块名称/插件/模型/Config.php
<?php
namespace Arun\NewSorting\Plugin\Model;
use Magento\Store\Model\StoreManagerInterface;
class Config {
protected $_storeManager;
public function __construct(
StoreManagerInterface $storeManager
) {
$this->_storeManager = $storeManager;
}
public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
{
$store = $this->_storeManager->getStore();
$currencySymbol = $store->getCurrentCurrency()->getCurrencySymbol();
// Remove specific default sorting options
$default_options = [];
$default_options['name'] = $options['name'];
unset($options['position']);
unset($options['name']);
unset($options['price']);
//Changing label
$customOption['position'] = __( 'Relevance' );
//New sorting options
$customOption['created_at'] = __( ' New' );
$customOption['name'] = $default_options['name'];
//Merge default sorting options with custom options
$options = array_merge($customOption, $options);
return $options;
}
}
第5步:覆盖Toolbar.php ***
供应商/模块名称/插件/产品/产品列表/Toolbar.php
<?php
namespace Arun\NewSorting\Plugin\Product\ProductList;
class Toolbar
{
public function aroundSetCollection(
\Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
\Closure $proceed,
$collection
) {
$currentOrder = $subject->getCurrentOrder();
$result = $proceed($collection);
if ($currentOrder) {
if ($currentOrder == 'created_at') {
$subject->getCollection()->setOrder('created_at', 'desc');
}
}
return $result;
}
}
完美地工作
created_at
在数据库表中找到产品属性eav_attribute
,将其列设置frontend_label
为Created At
(默认为null)。
created_at
在数据库表中找到产品属性catalog_eav_attribute
,将其列设置used_for_sort_by
为1
(默认为0)。
清理站点缓存,它正在工作。
# Get the attribute_id of 'created_at'
select attribute_id from eav_attribute where attribute_code = 'created_at' and entity_type_id=4;
# Set frontend_label
update eav_attribute set frontend_label = 'Created At' where attribute_id=112;
# Set used_for_sort_by
update catalog_eav_attribute set used_for_sort_by = 1 where attribute_id=112;
attribute_id
。
} elseif ( $this->getCurrentDirection() == 'asc' ) {
改为} else {
。