Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle multiple-currency and custom quote item price on Magento?

Tags:

php

magento

I have a magento store with two currency, and my items in cart have a dynamic price. I succesfully calculate my quote_item price, with an observer and setCustomPrice and setOriginalCustom price

 $quote_item->setCustomPrice($price);
 $quote_item->setOriginalCustomPrice($price);

And my observer:

<sales_quote_add_item>

But i have a problem, when i change the currency of my store, the subtotal is not update. How to handle multiple-currency and custom quote item price ?

like image 884
Guillaume Alouege Avatar asked Nov 27 '22 09:11

Guillaume Alouege


2 Answers

handle it through observer

< sales_quote_item_set_product>

$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
if($currentCurrencyCode!=$baseCurrencyCode)
    $price= Mage::helper('directory')->currencyConvert($baseprice, $baseCurrencyCode, $currentCurrencyCode); 
else
    $price = $baseprice;

$item->setPrice($baseprice);
$item->setRowTotal($item->getQty() * $price);
like image 159
Imran Shakil Avatar answered Nov 28 '22 22:11

Imran Shakil


I've had this very same issue over the last week. Using the ->setOriginalCustomPrice method is fine for a single currency site, but with currency switching, its rigidness means you need to update the cart items and list price everytime the currency is switched, which is very inefficient in my opinion.

I came up with a more elegant solution. Create a module and within the model section of your config add this;

<models>
        <catalog>
            <rewrite>
                <product>PixieMedia_CustomPrice_Model_Product</product>
            </rewrite>
        </catalog>
</models>

Counter intuitively, the main ->getFinalPrice function is in the product model and not the price model.

Now create your new Product.php model in /app/code/local/Namespace/Module/Model/Product.php

class PixieMedia_CustomPrice_Model_Product extends Mage_Catalog_Model_Product {

public function getFinalPrice($qty=null)
// REWRITTEN FUNCTION TO RETURN THE SPECIAL PRICE AND ENSURE CURRENCY CONVERSION
{
    $qBreak = false;
    $customPrice = Mage::Helper('pixiemedia_customprice')->getCustomerPrice($this);

    if($qty) { 
        $qBreak = $this->getQtyBreakPrice($this->getSku(),$qty,$customPrice[0]); 
        }

    if($qBreak) { return $qBreak; } else { return $customPrice[0]; }

}

}

On the particular project I was working on, the client is using multiple price lists for customer specific pricing, the scope of which would make Magento horrendously slow to index pricing. So we've loaded all the data to a custom table and perform a lookup to return the customer's correct price or qty break.

It's dead easy to hook your own logic in and return what ever price you wish. This fully supports currency conversion, so no need to fiddle around re-converting prices.

Hope this helps someone out. Enjoy :)

like image 41
PixieMedia Avatar answered Nov 28 '22 23:11

PixieMedia