在 magento2 中,默认价格范围(从价格和到价格)显示在产品列表页面上的组产品。在产品详细信息页面上,当我们单击“自定义并添加到购物车”按钮时,然后在自定义部分下显示预配置的捆绑产品价格。此捆绑包价格基于管理员中所选默认选项的价格。
我们不想在产品列表页面上显示价格范围。我们只想在列表页面上显示与在产品详细信息页面上显示的相同的预配置价格。
我们如何在列表页面上显示分组产品的预配置价格,例如产品详细信息页面上显示的预配置价格?
在 magento2 中,默认价格范围(从价格和到价格)显示在产品列表页面上的组产品。在产品详细信息页面上,当我们单击“自定义并添加到购物车”按钮时,然后在自定义部分下显示预配置的捆绑产品价格。此捆绑包价格基于管理员中所选默认选项的价格。
我们不想在产品列表页面上显示价格范围。我们只想在列表页面上显示与在产品详细信息页面上显示的相同的预配置价格。
我们如何在列表页面上显示分组产品的预配置价格,例如产品详细信息页面上显示的预配置价格?
创建前端观察者:app/code/Vendor/Module/etc/frontend/events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="catalog_product_collection_load_after">
<observer name="catalog_category_set_bundle_product_price" instance="Vendor\Module\Observer\Product\SetBundleProductPriceCollection"/>
</event>
</config>
观察者:app/code/Vendor/Module/Observer/Product/SetBundleProductPriceCollection.php
<?php
namespace Vendor\Module\Observer\Product;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Bundle\Ui\DataProvider\Product\Listing\Collector\BundlePrice;
use Magento\Framework\App\Request\Http;
/**
* Class SetBundleProductPriceCollection
*/
class SetBundleProductPriceCollection implements ObserverInterface
{
/**
* Identifier of PLP Controller action
*/
const PLP_MODULE_CONTROLLER_ACTION = 'catalog_category_view_catalog';
/**
* @var Http
*/
private $request;
/**
* @param Http $request
*/
public function __construct(
Http $request
) {
$this->request = $request;
}
/**
* Execute
*
* @param Observer $observer Observer
* @return void
*/
public function execute(Observer $observer)
{
$moduleName = $this->request->getModuleName();
$controller = $this->request->getControllerName();
$action = $this->request->getActionName();
$route = $this->request->getRouteName();
$routerAction = $moduleName.'_'.$controller.'_'.$action.'_'.$route;
$collection = $observer->getCollection();
if ($routerAction == self::PLP_MODULE_CONTROLLER_ACTION) {
foreach ($collection as $product) {
if ($product->getTypeId() === BundlePrice::PRODUCT_TYPE) {
$bundleObj = $product->getPriceInfo()->getPrice('final_price');
$product->setPrice($bundleObj->getMinimalPrice()->getValue());
}
}
}
}
}