2017-08-10 15 views
1

我試圖做的是獲取名爲「特殊產品」的屬性,然後查看它的特定類型特殊產品; garminpilot,garminpilotpro,garminpilotpremiumgarmintrainer。然後,如果產品上存在這些文件,請將特定文本字符串分配給變量$ specialproductmsg從Magento獲取特定屬性,如果該屬性值包含特定數據,請將特定字符串分配給變量

這是我目前擁有的。有沒有更好的方法來做到這一點?更有效率的東西?

$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 = ''; 
} 
+0

不,不是我的知識。 –

回答

0

您可以使用三元運算符是這樣的:

$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.' 
    : '';