VirtueMart Forum

VirtueMart 2 + 3 + 4 => Plugins: Payment, Shipment and others => Topic started by: Il_maca on June 21, 2016, 11:13:16 AM

Title: [CUSTOM PLUGIN] Add dynamic discount to cart
Post by: Il_maca on June 21, 2016, 11:13:16 AM
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?

Title: Re: [CUSTOM PLUGIN] Add dynamic discount to cart
Post by: Il_maca on June 21, 2016, 11:49:16 AM
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?
Title: Re: [CUSTOM PLUGIN] Add dynamic discount to cart
Post by: Il_maca on June 21, 2016, 11:57:26 AM
Ok, solved... there was a shipment calculation error.
Title: Re: [CUSTOM PLUGIN] Add dynamic discount to cart
Post by: Dragoner on September 29, 2016, 13:26:51 PM
Hello, please where can i fonud this plugin?

Thanks, David