【问题标题】:How do I show the product SKU instead of it's name in the breadcrumb trail in Magento?如何在 Magento 的面包屑路径中显示产品 SKU 而不是其名称?
【发布时间】:2012-09-21 10:24:54
【问题描述】:

我试图在面包屑路径中显示产品 SKU,而不是产品名称。有没有人有任何想法如何做到这一点?谢谢!

【问题讨论】:

  • hmm 将名称变量 getName() 替换为 getSku() ?
  • 感谢您的回复!刚刚在 app\code\core\Mage\Catalog\Helper\Data.php 中尝试过(在 getBreadcrumbPath 函数中。它有效,但它也改变了页面标题。知道解决方法吗?
  • 在模板中更改而不是在帮助程序中
  • 你能详细说明我应该如何去做吗?面包屑模板文件对于所有页面都是通用的,无论如何,对于产品来说,这些页面都是在该控制器中设置的。我敢肯定这很明显,我只是想不对。谢谢!
  • 基本上你做对了。但是,我建议重写帮助程序而不是编辑核心文件。我会提交并回答说明。

标签: magento product breadcrumbs skus


【解决方案1】:

你可以使用插件来改变这个

Magento 在此处设置面包屑的产品名称 Magento\Catalog\ViewModel\Product\Breadcrumbs

public function getJsonConfigurationHtmlEscaped() : string
    {
        return json_encode(
            [
                'breadcrumbs' => [
                    'categoryUrlSuffix' => $this->escaper->escapeHtml($this->getCategoryUrlSuffix()),
                    'useCategoryPathInUrl' => (int)$this->isCategoryUsedInProductUrl(),
                    'product' => $this->escaper->escapeHtml($this->getProductName())
                ]
            ],
            JSON_HEX_TAG
        );
    }

您可以将插件添加到您的自定义模块。 在您的插件中添加一个函数来获取产品 sku 然后添加函数 afterGetJsonConfigurationHtmlEscaped 以更新 getJsonConfigurationHtmlEscaped 以使用 sku 而不是名称。

在GetJsonConfigurationHtmlEscaped($subject)之后

 /**
 * Returns product sku.
 *
 * @return string
 * Add this function to get your Sku
 */
public function getProductSku(): string
{
    return $this->catalogData->getProduct() !== null
        ? $this->catalogData->getProduct()->getSku()
        : '';
}

/**
 * Returns breadcrumb json with html escaped Sku
 *
 * @return string
 */
public function afterGetJsonConfigurationHtmlEscaped($subject) : string
{
    return json_encode(
        [
            'breadcrumbs' => [
                'categoryUrlSuffix' => $this->escaper->escapeHtml($subject->getCategoryUrlSuffix()),
                'useCategoryPathInUrl' => (int)$subject->isCategoryUsedInProductUrl(),
                'product' => $this->escaper->escapeHtml($this->getProductSku())
            ]
        ],
        JSON_HEX_TAG
    );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多