1

I've integrated a function that generates a custom feed for Criteo.

All works fine except the product price, i can get the product price and the special price, but i can't get the final price (selling price that depends if the special price is active or if there are catalog rules applied on the product)

This is my code

 <?php
define('SAVE_FEED_LOCATION','criteo.txt');
set_time_limit(1800);
require_once 'app/Mage.php';
Mage::app('default');
try{
    $handle = fopen(SAVE_FEED_LOCATION, 'w');
    $heading = array('id','name','producturl','bigimage','smallimage','description','price','retailprice','stock');
    $feed_line=implode("|", $heading)."\r\n";
    fwrite($handle, $feed_line);

    $products = Mage::getModel('catalog/product')->getCollection();
    $products->addAttributeToFilter('status', 1);
    $products->addAttributeToFilter('visibility', 4);
    $products->addAttributeToSelect('*');
    $prodIds=$products->getAllIds();

    $product = Mage::getModel('catalog/product');

    $counter_test = 0;

    foreach($prodIds as $productId) {

        if (++$counter_test < 30000){

            $product->load($productId);

            $product_data = array();
            $product_data['sku'] = $product->getSku();

            $title_temp = $product->getName();
            if (strlen($title_temp) > 70){
                $title_temp = str_replace("Supply", "", $title_temp);
                $title_temp = str_replace("  ", " ", $title_temp);
            }
            $product_data['title'] = $title_temp;

            $product_data['Deeplink'] = "http://www.test.com/".$product->getUrlPath(); 
            $product_data['image_big'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getBigImage();
            $product_data['image_small'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getSmallImage();
            $descriptionFull = $product->getShortDescription();
            preg_match('/<h2>(.*?)<\/h2>/', $descriptionFull, $matches);
            $descriptionHtml = strip_tags($matches[1]);
            $product_data['description'] = substr(iconv("UTF-8","UTF-8//IGNORE",$descriptionHtml), 0, 500);

            //******************************************
            //***********PRODUCT PRICE *****************
            //******************************************

            // Selling price
            $product_data['price'] = number_format((float)$product->getFinalPrice(), 2, '.', '');

            // Original price
            $product_data['retailprice'] = number_format((float)$product->getPrice(), 2, '.', '');


            //******************************************
            //*********** /PRODUCT PRICE *****************
            //******************************************

            //
            if($product->getIsInStock())
                {
                    //in stock!
                    $product_data['stock'] = "true";
                }
                else
                {
                    //not in stock!
                    $product_data['stock'] = "false";
                }

            foreach($product_data as $k=>$val){
                $bad=array('"',"\r\n","\n","\r","\t");
                $good=array(""," "," "," ","");
                $product_data[$k] = str_replace($bad,$good,$val);
            }

            echo $product_prezzo  . " prezzo ";

            $feed_line = implode("|", $product_data)."\r\n";
            fwrite($handle, $feed_line);
            fflush($handle);

        }

    }

    fclose($handle);
}
catch(Exception $e){
    die($e->getMessage());
}

GetFinalPrice() doesn't work, it gives me the price of the first product (even if special) for all product... example:

final price product 1 = 19,00 (ok, and if there is a special price gives me special price, but no rules are applyed!!)
final price product 2 = 19,00 (wrong is the price of product 1!)
final price product 3 = 19,00 (wrong is the price of product 1!)
final price product ... = 19,00 (wrong is the price of product 1!)

I've tried this solution but doesn't work (no price is given)

$product_data['price'] = Mage::getModel('catalogrule/rule')->calcProductPriceRule($product, $product->getFinalPrice());

Thanks a lot!

0

you should have to follow this function which will be very helpful to you

 protected function _getProductPrice($objProduct){

        $price = '';
        $classId = $objProduct->getTaxClassId();

        $catalogRulePrice = "";
        $taxprice = Mage::helper('tax')->getPrice($objProduct, $objProduct->getFinalPrice());
        $catalogRulePrice = Mage::getModel('catalogrule/rule')->calcProductPriceRule($objProduct, $taxprice);
        // Added logc to consider speacial price in feed if it is available        
        if ($objProduct->getSpecialPrice()) {
            $today = mktime(0, 0, 0, date('m'), date('d'), date('y'));
            $todaytimestamp = strtotime(date('Y-m-d 00:00:00', $today));
            $spcl_pri_time = strtotime($objProduct->getSpecialToDate());
            if ($spcl_pri_time <= $todaytimestamp) {
                $_price = number_format($objProduct->getSpecialPrice(), 2, ".", "");
            } else {
                $_price = number_format($objProduct->getPrice(), 2, ".", "");
            }


        } else if ($catalogRulePrice) {
            $price = number_format($catalogRulePrice, 2, ".", "");            
        } else {
            $_price = number_format($objProduct->getPrice(), 2, ".", "");

        }

        return $price;

    }

And if you need to apply vat in price then you have to add in $price as well.

hope this will work for you

  • Thanks, i've tried your function, works well for special privce but doesn't give me the final price width catalog rule... i miss something? – Marco Nov 19 '14 at 15:39
  • Hi Liyakat, I found the problem, my rules are defined in the store view so I use Mage::getModel('catalogrule/rule')->calcProductPriceRule($product->setStoreId('MY_STORE_ID'), $taxprice); Thanks! – Marco Nov 19 '14 at 17:03
  • HI Liyakat, I've a problem with your code: when i have a rule applied to a product that have a special price magento choose the lower price, but with this code i've always the special price can you please give me an help? – Marco Nov 21 '14 at 12:01
  • you can compare both price and take lowest one to return from function – liyakat Nov 21 '14 at 12:19
0

Ah...I think I answered this kind of question at least 3 times before:
- https://magento.stackexchange.com/a/10043/146,
- https://magento.stackexchange.com/a/25165/146
- https://magento.stackexchange.com/a/35242/146 This one is actually related to the final price

The problem is that final_price is not a product attribute and is not loaded from the db. See more details about how it works on the last link from above.
You need to change the way you retrieve your collection and lose the load from inside the loop.

 $collection = Mage::getModel('catalog/product')->getCollection();
 $collection
        ->addAttributeToSelect('*') //or any other attributes you need.
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents();
 Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
 Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

foreach ($collection as $product) {
    $product_data = array();
    $product_data['sku'] = $product->getSku();
    .....
}
  • Thanks, I understand what you say, i've tried semplifiing my code to insert your but doesnt work, it seem that the foreach ($collection as $product) {..} is empty if I use only $collection->addAttributeToSelect('*'); It works but obviously doesn't give me the FinalPrice – Marco Nov 19 '14 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.