woo-discount-rules
Last commit date
assets
7 years ago
helper
7 years ago
i18n
7 years ago
includes
7 years ago
vendor
7 years ago
view
7 years ago
loader.php
7 years ago
readme.txt
7 years ago
woo-discount-rules.php
7 years ago
loader.php
354 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; // Exit if accessed directly |
| 3 | |
| 4 | /** |
| 5 | * Plugin Directory. |
| 6 | */ |
| 7 | define('WOO_DISCOUNT_DIR', untrailingslashit(plugin_dir_path(__FILE__))); |
| 8 | |
| 9 | /** |
| 10 | * Plugin Directory URI. |
| 11 | */ |
| 12 | define('WOO_DISCOUNT_URI', untrailingslashit(plugin_dir_url(__FILE__))); |
| 13 | |
| 14 | /** |
| 15 | * Plugin Base Name. |
| 16 | */ |
| 17 | define('WOO_DISCOUNT_PLUGIN_BASENAME', plugin_basename(__FILE__)); |
| 18 | |
| 19 | if(!function_exists('get_plugin_data')){ |
| 20 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Version of Woo Discount Rules. |
| 25 | */ |
| 26 | $pluginDetails = get_plugin_data(plugin_dir_path(__FILE__).'woo-discount-rules.php'); |
| 27 | define('WOO_DISCOUNT_VERSION', $pluginDetails['Version']); |
| 28 | |
| 29 | if(!class_exists('FlycartWooDiscountRules')){ |
| 30 | class FlycartWooDiscountRules{ |
| 31 | |
| 32 | private static $instance; |
| 33 | public $discountBase; |
| 34 | public $pricingRules; |
| 35 | public $config; |
| 36 | |
| 37 | /** |
| 38 | * To run the plugin |
| 39 | * */ |
| 40 | public static function init() { |
| 41 | if ( self::$instance == null ) { |
| 42 | self::$instance = new FlycartWooDiscountRules(); |
| 43 | } |
| 44 | return self::$instance; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * FlycartWooDiscountRules constructor |
| 49 | * */ |
| 50 | public function __construct() { |
| 51 | $this->includeFiles(); |
| 52 | $this->discountBase = new FlycartWooDiscountBase(); |
| 53 | $this->runUpdater(); |
| 54 | $this->pricingRules = new FlycartWooDiscountRulesPricingRules(); |
| 55 | if (is_admin()) { |
| 56 | $this->loadAdminScripts(); |
| 57 | } |
| 58 | if(FlycartWooDiscountRulesGeneralHelper::doIHaveToRun()){ |
| 59 | $this->loadSiteScripts(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * To include Files |
| 65 | * */ |
| 66 | protected function includeFiles(){ |
| 67 | include_once('helper/woo-function.php'); |
| 68 | include_once('includes/pricing-rules.php'); |
| 69 | include_once('helper/general-helper.php'); |
| 70 | include_once('includes/cart-rules.php'); |
| 71 | include_once('includes/discount-base.php'); |
| 72 | include_once('helper/purchase.php'); |
| 73 | require_once __DIR__ . '/vendor/autoload.php'; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Run Plugin updater |
| 78 | * */ |
| 79 | protected function runUpdater(){ |
| 80 | try{ |
| 81 | require plugin_dir_path( __FILE__ ).'/vendor/yahnis-elsts/plugin-update-checker/plugin-update-checker.php'; |
| 82 | |
| 83 | $purchase_helper = new FlycartWooDiscountRulesPurchase(); |
| 84 | $purchase_helper->init(); |
| 85 | $update_url = $purchase_helper->getUpdateURL(); |
| 86 | if(!$purchase_helper->isPro()){ |
| 87 | $dlid = $this->discountBase->getConfigData('license_key', null); |
| 88 | if(empty($dlid)) return false; |
| 89 | } |
| 90 | $myUpdateChecker = Puc_v4_Factory::buildUpdateChecker( |
| 91 | $update_url, |
| 92 | plugin_dir_path( __FILE__ ).'woo-discount-rules.php', |
| 93 | 'woo-discount-rules' |
| 94 | ); |
| 95 | add_action( 'after_plugin_row', array($purchase_helper, 'woodisc_after_plugin_row'),10,3 ); |
| 96 | |
| 97 | add_action('wp_ajax_forceValidateLicenseKey', array($purchase_helper, 'forceValidateLicenseKey')); |
| 98 | |
| 99 | add_action( 'admin_notices', array($purchase_helper, 'errorNoticeInAdminPages')); |
| 100 | } catch (Exception $e){} |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Load Admin scripts |
| 105 | * */ |
| 106 | protected function loadAdminScripts(){ |
| 107 | // Init in Admin Menu |
| 108 | add_action('admin_menu', array($this->discountBase, 'adminMenu')); |
| 109 | add_action('wp_ajax_savePriceRule', array($this->discountBase, 'savePriceRule')); |
| 110 | add_action('wp_ajax_saveCartRule', array($this->discountBase, 'saveCartRule')); |
| 111 | add_action('wp_ajax_saveConfig', array($this->discountBase, 'saveConfig')); |
| 112 | add_action('wp_ajax_resetWDRCache', array($this->discountBase, 'resetWDRCache')); |
| 113 | add_action('wp_ajax_loadProductSelectBox', array($this->discountBase, 'loadProductSelectBox')); |
| 114 | |
| 115 | add_action('wp_ajax_UpdateStatus', array($this->discountBase, 'updateStatus')); |
| 116 | add_action('wp_ajax_RemoveRule', array($this->discountBase, 'removeRule')); |
| 117 | add_action('wp_ajax_doBulkAction', array($this->discountBase, 'doBulkAction')); |
| 118 | add_action('wp_ajax_createDuplicateRule', array($this->discountBase, 'createDuplicateRule')); |
| 119 | add_action('admin_enqueue_scripts', array($this->discountBase, 'woo_discount_adminPageScript') ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Apply discount rules |
| 124 | * */ |
| 125 | public function applyDiscountRules(){ |
| 126 | $this->discountBase->handlePriceDiscount(); |
| 127 | $removeTheEvent = apply_filters('woo_discount_rules_remove_event_woocommerce_before_calculate_totals', false); |
| 128 | if(!$removeTheEvent){ |
| 129 | remove_action('woocommerce_before_calculate_totals', array($this, 'applyDiscountRules'), 1000); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Apply discount rules |
| 135 | * */ |
| 136 | public function applyCartDiscountRules(){ |
| 137 | $this->discountBase->handleCartDiscount(); |
| 138 | remove_action('woocommerce_cart_loaded_from_session', array($this, 'applyCartDiscountRules'), 97); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Script on product page for loading variant strikeout |
| 143 | * */ |
| 144 | public function script_on_product_page(){ |
| 145 | $runVariationStrikeoutAjax = apply_filters('woo_discount_rules_run_variation_strikeout_through_ajax', true); |
| 146 | $script = '<script>'; |
| 147 | $script .= 'jQuery( document ).ready( function() {'; |
| 148 | $do_product_page_strikeout = $this->discountBase->getConfigData('show_price_discount_on_product_page', 'dont'); |
| 149 | $enable_variable_product_cache = $this->discountBase->getConfigData('enable_variable_product_cache', 0); |
| 150 | if($do_product_page_strikeout == 'show' && $runVariationStrikeoutAjax) { |
| 151 | $script .= 'jQuery( ".single_variation_wrap" ).on( "show_variation", function ( event, variation, purchasable ) {'; |
| 152 | $script .= ' var container = jQuery(".single_variation .woocommerce-variation-price");'; |
| 153 | $script .= ' var current_object = jQuery(this); |
| 154 | current_object.trigger("woo_discount_rules_before_variant_strikeout");/*container.hide("slow");*/'; |
| 155 | $script .= ' jQuery.ajax({ |
| 156 | url: woo_discount_rules.ajax_url, |
| 157 | dataType: "json", |
| 158 | type: "POST", |
| 159 | data: {action: "loadWooDiscountedPriceForVariant", id: variation.variation_id, price_html: variation.price_html}, |
| 160 | beforeSend: function() { |
| 161 | }, |
| 162 | complete: function() { |
| 163 | }, |
| 164 | success: function (response) { |
| 165 | if(response.status == 1){ |
| 166 | jQuery(".single_variation .woocommerce-variation-price").html(response.price_html); |
| 167 | } |
| 168 | current_object.trigger("woo_discount_rules_after_variant_strikeout"); |
| 169 | /*container.show("slow");*/ |
| 170 | } |
| 171 | });'; |
| 172 | $script .= ' });'; |
| 173 | } |
| 174 | if($enable_variable_product_cache){ |
| 175 | $script .= ' var woo_discount_rules_session_storage_id = "woo_discount_rules_session_storage_id_"; |
| 176 | var woo_discount_rules_session_storage_time_id = "woo_discount_rules_session_storage_time_id_"; |
| 177 | const WOO_DISCOUNT_RULES = { |
| 178 | checkSessionStorageExists: function (id) { |
| 179 | var name = woo_discount_rules_session_storage_id+id; |
| 180 | if (sessionStorage.getItem(name) === null) { |
| 181 | return false; |
| 182 | } |
| 183 | return true; |
| 184 | }, |
| 185 | setSessionStorage: function (id, value) { |
| 186 | var name = woo_discount_rules_session_storage_id+id; |
| 187 | sessionStorage.setItem(name, value); |
| 188 | }, |
| 189 | getSessionStorage: function (id) { |
| 190 | var name = woo_discount_rules_session_storage_id+id; |
| 191 | return sessionStorage.getItem(name); |
| 192 | }, |
| 193 | setSessionStorageTime: function (id, value) { |
| 194 | var name = woo_discount_rules_session_storage_time_id+id; |
| 195 | sessionStorage.setItem(name, value); |
| 196 | }, |
| 197 | getSessionStorageTime: function (id) { |
| 198 | var name = woo_discount_rules_session_storage_time_id+id; |
| 199 | return sessionStorage.getItem(name); |
| 200 | } |
| 201 | } |
| 202 | '; |
| 203 | } |
| 204 | |
| 205 | $script .= ' if(jQuery(".woo_discount_rules_variant_table").length > 0){ |
| 206 | var p_id = jQuery( ".woo_discount_rules_variant_table" ).attr("data-id");'; |
| 207 | if($enable_variable_product_cache) { |
| 208 | $script .= ' var already_exists = WOO_DISCOUNT_RULES.checkSessionStorageExists(p_id);'; |
| 209 | $script .= ' var last_storage_time = WOO_DISCOUNT_RULES.getSessionStorageTime(p_id);'; |
| 210 | } else { |
| 211 | $script .= ' var already_exists = 0;'; |
| 212 | $script .= ' var last_storage_time = "";'; |
| 213 | } |
| 214 | $script .= ' setTimeout(function(){ |
| 215 | jQuery.ajax({ |
| 216 | url: woo_discount_rules.ajax_url, |
| 217 | type: "POST", |
| 218 | data: {action: "loadWooDiscountedDiscountTable", id: p_id, loaded: already_exists, time: last_storage_time}, |
| 219 | beforeSend: function() { |
| 220 | }, |
| 221 | complete: function() { |
| 222 | }, |
| 223 | success: function (response) { |
| 224 | responseData = jQuery.parseJSON(response); |
| 225 | if(responseData.cookie == "1" && already_exists){'; |
| 226 | if($enable_variable_product_cache) { |
| 227 | $script .= ' jQuery(".woo_discount_rules_variant_table").html(WOO_DISCOUNT_RULES.getSessionStorage(p_id));'; |
| 228 | } |
| 229 | $script .= ' } else { |
| 230 | jQuery(".woo_discount_rules_variant_table").html(responseData.html);'; |
| 231 | if($enable_variable_product_cache) { |
| 232 | $script .= ' WOO_DISCOUNT_RULES.setSessionStorage(p_id, responseData.html); |
| 233 | WOO_DISCOUNT_RULES.setSessionStorageTime(p_id, responseData.time);'; |
| 234 | } |
| 235 | $script .= ' } |
| 236 | } |
| 237 | }); |
| 238 | }, 1);'; |
| 239 | $script .= ' }'; |
| 240 | $script .= '});'; |
| 241 | $script .= '</script>'; |
| 242 | |
| 243 | echo $script; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Load Admin scripts |
| 248 | * */ |
| 249 | protected function loadSiteScripts(){ |
| 250 | $woocommerce_version = '2.0.0'; |
| 251 | $pluginDetails = get_plugin_data(WP_PLUGIN_DIR.'/woocommerce/woocommerce.php'); |
| 252 | if(isset($pluginDetails['Version'])){ |
| 253 | $woocommerce_version = $pluginDetails['Version']; |
| 254 | } |
| 255 | |
| 256 | add_action('woocommerce_after_cart_item_quantity_update', array($this->discountBase, 'handleDiscount'), 100); |
| 257 | if(version_compare($woocommerce_version, '3.0', '>=')){ |
| 258 | add_action('woocommerce_before_calculate_totals', array($this, 'applyDiscountRules'), 1000); |
| 259 | add_action('woocommerce_cart_loaded_from_session', array($this, 'applyCartDiscountRules'), 97); |
| 260 | add_action( 'woocommerce_after_cart_item_quantity_update', array($this->pricingRules, 'handleBOGODiscountOnUpdateQuantity'), 10, 4 ); |
| 261 | } else { |
| 262 | add_action( 'woocommerce_after_cart_item_quantity_update', array($this->pricingRules, 'handleBOGODiscountOnUpdateQuantity'), 10, 3 ); |
| 263 | add_action('woocommerce_cart_loaded_from_session', array($this->discountBase, 'handleDiscount'), 100); |
| 264 | } |
| 265 | |
| 266 | add_action('woocommerce_add_to_cart', array($this->pricingRules, 'handleBOGODiscount'), 10, 6); |
| 267 | |
| 268 | $add_free_product_on_coupon_applied = $this->discountBase->getConfigData('add_free_product_on_coupon_applied', 0); |
| 269 | if($add_free_product_on_coupon_applied){ |
| 270 | add_action('woocommerce_applied_coupon', array($this->pricingRules, 'handleBOGODiscountAfterApplyCoupon'), 10, 1); |
| 271 | } |
| 272 | |
| 273 | // Manually Update Line Item Name. |
| 274 | add_filter('woocommerce_cart_item_name', array($this->discountBase, 'modifyName')); |
| 275 | |
| 276 | // Remove Filter to make the previous one as last filter. |
| 277 | remove_filter('woocommerce_cart_item_name', 'filter_woocommerce_cart_item_name', 10, 3); |
| 278 | |
| 279 | // Alter the Display Price HTML. |
| 280 | add_filter('woocommerce_cart_item_price', array($this->pricingRules, 'replaceVisiblePricesCart'), 1000, 3); |
| 281 | |
| 282 | //replace visible price in product page |
| 283 | add_filter('woocommerce_get_price_html', array($this->pricingRules, 'replaceVisiblePricesOptimized'), 100, 3); |
| 284 | //replace visible price in product page for variant |
| 285 | add_filter('woocommerce_available_variation', array($this->pricingRules, 'replaceVisiblePricesForVariant'), 100, 3); |
| 286 | |
| 287 | // Older Version support this hook. |
| 288 | add_filter('woocommerce_cart_item_price_html', array($this->pricingRules, 'replaceVisiblePricesCart'), 1000, 3); |
| 289 | |
| 290 | // Pricing Table of Individual Product. |
| 291 | add_filter('woocommerce_before_add_to_cart_form', array($this->pricingRules, 'priceTable')); |
| 292 | add_filter('woocommerce_before_add_to_cart_form', array($this, 'script_on_product_page')); |
| 293 | |
| 294 | // Updating Log After Creating Order |
| 295 | add_action('woocommerce_thankyou', array($this->discountBase, 'storeLog')); |
| 296 | |
| 297 | add_action( 'woocommerce_after_checkout_form', array($this->discountBase, 'addScriptInCheckoutPage')); |
| 298 | |
| 299 | //To enable on-sale tag |
| 300 | add_filter('woocommerce_product_is_on_sale', array($this->pricingRules, 'displayProductIsOnSaleTagOptimized'), 10, 2); |
| 301 | |
| 302 | $force_refresh_cart_widget = $this->discountBase->getConfigData('force_refresh_cart_widget', 0); |
| 303 | if($force_refresh_cart_widget){ |
| 304 | if (isset($_REQUEST['wc-ajax']) && ($_REQUEST['wc-ajax'] == 'add_to_cart' || $_REQUEST['wc-ajax'] == 'remove_from_cart')) { |
| 305 | add_action('woocommerce_before_mini_cart', array($this, 'applyRulesBeforeMiniCart'), 10); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | add_action('wp_ajax_loadWooDiscountedPriceForVariant', array($this->pricingRules, 'getWooDiscountedPriceForVariant')); |
| 310 | add_action('wp_ajax_nopriv_loadWooDiscountedPriceForVariant', array($this->pricingRules, 'getWooDiscountedPriceForVariant')); |
| 311 | add_action('wp_ajax_loadWooDiscountedDiscountTable', array($this->pricingRules, 'getWooDiscountedPriceTableForVariant')); |
| 312 | add_action('wp_ajax_nopriv_loadWooDiscountedDiscountTable', array($this->pricingRules, 'getWooDiscountedPriceTableForVariant')); |
| 313 | add_action( 'wp_enqueue_scripts', array($this, 'includeScriptAndStyles') ); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * To include the styles |
| 318 | * */ |
| 319 | public function includeScriptAndStyles(){ |
| 320 | // Enqueued script with localized data. |
| 321 | wp_register_script( 'woo_discount_rules_site', WOO_DISCOUNT_URI . '/assets/js/woo_discount_rules.js', array('jquery'), WOO_DISCOUNT_VERSION, true ); |
| 322 | wp_localize_script('woo_discount_rules_site', 'woo_discount_rules', array( |
| 323 | 'home_url' => get_home_url(), |
| 324 | 'admin_url' => admin_url(), |
| 325 | 'ajax_url' => admin_url('admin-ajax.php') |
| 326 | )); |
| 327 | wp_enqueue_script( 'woo_discount_rules_site'); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * To load the dynamic data in mini-cart/cart widget while add to cart and remove from cart through widget |
| 332 | * */ |
| 333 | public function applyRulesBeforeMiniCart(){ |
| 334 | WC()->cart->get_cart_from_session(); |
| 335 | $this->discountBase->handlePriceDiscount(); |
| 336 | WC()->cart->calculate_totals(); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * init Woo Discount Rules |
| 343 | */ |
| 344 | if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) { |
| 345 | global $flycart_woo_discount_rules; |
| 346 | $flycart_woo_discount_rules = FlycartWooDiscountRules::init(); |
| 347 | $purchase_helper = new FlycartWooDiscountRulesPurchase(); |
| 348 | if($purchase_helper->isPro()){ |
| 349 | include_once('includes/advanced/free_shipping_method.php'); |
| 350 | include_once('includes/advanced/pricing-productdependent.php'); |
| 351 | include_once('includes/advanced/cart-totals.php'); |
| 352 | include_once('includes/advanced/advanced-helper.php'); |
| 353 | } |
| 354 | } |