phpstringmagentoyaml

Do not show string if value is equal or greater than another


It is necessary to hide the oldprice string if the value of the string is equal to or greater than the price string.

What is the correct syntax for a string oldprice?

 // Save product data into result array
     $result['products'][] = array(
     'id' => $_product->getId(),
     'in_stock' => (bool)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getIsInStock(),
     'url' => str_replace('/index.php', null, $result['shop_data']['url']) . $_product->getUrlKey() . $helper->getProductUrlSuffix(),
     'price' => $_product->getFinalPrice(),
     'oldprice' => $_product->getPrice(),
     'currencyId' => $currencyCode,
     'categoryId' => $_category->getId(),
     'picture' => $picUrl,
     'name' => $_product->getName(),
     'vendor' => trim($_product->getAttributeText('manufacturer')),
     'model' => $_product->getSku(),
     'description' => trim(strip_tags($_product->getShortDescription())),
     'local_delivery_cost' => $priceship[0],    
     'market_category' => trim($_product->getAttributeText('market_category')),
     'country_of_origin' => trim($_product->getAttributeText('country_of_manufacture')),
     'local_delivery_cost' => 500,
     'sales_notes' => '100% предоплата',
 );

Solution

  • You could do something like this:

     'oldprice' => ($_product->getPrice() >= $_product->getFinalPrice() ? 0 : $_product->getPrice())
    

    or just do the calculation beforehand

    $oldPrice = null;
    if ($_product->getPrice() >= $_product->getFinalPrice()) {
        $oldPrice = $_product->getPrice();
    }
    
    ...
    
    'oldprice' => $oldPrice
    

    However, this wouldn't stop a value being saved into your array (in this case 0), so you'd still need some logic where you're echoing it out.