Using OPC to get totals within modules
Posted: Wed Jun 11, 2014 4:11 pm
As respond to:
Need one more help. How can I get final value of cart on module after applying coupon ?
This code when used inside modules would print the current total:
Note: The code is not tested against typos
hello, if you'd like to use opc's functions to do the job, you can do this:
best regards, stan
Need one more help. How can I get final value of cart on module after applying coupon ?
This code when used inside modules would print the current total:
Note: The code is not tested against typos
hello, if you'd like to use opc's functions to do the job, you can do this:
- Code: Select all
//load VM:
if (!class_exists( 'VmConfig' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'config.php');
// load langues:
// only latest vm versions:
VmConfig::loadConfig();
// load your modules languages:
VmConfig::loadJLang('mod_virtuemart_cart', true);
//load cart:
if(!class_exists('VirtueMartCart')) require(JPATH_VM_SITE.DS.'helpers'.DS.'cart.php');
$cart = VirtueMartCart::getCart(false);
//load OPC:
require_once(JPATH_SITE.DS.'components'.DS.'com_onepage'.DS.'helpers'.DS.'loader.php');
// save any shipping or payment data:
$saved_id = $cart->virtuemart_shipmentmethod_id;
$saved_id2 = $cart->virtuemart_paymentmethod_id;
// make sure no API methods are called on each page (disables shipping or payment calculation):
$cart->virtuemart_shipmentmethod_id = $cart->virtuemart_paymentmethod_id = 0;
//irrelevant:
$vm2015 = false;
// using opc in this case will make sure that this code is compatible from vm2.0.0 to the very latest opc for vm2.9.x (incl. 2.6.x)
$prices = OPCloader::getCheckoutPrices( $cart, false, $vm2015, 'opc' );
// restore saved data:
$cart->virtuemart_shipmentmethod_id = $saved_id;
$cart->virtuemart_paymentmethod_id = $saved_id2;
// is a number/decimal:
$total = $prices['billTotal'];
// get proper currency :
$mainframe = Jfactory::getApplication();
$virtuemart_currency_id = $mainframe->getUserStateFromRequest( "virtuemart_currency_id", 'virtuemart_currency_id',JRequest::getInt('virtuemart_currency_id') );
// get currency display object:
if (!empty($virtuemart_currency_id))
$currencyDisplay = CurrencyDisplay::getInstance($virtuemart_currency_id);
else
{
$currencyDisplay = CurrencyDisplay::getInstance($cart->paymentCurrency);
$virtuemart_currency_id = $cart->paymentCurrency;
}
// convert if necessary:
$total = $currencyDisplay->convertCurrencyTo( $virtuemart_currency_id, $prices['billTotal'],false);
// get display:
$order_total_display = $currencyDisplay->createPriceDiv('billTotal','', $prices,false,false, 1);
// update your own class as needed:
$order_total_display = str_replace('class="', 'class="opc_', $order_total_display);
// display the total:
echo $order_total_display;
best regards, stan