Search code examples
phpmagento-1.9sku

Magento 1.9 if SKU Code Equals... code example not working - help :D


I am trying to check if sku first character equals then show a custom option value. here is the code

if (strpos($sku_code, 'F') == 0) { 
    $select->setValue($configValue); 
}

Using this code works in the desired effect that it changes the value of the custom option but unfortunately it changes the value for all sku's not just the ones starting with 'F'

Any thoughts? or examples of something I could try?

UPdate Thanks for the help.. I tried your solution but having same effect. Just ignoring the statement here is the code i am using.

if (strpos($sku_code, 'F') === 0) {
                    $select->setValue($configValue); 
                }
                else if ($_value->getTitle() == 'M') {
                    $select->setValue($_value->getOptionTypeId());  
                }
                else if ($_value->getTitle() == 'T') {
                    $select->setValue($_value->getOptionTypeId());  
                } 

Solution

  • strpos($sku_code, 'F')  
    

    will return false if the search string is not found. And in PHP, 0 is considered false. Therefore, the condition of yours will be evaluated to true! You can check the type of the return value too, like, if(strpos($sku_code, 'F') === 0) as this would also check the datatype.