Magento 2 可配置产品子产品URL刷新404重定向及URL修改求助
Magento 2 不可见简单产品重定向至父可配置产品解决方案
核心思路
针对不可见简单产品的请求,拦截并重定向到对应的父可配置产品页面,同时携带简单产品对应的配置选项参数,确保页面加载后自动预选对应选项。放弃事件钩子改用控制器插件实现,因为插件能更精准地干预请求流程,直接修改响应头(包括重定向URL)。
实现步骤
1. 创建插件拦截产品视图控制器
拦截Magento\Catalog\Controller\Product\View控制器的execute方法,在执行前判断当前请求的产品是否为不可见的简单产品且属于某个可配置产品的子产品。
<?php namespace YourVendor\YourModule\Plugin\Catalog\Controller\Product; use Magento\Catalog\Controller\Product\View as ProductViewController; use Magento\Catalog\Model\Product; use Magento\Catalog\Model\ProductRepository; use Magento\Framework\Controller\Result\RedirectFactory; use Magento\Framework\Exception\NoSuchEntityException; class RedirectInvisibleSimpleProduct { protected $productRepository; protected $redirectFactory; public function __construct( ProductRepository $productRepository, RedirectFactory $redirectFactory ) { $this->productRepository = $productRepository; $this->redirectFactory = $redirectFactory; } public function aroundExecute(ProductViewController $subject, \Closure $proceed) { $productId = $subject->getRequest()->getParam('id'); try { /** @var Product $product */ $product = $this->productRepository->getById($productId); } catch (NoSuchEntityException $e) { return $proceed(); } // 判断是否为不可见的简单产品,且属于可配置产品的子产品 if ($product->getTypeId() === 'simple' && !$product->isVisibleInSiteVisibility() && $product->getParentProductIds()) { $parentId = current($product->getParentProductIds()); try { /** @var Product $parentProduct */ $parentProduct = $this->productRepository->getById($parentId); if ($parentProduct->getTypeId() === 'configurable') { // 获取简单产品对应的配置选项参数 $configurableOptions = $this->getConfigurableOptions($parentProduct, $product); $redirectUrl = $parentProduct->getUrlModel()->getUrl($parentProduct, ['_query' => $configurableOptions]); // 执行301永久重定向(利于SEO) $redirect = $this->redirectFactory->create(); $redirect->setUrl($redirectUrl); return $redirect; } } catch (NoSuchEntityException $e) { // 父产品不存在时,继续原流程返回404 return $proceed(); } } return $proceed(); } /** * 获取简单产品对应的可配置选项参数(用于URL查询字符串) */ protected function getConfigurableOptions(Product $parentProduct, Product $simpleProduct) { $configurableOptions = []; $configurableType = $parentProduct->getTypeInstance(); $attributes = $configurableType->getConfigurableAttributes($parentProduct); foreach ($attributes as $attribute) { $attributeCode = $attribute->getProductAttribute()->getAttributeCode(); $optionValue = $simpleProduct->getData($attributeCode); $configurableOptions[$attributeCode] = $optionValue; } return $configurableOptions; } }
2. 注册插件
在etc/frontend/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\Controller\Product\View"> <plugin name="redirect_invisible_simple_product" type="YourVendor\YourModule\Plugin\Catalog\Controller\Product\RedirectInvisibleSimpleProduct" sortOrder="10"/> </type> </config>
3. 关键说明
- 插件替代事件的原因:事件钩子(如
controller_action_predispatch_catalog_product_view)虽能拦截请求,但修改重定向URL的灵活性不足。插件的aroundExecute方法可直接返回重定向结果,中断原控制器执行,确保URL被正确修改。 - SEO优化:使用301永久重定向,告知搜索引擎该简单产品的正确地址为父可配置产品页面,避免重复内容与404错误。
- 选项预选适配:通过
getConfigurableOptions获取的属性参数,可直接对接你已实现的预选逻辑,确保页面加载后自动选中对应选项。 - 异常容错:加入产品不存在的异常捕获,极端情况下不会破坏原有页面流程。
4. 测试验证
- 访问不可见简单产品URL,应自动重定向至父可配置产品页面,URL包含配置选项参数。
- 刷新页面后,页面正常加载且对应选项被预选。
- 检查浏览器网络请求,确认返回301状态码。
内容的提问来源于stack exchange,提问作者Andrea Mastracchio
相关产品推荐
相关产品推荐

