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

yanping
24 声望0 粉丝

引用和评论

0 条评论