我试图在痕迹导航跟踪中显示产品 SKU 而不是产品名称。有人对如何做到这一点有任何想法吗?谢谢!
您可以使用插件来更改此设置
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然后在 GetJsonConfigurationHtmlEscaped 之后添加函数以更新 getJsonConfigurationHtmlEscaped 以使用 sku 而不是名称。
afterGetJsonConfigurationHtmlEscaped($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
);
}