浏览文章

文章信息

后端 Magento2 生成自定义产品链接 16003


插件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\Product\Url">
        <plugin name="Mjd_Product_create_url" type="Mjd\Product\Plugin\ProductUrlCreatePlugin" sortOrder="1"/>
    </type>
</config>

Plugin插件代码

<?php
/**
 * DESC:
 * author:秋枫雁飞
 * website:https://bbs.aiweline.com
 * email: aiweline@qq.com
 */
namespace Mjd\Product\Plugin;
class ProductUrlCreatePlugin
{
    /**
     * DESC:
     * author:秋枫雁飞
     * website:https://bbs.aiweline.com
     * email: aiweline@qq.com
     *
     * 参数区:
     * @param \Magento\Catalog\Model\Product\Url $object
     * @param \Magento\Catalog\Model\Product $product
     * @param $result
     * @return string
     */
    function afterGetUrl(\Magento\Catalog\Model\Product\Url $object, $result, \Magento\Catalog\Model\Product $product)
    {
        $category_ids = implode(',', $product->getCategoryIds());
        $product_sku = urlencode($product->getSku());
        return $result . "?product_id={$product->getId()}&sku={$product_sku}&entity=product";
    }
}


原创