diff --git a/plugins/vmcouponprice/Makefile b/plugins/vmcouponprice/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..df0c814dacdbc9b6d16fd85680cda40ae01b219b --- /dev/null +++ b/plugins/vmcouponprice/Makefile @@ -0,0 +1,19 @@ +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) diff --git a/plugins/vmcouponprice/index.html b/plugins/vmcouponprice/index.html new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/plugins/vmcouponprice/language/en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.ini b/plugins/vmcouponprice/language/en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.ini new file mode 100644 index 0000000000000000000000000000000000000000..56172e564d4872c9055d544482870824afcc1411 --- /dev/null +++ b/plugins/vmcouponprice/language/en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.ini @@ -0,0 +1,6 @@ +; 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>" diff --git a/plugins/vmcouponprice/language/en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.sys.ini b/plugins/vmcouponprice/language/en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.sys.ini new file mode 100644 index 0000000000000000000000000000000000000000..56172e564d4872c9055d544482870824afcc1411 --- /dev/null +++ b/plugins/vmcouponprice/language/en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.sys.ini @@ -0,0 +1,6 @@ +; 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>" diff --git a/plugins/vmcouponprice/language/en-GB/index.html b/plugins/vmcouponprice/language/en-GB/index.html new file mode 100644 index 0000000000000000000000000000000000000000..2efb97f319a35f6bd80f1751134ed71ec11888eb --- /dev/null +++ b/plugins/vmcouponprice/language/en-GB/index.html @@ -0,0 +1 @@ +<!DOCTYPE html><title></title> diff --git a/plugins/vmcouponprice/language/index.html b/plugins/vmcouponprice/language/index.html new file mode 100644 index 0000000000000000000000000000000000000000..2efb97f319a35f6bd80f1751134ed71ec11888eb --- /dev/null +++ b/plugins/vmcouponprice/language/index.html @@ -0,0 +1 @@ +<!DOCTYPE html><title></title> diff --git a/plugins/vmcouponprice/plg_opentools_vmshipmentrules_vmcouponprice_v1.0.zip b/plugins/vmcouponprice/plg_opentools_vmshipmentrules_vmcouponprice_v1.0.zip new file mode 100644 index 0000000000000000000000000000000000000000..867b08877e158584d0329a6833feb1139fd0dc24 Binary files /dev/null and b/plugins/vmcouponprice/plg_opentools_vmshipmentrules_vmcouponprice_v1.0.zip differ diff --git a/plugins/vmcouponprice/vmcouponprice.php b/plugins/vmcouponprice/vmcouponprice.php new file mode 100644 index 0000000000000000000000000000000000000000..c8d8e6bd72c4c015442c6ee252bebd3b366fbdfd --- /dev/null +++ b/plugins/vmcouponprice/vmcouponprice.php @@ -0,0 +1,79 @@ +<?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 diff --git a/plugins/vmcouponprice/vmcouponprice.script.php b/plugins/vmcouponprice/vmcouponprice.script.php new file mode 100644 index 0000000000000000000000000000000000000000..3671a9f818f2913a436c2b8485da0ad31d3fb3fc --- /dev/null +++ b/plugins/vmcouponprice/vmcouponprice.script.php @@ -0,0 +1,88 @@ +<?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(); +// } +} diff --git a/plugins/vmcouponprice/vmcouponprice.xml b/plugins/vmcouponprice/vmcouponprice.xml new file mode 100644 index 0000000000000000000000000000000000000000..1f3a8e2bf2cb3ff7964969db697dec5e5fef75c6 --- /dev/null +++ b/plugins/vmcouponprice/vmcouponprice.xml @@ -0,0 +1,27 @@ +<?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>