News:

Looking for documentation? Take a look on our wiki

Main Menu

[CUSTOM PLUGIN] Add dynamic discount to cart

Started by Il_maca, June 21, 2016, 11:13:16 AM

Previous topic - Next topic

Il_maca

Hi to all!
I'm building a new multivendor platform with Virtuemart and Joomla.
I want add a certain dynamic discount... because the discount is based on current shopper AltaUserPoint and Total Cart.
I was thinking about using the coupon... but managing is very difficult and dangerous...
So, I've made a custom calculation plugin, with this function:

public function plgVmOnQuantityProductUpdate(&$cart) {
$discountvalue = 0;
$user = JFactory::getUser();
if (!$user->guest) {
$profil = AltaUserPointsHelper::getUserInfo ( '', $user->id );
$AvailablePoints = $profil->points;
$totalBill = round($cart->cartPrices['salesPrice'], 2);
if($AvailablePoints > $totalBill) $discountvalue = $totalBill;
else $discountvalue = $AvailablePoints;


echo "Punti disponibili: " . $AvailablePoints . "<br>";
echo "Totale Carrello: " . $totalBill . "<br>";
echo "Valore Coupon: " . $discountvalue . "<br>";

foreach( $cart->cartData['DBTaxRulesBill'] as &$calculation){
if($calculation['calc_name'] == "SynthCoins" ){
$calculation['calc_value'] = $discountvalue;
}
echo "<br><br>";
}
print_r($cart);
//$cart->prepareCartData();
//$cart->setCartIntoSession();

}


        return true;
    }


So, I can see the row with correct discount, but the total price doesn't affected by new discount value. I tried to insert $cart->prepareCartData(), but my custom discount is replaced by the fixed value setted in admin.
I need of a trigger like 'plgVmonCalculationApplied' or similar... it exist?


Il_maca

Ok, after a search on virtuemart code, i've found: plgVmInGatherEffectRulesBill...
so, my new code is:

public function plgVmInGatherEffectRulesBill(&$calculationHelper,&$rules){
$discountvalue = 0;
$user = JFactory::getUser();
$cart = VirtueMartCart::getCart(false);
if (!$user->guest) {
$profil = AltaUserPointsHelper::getUserInfo ( '', $user->id );
$AvailablePoints = $profil->points;
$totalBill = $cart->cartPrices['salesPrice'];
if($AvailablePoints > $totalBill) $discountvalue = $totalBill;
else $discountvalue = $AvailablePoints;

if($calculationHelper->inCart){
foreach($rules as &$rule){//$rules as $k=>$rule
if($rule['calc_name'] == "SynthCoins"){
$rule['calc_value'] = $discountvalue;
}

}
}
}
}
[code]

But i have a little issue:
On certain totals, i get an "nan" error... maybe it is a round issue.  How can i resolve?
I shouldn't use cartPrices['salesPrice'], right?

Il_maca

Ok, solved... there was a shipment calculation error.

Dragoner

Hello, please where can i fonud this plugin?

Thanks, David