关于magento2:通过option-id拿到对应的option-label值

42次阅读

共计 689 个字符,预计需要花费 2 分钟才能阅读完成。

We can get hash code value of specific visual swatch by swatch option id.
We have created Color Product attribute from Stores -> Attributes -> Product.
Color attribute code with Catalog Input Type for Store Owner set to Visual swatch.

Now We can get the color hash code by color option id using below code snippet.

public function __construct(\Magento\Swatches\Helper\Data $swatchHelper,) {$this->swatchHelper = $swatchHelper;}
/* Get Hashcode of Visual swatch by option id */
public function getAtributeSwatchHashcode($optionid) {$hashcodeData = $this->swatchHelper->getSwatchesByOptionsId([$optionid]);
    return $hashcodeData[$optionid]['value'];
}

Call getAtributeSwatchHashcode() function from template,

$hashcode = $this->getAtributeSwatchHashcode(50); //assume 50 is Red option id

正文完
 0