Commit 9b9188a4 authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

VMCoupon_V1.0: Add helper plugin to provide VM coupon discounts in the...

VMCoupon_V1.0: Add helper plugin to provide VM coupon discounts in the variables SalesPriceAfterCoupon and CouponValue
parent 32bcdc11
BASE=vmcouponprice
PLUGINTYPE=vmshipmentrules
ZIPBASE=opentools_vmshipmentrules
VERSION=1.0
PLUGINFILES=$(BASE).php $(BASE).script.php $(BASE).xml index.html
TRANSLATIONS=$(call wildcard,language/en*/*.plg_$(PLUGINTYPE)_$(BASE).*ini)
INDEXFILES=language/index.html $(call wildcard,language/**/index.html)
ZIPFILE=plg_$(ZIPBASE)_$(BASE)_v$(VERSION).zip
all: zip
zip: $(PLUGINFILES) $(TRANSLATIONS) $(ADVANCEDFILES) $(INDEXFILES)
@echo "Packing all files into distribution file $(ZIPFILE):"
@zip -r $(ZIPFILE) $(PLUGINFILES) $(TRANSLATIONS) $(INDEXFILES)
clean:
rm -f $(ZIPFILE)
; VM Rule-based Shipping plugin: Extension plugin to provide coupon-related variables
; Copyright (C) 2017 Reinhold Kainhofer, Open Tools. All rights reserved.
; License http://www.gnu.org/licenses/gpl.html GNU/GPL
; Note : All ini files need to be saved as UTF-8 - No BOM
VMSHIPMENTRULES_VMCOUPONPRICE="VM Shipping by Rules - Coupon handler"
VMSHIPMENTRULES_VMCOUPONPRICE_DESC="<p>This plugin extends the shipping by rules plugin with two variables (couponValue and AmountAfterCoupon / salesPriceAfterCoupon) that take into account coupon discounts by the stock virtuemart coupon system.</p><p>By default, VirtueMart evaluates shipping costs first and coupons afterwards, so when shipping is calculated, the coupon discount is not yet calculated in general. To work around this issue, this small plugin adds to trickery to manually calculate the coupon during shipping cost evaluation. As this might interfere with VM (we have not yet run into issues, but who knows...), this functionality is not included by default, but only with this small extension plugin.</p>"
; VM Rule-based Shipping plugin: Extension plugin to provide coupon-related variables
; Copyright (C) 2017 Reinhold Kainhofer, Open Tools. All rights reserved.
; License http://www.gnu.org/licenses/gpl.html GNU/GPL
; Note : All ini files need to be saved as UTF-8 - No BOM
VMSHIPMENTRULES_VMCOUPONPRICE="VM Shipping by Rules - Coupon handler"
VMSHIPMENTRULES_VMCOUPONPRICE_DESC="<p>This plugin extends the shipping by rules plugin with two variables (couponValue and AmountAfterCoupon / salesPriceAfterCoupon) that take into account coupon discounts by the stock virtuemart coupon system.</p><p>By default, VirtueMart evaluates shipping costs first and coupons afterwards, so when shipping is calculated, the coupon discount is not yet calculated in general. To work around this issue, this small plugin adds to trickery to manually calculate the coupon during shipping cost evaluation. As this might interfere with VM (we have not yet run into issues, but who knows...), this functionality is not included by default, but only with this small extension plugin.</p>"
<!DOCTYPE html><title></title>
<?php
defined ('_JEXEC') or die('Restricted access');
/**
* Plugin providing Custom variables for VM Shipping by Rules
*
* @subpackage Plugins - VmShipmentRules
* @copyright Copyright (C) 2017 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.
*
* 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 plgVmShipmentRulesVMCouponPrice 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.
* This trigger will be first called right before any rule is evaluated. In that case, $products
* will contain all products in the cart and $cart_prices will be an arrow containing the calculated
* prices of the order.
* Please notice that this function might also be called for only a subset of products of the cart
* when the plugin evaluates a scoping function like evaluate_for_categories(...).
*/
function onVmShippingRulesGetCartValues(&$cartvals, $cart, $products, $method) {
// The coupon code is in general not yet set (will be set by the cart controller AFTER shipping is handled!
// -> set the coupon code now from the coupon_code REQUEST variable.
// If the coupon is not changed, the coupon variables are already properly set in the cartPrices
$coupon_code = trim(vRequest::getString('coupon_code', ''));
if(!empty($coupon_code)){
$this->couponHandler(/*$cart->couponCode*/$coupon_code, $cart);
}
$cartvals['salespriceaftercoupon']= $cart->cartPrices['salesPrice'] + (isset($cart->cartPrices['salesPriceCoupon'])?$cart->cartPrices['salesPriceCoupon']:0);
$cartvals['amountaftercoupon']= $cart->cartPrices['salesPrice'] + (isset($cart->cartPrices['salesPriceCoupon'])?$cart->cartPrices['salesPriceCoupon']:0);
$cartvals['couponvalue'] = isset($cart->cartPrices['couponValue'])?$cart->cartPrices['couponValue']:0;
}
# As the coupon handler in calculationh.php is protected, we need to
# duplicate all code here instead of calling the VM coupon handler directly....
protected function couponHandler($_code, &$cart) {
JPluginHelper::importPlugin('vmcoupon');
$dispatcher = JDispatcher::getInstance();
$returnValues = $dispatcher->trigger('plgVmCouponHandler', array($cart->couponCode, &$cart->cartData, &$cart->cartPrices));
if(!empty($returnValues)){
foreach ($returnValues as $returnValue) {
if ($returnValue !== null ) {
return $returnValue;
}
}
}
if (!class_exists('CouponHelper'))
require(VMPATH_SITE . DS . 'helpers' . DS . 'coupon.php');
if (!($_data = CouponHelper::getCouponDetails($_code))) {
return; // TODO give some error here
}
$_value_is_total = ($_data->percent_or_total == 'total');
$cart->cartPrices['salesPriceCoupon'] = ($_value_is_total ? $_data->coupon_value * -1 : ($cart->cartPrices['salesPrice'] * ($_data->coupon_value / 100)) * -1);
$cart->cartPrices['couponTax'] = 0;
$cart->cartPrices['couponValue'] = $cart->cartPrices['salesPriceCoupon'] - $cart->cartPrices['couponTax'];
}
}
// No closing tag
<?php
defined('_JEXEC') or die('Restricted access');
/**
* Installation script for the plugin
*
* @copyright Copyright (C) 2014 Reinhold Kainhofer, office@open-tools.net
* @license GPL v3+, http://www.gnu.org/copyleft/gpl.html
*/
// Adjust the class name. It has to be of the form:
// plgVmShipmentVMCouponPriceInstallerScript
class plgVmShipmentRulesVMCouponPriceInstallerScript
{
/**
* Constructor
*
* @param JAdapterInstance $adapter The object responsible for running this script
*/
// public function __constructor(JAdapterInstance $adapter);
/**
* Called before any type of action
*
* @param string $route Which action is happening (install|uninstall|discover_install)
* @param JAdapterInstance $adapter The object responsible for running this script
*
* @return boolean True on success
*/
// public function preflight($route, JAdapterInstance $adapter);
/**
* Called after any type of action
*
* @param string $route Which action is happening (install|uninstall|discover_install)
* @param JAdapterInstance $adapter The object responsible for running this script
*
* @return boolean True on success
*/
// public function postflight($route, JAdapterInstance $adapter);
/**
* Called on installation
*
* @param JAdapterInstance $adapter The object responsible for running this script
*
* @return boolean True on success
*/
public function install(JAdapterInstance $adapter)
{
// enabling plugin upon installation
$db = JFactory::getDBO();
$db->setQuery('update #__extensions set enabled = 1 where type = "plugin" and element = "vmcouponprice" and folder = "vmshipmentrules"');
$db->query();
return True;
}
/**
* Called on update
*
* @param JAdapterInstance $adapter The object responsible for running this script
*
* @return boolean True on success
*/
// public function update(JAdapterInstance $adapter)
// {
// jimport( 'joomla.filesystem.file' );
// $file = JPATH_ROOT . DS . "administrator" . DS . "language" . DS . "en-GB" . DS . "en-GB.plg_vmshipmentrules_vmcouponprice.sys.ini";
// if (JFile::exists($file)) JFile::delete($file);
// $file = JPATH_ROOT . DS . "administrator" . DS . "language" . DS . "de-DE" . DS . "de-DE.plg_vmshipmentrules_vmcouponprice.sys.ini";
// if (JFile::exists($file)) JFile::delete($file);
// return true;
// }
/**
* Called on uninstallation
*
* @param JAdapterInstance $adapter The object responsible for running this script
*/
// public function uninstall(JAdapterInstance $adapter)
// {
// // Remove plugin table
// $db =& JFactory::getDBO();
// $db->setQuery('DROP TABLE IF EXISTS `#__virtuemart_vmshipmentrulesles_vmcouponprice`;');
// $db->query();
// }
}
<?xml version="1.0" encoding="UTF-8" ?>
<extension version="2.5" type="plugin" group="vmshipmentrules" method="upgrade">
<name>VM Shipping by Rules - Coupon handler</name>
<creationDate>2017-01-04</creationDate>
<author>Reinhold Kainhofer, Open Tools</author>
<authorUrl>http://www.open-tools.net</authorUrl>
<copyright>Copyright (C) 2017, Reinhold Kainhofer</copyright>
<license>GPL v3+</license>
<version>1.0</version>
<description>VMSHIPMENTRULES_VMCOUPONPRICE_DESC</description>
<files>
<filename plugin="vmcouponprice">vmcouponprice.php</filename>
<folder>language</folder>
</files>
<languages folder="language">
<language tag="en-GB">en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.ini</language>
<language tag="en-GB">en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.sys.ini</language>
</languages>
<scriptfile>vmcouponprice.script.php</scriptfile>
<!-- VM 3.x support (fields rather than params): -->
<vmconfig></vmconfig>
<!-- VM 2.0 support (params rather than fields): -->
<params></params>
</extension>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment