从 Magento 获取特定属性,如果该属性值包含特定数据,则将特定字符串分配给变量



我试图做的是获取名为">specialproduct"的属性,然后通过它查找特定类型的特殊产品;Garminpilot、GarminpilotPro、GarminpilotPremiumGarmintrainer然后,如果产品上存在这些文本字符串,请将特定的文本字符串分配给变量$specialproductmsg

这就是我目前拥有的。有没有更好的方法呢?还有什么更有效的吗?

$specialtype = $item->getProduct()->getAttributeText('specialproduct');
if(in_array($specialtype, array('garminpilot','garminpilotpro','garminpilotpremium','garmintrainer'))) {
$specialproductmsg = 'You have ordered an online course. Please visit http://example.com/onlinecourse to view your online course.';
}
else {
$specialproductmsg = '';
}

你可以像这样使用三元运算符:

$types = ['garminpilot', 'garminpilotpro', 'garminpilotpremium', 'garmintrainer'];
$specialproductmsg = in_array($specialtype, $types)
? 'You have ordered an online course. Please visit http://example.com/onlinecourse to view your online course.' 
: '';

最新更新