Skip to content
Snippets Groups Projects
awocoupon.php 3.43 KiB
<?php

defined ('_JEXEC') or die('Restricted access');

/**
 * AwoCoupon variables for VM Shipping by Rules
 *
 * @subpackage Plugins - VmShipmentRules
 * @copyright Copyright (C) 2015 Reinhold Kainhofer, office@open-tools.net
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
 * VirtueMart is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
 *
 * http://open-tools.net/
 *
 */
if (!class_exists ('VmPlugin')) {
	require(JPATH_VM_PLUGINS . DS . 'vmplugin.php');
}

/** Extension plugin for the "Shipping by Rules" shipping plugin for VirtueMart
 */
class plgVmShipmentRulesAwoCoupon extends VmPlugin {
	/**  Trigger to add variables to the cart values
	  *  You can add new variables to the $cartvals array or modify existing ones. They will be directly 
	  *  available in all rules.
	  *  Please notice that this function might also be called for only a subset of products of the cart.
	  */
	function onVmShippingRulesGetCartValues(&$cartvals, $cart, $products, $method) {
		// First, make sure coupons are processes (VM core processes third-party coupons AFTER the shipping)
		// This does NOT update the cart_prices array, but at least it makes sure Awocoupon has processed the coupon
		JPluginHelper::importPlugin('vmcoupon');
		if (!empty($cart->couponCode)) {
			$dispatcher = JDispatcher::getInstance();
			// TODO: This does not yet work...
// 			$returnValues = $dispatcher->trigger('plgVmCouponHandler', array($cart->couponCode, &$cart->cartData, $cart->cartPrices));
		}

		$coupon_discount = 0;
		$session = JFactory::getSession();
		$coupon_row = $session->get('coupon', '', 'awocoupon');
		
		$cartvals['awoproductdiscount'] = 0.0;
		$cartvals['awoproductdiscountnotax'] = 0.0;
// 		$cartvals['awoproductdiscounttax'] = 0.0;
		$cartvals['awoshippingdiscount'] = 0.0;
		$cartvals['awoshippingdiscountnotax'] = 0.0;
// 		$cartvals['awoshippingdiscounttax'] = 0.0;
		$cartvals['awodiscount'] = 0.0;
		if(!empty($coupon_row)) {
			$coupon_row = unserialize($coupon_row);
// JFactory::getApplication()->enqueueMessage("<pre>AwoCoupon row: ".print_r($coupon_row,1)."</pre>", 'error');

			$cartvals['awoproductdiscount']       = $coupon_row['product_discount'];
			$cartvals['awoproductdiscountnotax']  = $coupon_row['product_discount_notax'];
// 			$cartvals['awoproductdiscounttax']    = $coupon_row['product_discount_tax'];
			$cartvals['awoshippingdiscount']      = $coupon_row['shipping_discount'];
			$cartvals['awoshippingdiscountnotax'] = $coupon_row['shipping_discount_notax'];
// 			$cartvals['awoshippingdiscounttax']   = $coupon_row['shipping_discount_tax'];
 			$cartvals['awodiscount']              = $coupon_row['product_discount'] + $coupon_row['shipping_discount'];
		}
		$cartvals['amountaftercoupon'] = $cartvals['amount'] - $cartvals['awoproductdiscount'];
		$cartvals['amountwithtaxaftercoupon'] = $cartvals['amountaftercoupon'];
		$cartvals['taxamountaftercoupon'] = $cartvals['taxamount'] - ($cartvals['awoproductdiscount'] - $cartvals['awoproductdiscountnotax']);
		$cartvals['amountwithouttaxaftercoupon'] = $cartvals['amountaftercoupon'] - $cartvals['taxamountaftercoupon'];
		
		// TODO: Apply shipping coupon discounts
    }
}
  

// No closing tag