我想隐藏一个类别的缺货产品,同时显示其他类别的缺货产品。请建议我该怎么做
我尝试打开“隐藏缺货”,但它对所有类别都隐藏。请建议我任何方法
创建自定义模块
<?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\ResourceModel\Product\Collection">
<plugin name="custom_out_of_stock_filter_plugin" type="Vendor\Module\Plugin\ProductCollectionPlugin"/>
</type>
</config>
创建插件文件
<?php
namespace Vendor\Module\Plugin;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogInventory\Model\Stock\Status;
use Magento\Framework\App\RequestInterface;
class ProductCollectionPlugin
{
protected $request;
protected $stockStatus;
public function __construct(
RequestInterface $request,
Status $stockStatus
) {
$this->request = $request;
$this->stockStatus = $stockStatus;
}
public function beforeLoad(Collection $subject)
{
$categoryId = $this->request->getParam('id'); // Get the current category ID
if ($categoryId == 'SPECIFIC_CATEGORY_ID') { // Replace with your specific category ID
// Apply filter to hide out-of-stock products for this category
$subject->getSelect()->join(
['stock_status' => $subject->getTable('cataloginventory_stock_status')],
'e.entity_id = stock_status.product_id',
[]
)->where('stock_status.stock_status = ?', 1);
}
return $subject;
}
}