VirtueMart Forum

VirtueMart 2 + 3 + 4 => Product pricing => Topic started by: Peter Pillen on August 05, 2014, 00:31:17 AM

Title: Triggering a discount for related products
Post by: Peter Pillen on August 05, 2014, 00:31:17 AM
Using joomla 2.5 and VM 2.6.6 for VM members

Goal
I would like VM to automatically give a discount when a customer puts two products in his basket that are set as "related" products in the products database. To explain ... the shop sells scarves and bonnets separatly, but if the customer buys them as a pair, a discount should be applied. I'm adapting my template overrides in such way that when product A is in the basket, the related product B gets highlighted and shown with a discounted price (category and detail view).

I would temporarly code this to be triggered from the template override in the productdetailsview, but the problem is that I can't seem to figure out how I can trigger the re-calculation for the discounted price for the cart. I have no problem to code the template in such way that it displays a highlighted discount, but how can it be sent to the cart? Which function do I use? It must be somewhere in the vmplugin.php.

My favorite solution would be ... if products are related ... apply calculation rule with id X from VM backend. I can imagine that a 10-line code calculation plugin could solve this.

Maybe someone knows another way to approach this problem, but making a new category with discount is no good.
Title: Re: Triggering a discount for related products
Post by: balai on August 05, 2014, 09:58:55 AM
Check product bundles
http://breakdesigns.net/extensions/product-bundles

This is exactly what this plugin does.
It is not mandatory to display your bundles. The discount will be applied any way you add the products to the cart.
Title: Re: Triggering a discount for related products
Post by: Peter Pillen on August 05, 2014, 17:24:05 PM
Okay balai ... probably it could do the trick. But I have a whole folder with "suitable" plugins that didn't work as it should. I'm not keen on spending money without the possibility of testing it (I know that's not easy for developpers) It also - in my case - to learn and understand how to create and code custom plugins.
Title: Re: Triggering a discount for related products
Post by: Peter Pillen on August 07, 2014, 14:22:36 PM
I have noticed that placing a couponcode in the cart with the code below works perfectly, but it is not possible to alter the prices of the products in the cart?

<?php
                $Cart 
VirtueMartCart::getCart();
.
$Cart->couponCode='oneortheothercouponcode';
$Cart->couponDescr='10%';
$Cart->_triesValidateCoupon=array('oneortheothercouponcode');

                
$Cart->setCartIntoSession();
?>



If I alter the prices of the products in the cart and the cart total in this way, it always gets re-calculated in cart view. I prefer to code this in the template rather than changing calculationh.php. Any ideas on how to apply a discount from productdetailview (or cartview)?
Title: Re: Triggering a discount for related products
Post by: seyi on August 07, 2014, 15:15:57 PM
Better to apply it from a plugin.  Here is one I wrote, can modify it to work as you need:
http://awodev.com/products#plg_awocoupontrigger

Title: Re: Triggering a discount for related products
Post by: Peter Pillen on August 08, 2014, 17:17:17 PM
@seyi ... thanks for your post. Got me in the right direction. I installed the awocoupontrigger and changed the function in the awocoupontrigger.php file like below (it works). Maybe the code is of use to you or someone else.

<?php
public function 
plgVmgetPaymentCurrency($virtuemart_paymentmethod_id, &$paymentCurrency){
static $plgVmgetPaymentCurrency_awocoupontrigger_called;

if($plgVmgetPaymentCurrency_awocoupontrigger_called==1) return;
$plgVmgetPaymentCurrency_awocoupontrigger_called 1;

$mainframe JFactory::getApplication();
$coupon_code $this->params->get('couponcode_1','');
if(empty($coupon_code)) return;

//start Peter Pillen
if(!class_exists('VirtueMartCart')) require JPATH_ROOT.'/components/com_virtuemart/helpers/cart.php';
$Cart VirtueMartCart::getCart();
$ProductsInCart=array();
$NumberOfProductsInCart=count($Cart->products);
$RelatedProductsInCart=array();
if($NumberOfProductsInCart>0){
foreach($Cart->products as $CartProductid){
$db JFactory::getDbo();
$db->setQuery('SELECT `custom_value` 
FROM `#__virtuemart_product_customfields` 
WHERE `virtuemart_product_id` = '
.$CartProductid->virtuemart_product_id.' AND `virtuemart_custom_id` = 1');
$RelatedToProduct$db->loadColumn();
foreach($RelatedToProduct as $RelatedToProductid){
$ProductsInCart[]=$CartProductid->virtuemart_product_id.'/'.$RelatedToProductid;
if(in_array($RelatedToProductid.'/'.$CartProductid->virtuemart_product_id,$ProductsInCart)){
$RelatedProductsInCart[]=$RelatedToProductid;
$RelatedProductsInCart[]=$CartProductid;
}
}
}
}
if(count($RelatedProductsInCart)>=2){
$Cart->setCouponCode($coupon_code);
$Cart->getCartPrices();
}else{
$Cart->couponCode='';
$Cart->couponDescr='';
$Cart->_triesValidateCoupon=array();
$Cart->setCartIntoSession();
$Cart->getCartPrices();
}
//end Peter Pillen
return null;

}
?>