woo-discount-rules
Last commit date
assets
6 years ago
helper
6 years ago
i18n
6 years ago
includes
6 years ago
vendor
6 years ago
view
6 years ago
loader.php
6 years ago
readme.txt
6 years ago
woo-discount-rules.php
6 years ago
loader.php
672 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 | if(!function_exists('get_plugin_data')){ |
| 15 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Version of Woo Discount Rules. |
| 20 | */ |
| 21 | $pluginDetails = get_plugin_data(plugin_dir_path(__FILE__).'woo-discount-rules.php'); |
| 22 | define('WOO_DISCOUNT_VERSION', $pluginDetails['Version']); |
| 23 | |
| 24 | if(!class_exists('FlycartWooDiscountRules')){ |
| 25 | class FlycartWooDiscountRules{ |
| 26 | |
| 27 | private static $instance; |
| 28 | public $discountBase; |
| 29 | public $pricingRules; |
| 30 | public $config; |
| 31 | |
| 32 | public static $product_variations = array(); |
| 33 | |
| 34 | /** |
| 35 | * To run the plugin |
| 36 | * */ |
| 37 | public static function init() { |
| 38 | if ( self::$instance == null ) { |
| 39 | self::$instance = new FlycartWooDiscountRules(); |
| 40 | } |
| 41 | return self::$instance; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * FlycartWooDiscountRules constructor |
| 46 | * */ |
| 47 | public function __construct() { |
| 48 | $this->hasWPML(); |
| 49 | $this->includeFiles(); |
| 50 | $this->discountBase = new FlycartWooDiscountBase(); |
| 51 | $this->runUpdater(); |
| 52 | $this->pricingRules = new FlycartWooDiscountRulesPricingRules(); |
| 53 | if (is_admin()) { |
| 54 | $this->loadAdminScripts(); |
| 55 | } |
| 56 | if(FlycartWooDiscountRulesGeneralHelper::doIHaveToRun()){ |
| 57 | $this->loadSiteScripts(); |
| 58 | } |
| 59 | $this->loadCommonScripts(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * To check for WPML |
| 64 | * */ |
| 65 | protected function hasWPML(){ |
| 66 | if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) { |
| 67 | define('WOO_DISCOUNT_AVAILABLE_WPML', true); |
| 68 | } else { |
| 69 | define('WOO_DISCOUNT_AVAILABLE_WPML', false); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * To include Files |
| 75 | * */ |
| 76 | protected function includeFiles(){ |
| 77 | include_once(dirname(__FILE__).'/helper/woo-function.php'); |
| 78 | include_once(dirname(__FILE__).'/includes/pricing-rules.php'); |
| 79 | include_once(dirname(__FILE__).'/helper/general-helper.php'); |
| 80 | include_once(dirname(__FILE__).'/includes/cart-rules.php'); |
| 81 | include_once(dirname(__FILE__).'/includes/discount-base.php'); |
| 82 | include_once(dirname(__FILE__).'/helper/purchase.php'); |
| 83 | include_once(dirname(__FILE__).'/includes/compatibility.php'); |
| 84 | include_once(dirname(__FILE__).'/includes/survey.php'); |
| 85 | require_once __DIR__ . '/vendor/autoload.php'; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Run Plugin updater |
| 90 | * */ |
| 91 | protected function runUpdater(){ |
| 92 | add_filter('puc_request_info_result-woo-discount-rules', array($this, 'loadWooDiscountRulesUpdateDetails'), 10, 2); |
| 93 | |
| 94 | try{ |
| 95 | require plugin_dir_path( __FILE__ ).'/vendor/yahnis-elsts/plugin-update-checker/plugin-update-checker.php'; |
| 96 | |
| 97 | $purchase_helper = new FlycartWooDiscountRulesPurchase(); |
| 98 | $purchase_helper->init(); |
| 99 | $update_url = $purchase_helper->getUpdateURL(); |
| 100 | if(!$purchase_helper->isPro()){ |
| 101 | $dlid = $this->discountBase->getConfigData('license_key', null); |
| 102 | if(empty($dlid)) return false; |
| 103 | } |
| 104 | $myUpdateChecker = Puc_v4_Factory::buildUpdateChecker( |
| 105 | $update_url, |
| 106 | plugin_dir_path( __FILE__ ).'woo-discount-rules.php', |
| 107 | 'woo-discount-rules' |
| 108 | ); |
| 109 | add_action( 'after_plugin_row', array($purchase_helper, 'woodisc_after_plugin_row'),10,3 ); |
| 110 | |
| 111 | add_action('wp_ajax_forceValidateLicenseKey', array($purchase_helper, 'forceValidateLicenseKey')); |
| 112 | |
| 113 | add_action( 'admin_notices', array($purchase_helper, 'errorNoticeInAdminPages')); |
| 114 | } catch (Exception $e){} |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * To load Woo discount rules update details |
| 119 | * */ |
| 120 | public function loadWooDiscountRulesUpdateDetails($pluginInfo, $result){ |
| 121 | try{ |
| 122 | global $wp_version; |
| 123 | // include an unmodified $wp_version |
| 124 | include( ABSPATH . WPINC . '/version.php' ); |
| 125 | $args = array('slug' => 'woo-discount-rules', 'fields' => array('active_installs')); |
| 126 | $response = wp_remote_post( |
| 127 | 'http://api.wordpress.org/plugins/info/1.0/', |
| 128 | array( |
| 129 | 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ), |
| 130 | 'body' => array( |
| 131 | 'action' => 'plugin_information', |
| 132 | 'request'=>serialize((object)$args) |
| 133 | ) |
| 134 | ) |
| 135 | ); |
| 136 | |
| 137 | if(!empty($response)){ |
| 138 | $returned_object = maybe_unserialize(wp_remote_retrieve_body($response)); |
| 139 | if(!empty($returned_object)){ |
| 140 | if(!empty($returned_object->name)) $pluginInfo->name = $returned_object->name; |
| 141 | if(!empty($returned_object->sections)) $pluginInfo->sections = $returned_object->sections; |
| 142 | if(!empty($returned_object->author)) $pluginInfo->author = $returned_object->author; |
| 143 | if(!empty($returned_object->author_profile)) $pluginInfo->author_profile = $returned_object->author_profile; |
| 144 | if(!empty($returned_object->requires)) $pluginInfo->requires = $returned_object->requires; |
| 145 | if(!empty($returned_object->tested)) $pluginInfo->tested = $returned_object->tested; |
| 146 | if(!empty($returned_object->rating)) $pluginInfo->rating = $returned_object->rating; |
| 147 | if(!empty($returned_object->ratings)) $pluginInfo->ratings = $returned_object->ratings; |
| 148 | if(!empty($returned_object->num_ratings)) $pluginInfo->num_ratings = $returned_object->num_ratings; |
| 149 | if(!empty($returned_object->support_threads)) $pluginInfo->support_threads = $returned_object->support_threads; |
| 150 | if(!empty($returned_object->support_threads_resolved)) $pluginInfo->support_threads_resolved = $returned_object->support_threads_resolved; |
| 151 | if(!empty($returned_object->downloaded)) $pluginInfo->downloaded = $returned_object->downloaded; |
| 152 | if(!empty($returned_object->last_updated)) $pluginInfo->last_updated = $returned_object->last_updated; |
| 153 | if(!empty($returned_object->added)) $pluginInfo->added = $returned_object->added; |
| 154 | if(!empty($returned_object->versions)) $pluginInfo->versions = $returned_object->versions; |
| 155 | if(!empty($returned_object->tags)) $pluginInfo->tags = $returned_object->tags; |
| 156 | if(!empty($returned_object->screenshots)) $pluginInfo->screenshots = $returned_object->screenshots; |
| 157 | if(!empty($returned_object->active_installs)) $pluginInfo->active_installs = $returned_object->active_installs; |
| 158 | } |
| 159 | } |
| 160 | } catch (Exception $e){} |
| 161 | |
| 162 | return $pluginInfo; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Show up the survey form |
| 167 | */ |
| 168 | function setupSurveyForm() |
| 169 | { |
| 170 | $survey = new FlycartWooDiscountRulesSurvey(); |
| 171 | $survey->init('woo-discount-rules', 'Discount Rules for WooCommerce', 'woo-discount-rules'); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Load Admin scripts |
| 176 | * */ |
| 177 | protected function loadAdminScripts(){ |
| 178 | // Init in Admin Menu |
| 179 | add_action('admin_menu', array($this->discountBase, 'adminMenu')); |
| 180 | add_action('wp_ajax_savePriceRule', array($this->discountBase, 'savePriceRule')); |
| 181 | add_action('wp_ajax_saveCartRule', array($this->discountBase, 'saveCartRule')); |
| 182 | add_action('wp_ajax_saveConfig', array($this->discountBase, 'saveConfig')); |
| 183 | add_action('wp_ajax_resetWDRCache', array($this->discountBase, 'resetWDRCache')); |
| 184 | add_action('wp_ajax_loadProductSelectBox', array($this->discountBase, 'loadProductSelectBox')); |
| 185 | add_action('wp_ajax_loadCoupons', array($this->discountBase, 'loadCoupons')); |
| 186 | |
| 187 | add_action('wp_ajax_UpdateStatus', array($this->discountBase, 'updateStatus')); |
| 188 | add_action('wp_ajax_RemoveRule', array($this->discountBase, 'removeRule')); |
| 189 | add_action('wp_ajax_doBulkAction', array($this->discountBase, 'doBulkAction')); |
| 190 | add_action('wp_ajax_createDuplicateRule', array($this->discountBase, 'createDuplicateRule')); |
| 191 | add_action('admin_enqueue_scripts', array($this->discountBase, 'woo_discount_adminPageScript'), 100 ); |
| 192 | $display_you_saved_text = $this->discountBase->getConfigData('display_you_saved_text', 'no'); |
| 193 | if(in_array($display_you_saved_text, array('on_each_line_item', 'both_line_item_and_after_total'))){ |
| 194 | add_action( 'woocommerce_after_order_itemmeta', array( $this->pricingRules, 'addAdditionalContentInAfterOrderItemMeta'), 1000, 3); |
| 195 | } |
| 196 | if(in_array($display_you_saved_text, array('after_total', 'both_line_item_and_after_total'))){ |
| 197 | add_action( 'woocommerce_admin_order_totals_after_total', array( $this->pricingRules, 'displayTotalSavingsThroughDiscountInOrder'), 10); |
| 198 | } |
| 199 | add_filter( 'plugin_action_links_' . WOO_DISCOUNT_PLUGIN_BASENAME, array('FlycartWooDiscountBase', 'addActionLinksInPluginPage') ); |
| 200 | |
| 201 | add_action('admin_init', array($this, 'setupSurveyForm'), 10); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Apply discount rules |
| 206 | * */ |
| 207 | public function applyDiscountRules(){ |
| 208 | $this->discountBase->handlePriceDiscount(); |
| 209 | $removeTheEvent = apply_filters('woo_discount_rules_remove_event_woocommerce_before_calculate_totals', false); |
| 210 | if(!$removeTheEvent){ |
| 211 | remove_action('woocommerce_before_calculate_totals', array($this, 'applyDiscountRules'), 1000); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Apply discount rules |
| 217 | * */ |
| 218 | public function applyCartDiscountRules(){ |
| 219 | $removeTheEvent = apply_filters('woo_discount_rules_remove_event_woocommerce_cart_loaded_from_session', false); |
| 220 | if(!$removeTheEvent){ |
| 221 | remove_action('woocommerce_cart_loaded_from_session', array($this, 'applyCartDiscountRules'), 97); |
| 222 | } |
| 223 | $this->discountBase->handleCartDiscount(); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Script on product page for loading variant strikeout |
| 228 | * */ |
| 229 | public function script_on_product_page() |
| 230 | { |
| 231 | $runVariationStrikeoutAjax = apply_filters('woo_discount_rules_run_variation_strikeout_through_ajax', true); |
| 232 | $script = '<script>'; |
| 233 | $script .= 'if(flycart_woo_discount_rules_strikeout_script_executed == undefined){'; |
| 234 | $script .= 'jQuery( document ).ready( function() {'; |
| 235 | $enable_variable_product_cache = $this->discountBase->getConfigData('enable_variable_product_cache', 0); |
| 236 | if ((FlycartWooDiscountRulesGeneralHelper::showDiscountOnProductPage()) && $runVariationStrikeoutAjax) { |
| 237 | $script .= 'jQuery( ".single_variation_wrap" ).on( "show_variation", function ( event, variation, purchasable ) {'; |
| 238 | $script .= ' var container = jQuery(".single_variation .woocommerce-variation-price");'; |
| 239 | $script .= ' var current_object = jQuery(this); |
| 240 | current_object.trigger("woo_discount_rules_before_variant_strikeout");/*container.hide("slow");*/'; |
| 241 | $script .= ' jQuery.ajax({ |
| 242 | url: woo_discount_rules.ajax_url, |
| 243 | dataType: "json", |
| 244 | type: "POST", |
| 245 | data: {action: "loadWooDiscountedPriceForVariant", id: variation.variation_id, price_html: variation.price_html}, |
| 246 | beforeSend: function() { |
| 247 | }, |
| 248 | complete: function() { |
| 249 | }, |
| 250 | success: function (response) { |
| 251 | if(response.status == 1){ |
| 252 | jQuery(".single_variation .woocommerce-variation-price").html(response.price_html); |
| 253 | } |
| 254 | current_object.trigger("woo_discount_rules_after_variant_strikeout"); |
| 255 | /*container.show("slow");*/ |
| 256 | } |
| 257 | });'; |
| 258 | $script .= ' });'; |
| 259 | } |
| 260 | if ($enable_variable_product_cache) { |
| 261 | $script .= ' var woo_discount_rules_session_storage_id = "woo_discount_rules_session_storage_id_"; |
| 262 | var woo_discount_rules_session_storage_time_id = "woo_discount_rules_session_storage_time_id_"; |
| 263 | const WOO_DISCOUNT_RULES = { |
| 264 | checkSessionStorageExists: function (id) { |
| 265 | var name = woo_discount_rules_session_storage_id+id; |
| 266 | if (sessionStorage.getItem(name) === null) { |
| 267 | return false; |
| 268 | } |
| 269 | return true; |
| 270 | }, |
| 271 | setSessionStorage: function (id, value) { |
| 272 | var name = woo_discount_rules_session_storage_id+id; |
| 273 | sessionStorage.setItem(name, value); |
| 274 | }, |
| 275 | getSessionStorage: function (id) { |
| 276 | var name = woo_discount_rules_session_storage_id+id; |
| 277 | return sessionStorage.getItem(name); |
| 278 | }, |
| 279 | setSessionStorageTime: function (id, value) { |
| 280 | var name = woo_discount_rules_session_storage_time_id+id; |
| 281 | sessionStorage.setItem(name, value); |
| 282 | }, |
| 283 | getSessionStorageTime: function (id) { |
| 284 | var name = woo_discount_rules_session_storage_time_id+id; |
| 285 | return sessionStorage.getItem(name); |
| 286 | } |
| 287 | } |
| 288 | '; |
| 289 | } |
| 290 | |
| 291 | $script .= ' if(jQuery(".woo_discount_rules_variant_table").length > 0){ |
| 292 | var p_id = jQuery( ".woo_discount_rules_variant_table" ).attr("data-id");'; |
| 293 | if ($enable_variable_product_cache) { |
| 294 | $script .= ' var already_exists = WOO_DISCOUNT_RULES.checkSessionStorageExists(p_id);'; |
| 295 | $script .= ' var last_storage_time = WOO_DISCOUNT_RULES.getSessionStorageTime(p_id);'; |
| 296 | } else { |
| 297 | $script .= ' var already_exists = 0;'; |
| 298 | $script .= ' var last_storage_time = "";'; |
| 299 | } |
| 300 | $enable_discount_table = $this->discountBase->getConfigData('show_discount_table', 'show'); |
| 301 | if(in_array($enable_discount_table, array('show', 'advance'))){ |
| 302 | $script .= ' setTimeout(function(){ |
| 303 | jQuery.ajax({ |
| 304 | url: woo_discount_rules.ajax_url, |
| 305 | type: "POST", |
| 306 | data: {action: "loadWooDiscountedDiscountTable", id: p_id, loaded: already_exists, time: last_storage_time}, |
| 307 | beforeSend: function() { |
| 308 | }, |
| 309 | complete: function() { |
| 310 | }, |
| 311 | success: function (response) { |
| 312 | responseData = jQuery.parseJSON(response); |
| 313 | if(responseData.cookie == "1" && already_exists){'; |
| 314 | if ($enable_variable_product_cache) { |
| 315 | $script .= ' jQuery(".woo_discount_rules_variant_table").html(WOO_DISCOUNT_RULES.getSessionStorage(p_id));'; |
| 316 | } |
| 317 | $script .= ' } else { |
| 318 | jQuery(".woo_discount_rules_variant_table").html(responseData.html);'; |
| 319 | if ($enable_variable_product_cache) { |
| 320 | $script .= ' WOO_DISCOUNT_RULES.setSessionStorage(p_id, responseData.html); |
| 321 | WOO_DISCOUNT_RULES.setSessionStorageTime(p_id, responseData.time);'; |
| 322 | } |
| 323 | $script .= ' } |
| 324 | } |
| 325 | }); |
| 326 | }, 1);'; |
| 327 | } |
| 328 | $script .= ' }'; |
| 329 | $script .= '});'; |
| 330 | $script .= 'var flycart_woo_discount_rules_strikeout_script_executed = 1; }'; |
| 331 | $script .= '</script>'; |
| 332 | |
| 333 | echo $script; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Load common scripts |
| 338 | * */ |
| 339 | protected function loadCommonScripts(){ |
| 340 | add_filter( 'woocommerce_email_styles', array($this, 'add_additional_woocommerce_email_styles'), 100); |
| 341 | $display_you_saved_text = $this->discountBase->getConfigData('display_you_saved_text', 'no'); |
| 342 | if(in_array($display_you_saved_text, array('on_each_line_item', 'both_line_item_and_after_total'))){ |
| 343 | add_filter( 'woocommerce_order_formatted_line_subtotal', array( $this->pricingRules, 'addAdditionalContentInOrderItemSubTotal'), 1000, 3); |
| 344 | } |
| 345 | if(in_array($display_you_saved_text, array('after_total', 'both_line_item_and_after_total'))){ |
| 346 | add_action( 'woocommerce_email_after_order_table', array( $this->pricingRules, 'displayTotalSavingsThroughDiscountInOrder'), 10); |
| 347 | } |
| 348 | add_action( 'woo_discount_rules_get_total_savings_through_discount_in_cart', array( $this->pricingRules, 'displayTotalSavingsThroughDiscountInCart'), 10); |
| 349 | add_action( 'woo_discount_rules_get_total_savings_through_discount_from_order', array( $this->pricingRules, 'displayTotalSavingsThroughDiscountInOrder'), 10); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Add additional css in emails |
| 354 | * */ |
| 355 | public function add_additional_woocommerce_email_styles($css){ |
| 356 | return $css.'.wdr_you_saved_con { |
| 357 | color: green; |
| 358 | }'; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Load Admin scripts |
| 363 | * */ |
| 364 | protected function loadSiteScripts(){ |
| 365 | $woocommerce_version = '2.0.0'; |
| 366 | $pluginDetails = get_plugin_data(WP_PLUGIN_DIR.'/woocommerce/woocommerce.php'); |
| 367 | if(isset($pluginDetails['Version'])){ |
| 368 | $woocommerce_version = $pluginDetails['Version']; |
| 369 | } |
| 370 | |
| 371 | add_action('woocommerce_after_cart_item_quantity_update', array($this->discountBase, 'handleDiscount'), 100); |
| 372 | if(version_compare($woocommerce_version, '3.0', '>=')){ |
| 373 | add_action('woocommerce_before_calculate_totals', array($this, 'applyDiscountRules'), 1000); |
| 374 | add_action('woocommerce_cart_loaded_from_session', array($this, 'applyCartDiscountRules'), 97); |
| 375 | add_action( 'woocommerce_after_cart_item_quantity_update', array($this->pricingRules, 'handleBOGODiscountOnUpdateQuantity'), 10, 4 ); |
| 376 | } else { |
| 377 | add_action( 'woocommerce_after_cart_item_quantity_update', array($this->pricingRules, 'handleBOGODiscountOnUpdateQuantity'), 10, 3 ); |
| 378 | add_action('woocommerce_cart_loaded_from_session', array($this->discountBase, 'handleDiscount'), 100); |
| 379 | } |
| 380 | |
| 381 | add_action('woocommerce_after_calculate_totals', array($this->discountBase, 'removeThirdPartyCoupon'), 20); |
| 382 | |
| 383 | add_filter('woocommerce_coupon_message', array($this->discountBase, 'removeAppliedMessageOfThirdPartyCoupon'), 10, 3); |
| 384 | |
| 385 | add_action('woocommerce_add_to_cart', array($this->pricingRules, 'handleBOGODiscount'), 10, 6); |
| 386 | |
| 387 | $add_free_product_on_coupon_applied = $this->discountBase->getConfigData('add_free_product_on_coupon_applied', 0); |
| 388 | if($add_free_product_on_coupon_applied){ |
| 389 | add_action('woocommerce_applied_coupon', array($this->pricingRules, 'handleBOGODiscountAfterApplyCoupon'), 10, 1); |
| 390 | } |
| 391 | |
| 392 | $add_free_product_on_change_checkout_fields = $this->discountBase->getConfigData('add_free_product_on_change_checkout_fields', 0); |
| 393 | if($add_free_product_on_change_checkout_fields) { |
| 394 | if (isset($_REQUEST['wc-ajax']) && sanitize_text_field($_REQUEST['wc-ajax']) == 'update_order_review') { |
| 395 | $this->add_free_product_on_change_checkout_fields(); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | add_action('woo_discount_rules_run_auto_add_bogo_product', array($this->pricingRules, 'handleBOGODiscountAfterApplyCoupon'), 10); |
| 400 | |
| 401 | add_action( 'woocommerce_checkout_create_order_line_item', array( $this->pricingRules, 'onCreateWoocommerceOrderLineItem'), 10, 4); |
| 402 | $display_you_saved_text = $this->discountBase->getConfigData('display_you_saved_text', 'no'); |
| 403 | if(in_array($display_you_saved_text, array('on_each_line_item', 'both_line_item_and_after_total'))){ |
| 404 | add_filter( 'woocommerce_cart_item_subtotal', array( $this->pricingRules, 'addAdditionalContentInCartItemSubTotal'), 1000, 3); |
| 405 | } |
| 406 | if(in_array($display_you_saved_text, array('after_total', 'both_line_item_and_after_total'))){ |
| 407 | add_action( 'woocommerce_cart_totals_after_order_total', array( $this->pricingRules, 'displayTotalSavingsThroughDiscountInCart'), 10); |
| 408 | add_action( 'woocommerce_review_order_after_order_total', array( $this->pricingRules, 'displayTotalSavingsThroughDiscountInCart'), 10); |
| 409 | add_action( 'woocommerce_order_details_after_order_table', array( $this->pricingRules, 'displayTotalSavingsThroughDiscountInOrder'), 10); |
| 410 | } |
| 411 | |
| 412 | // Manually Update Line Item Name. |
| 413 | add_filter('woocommerce_cart_item_name', array($this->discountBase, 'modifyName')); |
| 414 | |
| 415 | // Remove Filter to make the previous one as last filter. |
| 416 | remove_filter('woocommerce_cart_item_name', 'filter_woocommerce_cart_item_name', 10, 3); |
| 417 | |
| 418 | // Alter the Display Price HTML. |
| 419 | add_filter('woocommerce_cart_item_price', array($this->pricingRules, 'replaceVisiblePricesCart'), 1000, 3); |
| 420 | |
| 421 | //replace visible price in product page |
| 422 | add_filter('woocommerce_get_price_html', array($this->pricingRules, 'replaceVisiblePricesOptimized'), 1000, 3); |
| 423 | add_filter('woocommerce_get_price_html', array($this->pricingRules, 'replaceVisiblePricesForSalePriceAdjustment'), 9, 3); |
| 424 | |
| 425 | //replace visible price in product page for variant |
| 426 | add_filter('woocommerce_available_variation', array($this->pricingRules, 'replaceVisiblePricesForVariant'), 100, 3); |
| 427 | |
| 428 | // Older Version support this hook. |
| 429 | add_filter('woocommerce_cart_item_price_html', array($this->pricingRules, 'replaceVisiblePricesCart'), 1000, 3); |
| 430 | |
| 431 | //For changing the sale tag text |
| 432 | add_filter( 'woocommerce_sale_flash', array($this->pricingRules, 'replaceSaleTagText'), 100, 3); |
| 433 | |
| 434 | // Pricing Table of Individual Product. |
| 435 | $discount_table_placement = $this->discountBase->getConfigData('discount_table_placement', 'before_cart_form'); |
| 436 | if($discount_table_placement == 'before_cart_form'){ |
| 437 | add_filter('woocommerce_before_add_to_cart_form', array($this->pricingRules, 'priceTable')); |
| 438 | add_filter('woocommerce_before_add_to_cart_form', array($this, 'script_on_product_page')); |
| 439 | } else { |
| 440 | add_filter('woocommerce_after_add_to_cart_form', array($this->pricingRules, 'priceTable')); |
| 441 | add_filter('woocommerce_after_add_to_cart_form', array($this, 'script_on_product_page')); |
| 442 | } |
| 443 | |
| 444 | // Updating Log After Creating Order |
| 445 | add_action('woocommerce_thankyou', array($this->discountBase, 'storeLog')); |
| 446 | |
| 447 | add_action( 'woocommerce_after_checkout_form', array($this->discountBase, 'addScriptInCheckoutPage')); |
| 448 | |
| 449 | //To enable on-sale tag |
| 450 | add_filter('woocommerce_product_is_on_sale', array($this->pricingRules, 'displayProductIsOnSaleTagOptimized'), 10, 2); |
| 451 | |
| 452 | $force_refresh_cart_widget = $this->discountBase->getConfigData('force_refresh_cart_widget', 0); |
| 453 | if($force_refresh_cart_widget){ |
| 454 | if (isset($_REQUEST['wc-ajax']) && ($_REQUEST['wc-ajax'] == 'add_to_cart' || $_REQUEST['wc-ajax'] == 'remove_from_cart')) { |
| 455 | add_action('woocommerce_before_mini_cart', array($this, 'applyRulesBeforeMiniCart'), 10); |
| 456 | } |
| 457 | // Refresh the cart when a coupon applied |
| 458 | add_action('woocommerce_applied_coupon', function (){ |
| 459 | add_action('woocommerce_before_cart', function (){ |
| 460 | ?> |
| 461 | <script type="text/javascript"> |
| 462 | jQuery( document ).ready(function() { |
| 463 | jQuery("[name='update_cart']").removeAttr('disabled'); |
| 464 | jQuery("[name='update_cart']").trigger("click"); |
| 465 | }); |
| 466 | </script> |
| 467 | <?php |
| 468 | }); |
| 469 | }); |
| 470 | } |
| 471 | |
| 472 | add_action('wp_ajax_loadWooDiscountStrikeoutPriceOfProduct', array($this->pricingRules, 'getWooDiscountStrikeoutPriceOfProduct')); |
| 473 | add_action('wp_ajax_nopriv_loadWooDiscountStrikeoutPriceOfProduct', array($this->pricingRules, 'getWooDiscountStrikeoutPriceOfProduct')); |
| 474 | |
| 475 | add_action('wp_ajax_loadWooDiscountedPriceForVariant', array($this->pricingRules, 'getWooDiscountedPriceForVariant')); |
| 476 | add_action('wp_ajax_nopriv_loadWooDiscountedPriceForVariant', array($this->pricingRules, 'getWooDiscountedPriceForVariant')); |
| 477 | add_action('wp_ajax_loadWooDiscountedDiscountTable', array($this->pricingRules, 'getWooDiscountedPriceTableForVariant')); |
| 478 | add_action('wp_ajax_nopriv_loadWooDiscountedDiscountTable', array($this->pricingRules, 'getWooDiscountedPriceTableForVariant')); |
| 479 | add_action( 'wp_enqueue_scripts', array($this, 'includeScriptAndStyles') ); |
| 480 | |
| 481 | add_action('woocommerce_before_checkout_form', array($this, 'displayAppliedDiscountMessagesForPriceRules')); |
| 482 | add_action('woocommerce_before_checkout_form', array($this, 'displayAppliedDiscountMessagesForCartRules')); |
| 483 | add_action('woocommerce_before_cart', array($this, 'displayAppliedDiscountMessagesForPriceRules')); |
| 484 | add_action('woocommerce_before_cart', array($this, 'displayAppliedDiscountMessagesForCartRules')); |
| 485 | |
| 486 | add_filter('woo_discount_rule_products_to_exclude', array($this, 'woo_discount_get_variations'), 3, 10); |
| 487 | add_filter('woo_discount_rule_products_to_include', array($this, 'woo_discount_get_variations'), 3, 10); |
| 488 | |
| 489 | $force_customize_sale_tag = $this->discountBase->getConfigData('force_customize_sale_tag', 0); |
| 490 | if($force_customize_sale_tag){ |
| 491 | add_action( "wp_loaded", array( 'FlycartWooDiscountBase', 'removeHooksSetByOtherPlugins' ) ); |
| 492 | // change template of sale tag |
| 493 | add_filter('wc_get_template', array( 'FlycartWooDiscountBase', 'changeTemplateForSaleTag'), 10, 5); |
| 494 | } |
| 495 | |
| 496 | add_filter('woocommerce_get_shop_coupon_data', array('FlycartWooDiscountRulesGeneralHelper', 'addVirtualCoupon'), 9, 2); |
| 497 | |
| 498 | $show_promotion_messages = $this->discountBase->getConfigData('show_promotion_messages', array()); |
| 499 | if(!empty($show_promotion_messages) && is_array($show_promotion_messages)){ |
| 500 | if(in_array('shop_page', $show_promotion_messages)){ |
| 501 | add_action('woocommerce_before_shop_loop', array('FlycartWooDiscountRulesGeneralHelper', 'displayPromotionMessages'), 10); |
| 502 | } |
| 503 | if(in_array('product_page', $show_promotion_messages)){ |
| 504 | add_action('woocommerce_before_single_product', array('FlycartWooDiscountRulesGeneralHelper', 'displayPromotionMessages'), 10); |
| 505 | } |
| 506 | if(in_array('cart_page', $show_promotion_messages)){ |
| 507 | add_action('woocommerce_before_cart', array('FlycartWooDiscountRulesGeneralHelper', 'displayPromotionMessages'), 10); |
| 508 | } |
| 509 | if(in_array('checkout_page', $show_promotion_messages)){ |
| 510 | add_action('woocommerce_before_checkout_form', array('FlycartWooDiscountRulesGeneralHelper', 'displayPromotionMessagesInCheckoutContainer'), 10); |
| 511 | add_action('woocommerce_review_order_before_cart_contents', array('FlycartWooDiscountRulesGeneralHelper', 'displayPromotionMessagesInCheckout'), 10); |
| 512 | } |
| 513 | } |
| 514 | add_action('woo_discount_rules_load_promotion_messages', array('FlycartWooDiscountRulesGeneralHelper', 'displayPromotionMessages')); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Add free product on change checkout fields |
| 519 | * */ |
| 520 | public function add_free_product_on_change_checkout_fields() |
| 521 | { |
| 522 | add_action('woocommerce_cart_loaded_from_session', array($this->pricingRules, 'handleBOGODiscountAfterApplyCoupon'), 10); |
| 523 | remove_action('woocommerce_after_cart_item_quantity_update', array($this->discountBase, 'handleDiscount'), 100); |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Include the variant product as well while choose parent product |
| 528 | * |
| 529 | * @param array $excluded_products |
| 530 | * @return array |
| 531 | * */ |
| 532 | public function woo_discount_get_variations($excluded_products = array(), $rule, $variants = null) { |
| 533 | $include_variants_on_select_parent_product = $this->discountBase->getConfigData('include_variants_on_select_parent_product', 0); |
| 534 | if($include_variants_on_select_parent_product){ |
| 535 | // Load from Rules if we already saved with rules |
| 536 | if($variants !== null && is_array($variants)){ |
| 537 | if(!empty($variants)){ |
| 538 | $excluded_products = array_merge($excluded_products, $variants); |
| 539 | } |
| 540 | return $excluded_products; |
| 541 | } |
| 542 | static $sets; |
| 543 | if (!is_array($sets)) { |
| 544 | $sets = array(); |
| 545 | } |
| 546 | |
| 547 | if(count($excluded_products) < 1) return $excluded_products; |
| 548 | $string = json_encode($excluded_products); |
| 549 | |
| 550 | if (!isset($sets[$string])) { |
| 551 | $all_excluded_products = $excluded_products; |
| 552 | foreach ($excluded_products as $exclude_id) { |
| 553 | if(isset(self::$product_variations[$exclude_id])){} else { |
| 554 | $product = FlycartWoocommerceProduct::wc_get_product($exclude_id); |
| 555 | if (is_object($product) && method_exists($product, 'get_type') && $product->get_type() == 'variable') { |
| 556 | self::$product_variations[$exclude_id] = $children_ids = FlycartWoocommerceProduct::get_children($product);//$product->get_children(); |
| 557 | //$all_excluded_products = array_merge($all_excluded_products, $children_ids); |
| 558 | } |
| 559 | } |
| 560 | if(isset(self::$product_variations[$exclude_id])){ |
| 561 | if(!empty(self::$product_variations[$exclude_id]) && is_array(self::$product_variations[$exclude_id])){ |
| 562 | $all_excluded_products = array_merge($all_excluded_products, self::$product_variations[$exclude_id]); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | $all_excluded_products = array_unique($all_excluded_products); |
| 567 | $sets[$string] = $all_excluded_products; |
| 568 | } |
| 569 | |
| 570 | return $sets[$string]; |
| 571 | } else { |
| 572 | return $excluded_products; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * To include the styles |
| 578 | * */ |
| 579 | public function includeScriptAndStyles(){ |
| 580 | wp_register_style('woo_discount_rules_front_end', WOO_DISCOUNT_URI . '/assets/css/woo_discount_rules.css', array(), WOO_DISCOUNT_VERSION); |
| 581 | wp_enqueue_style('woo_discount_rules_front_end'); |
| 582 | // Enqueued script with localized data. |
| 583 | wp_register_script( 'woo_discount_rules_site', WOO_DISCOUNT_URI . '/assets/js/woo_discount_rules.js', array('jquery'), WOO_DISCOUNT_VERSION, true ); |
| 584 | wp_localize_script('woo_discount_rules_site', 'woo_discount_rules', array( |
| 585 | 'home_url' => get_home_url(), |
| 586 | 'admin_url' => admin_url(), |
| 587 | 'ajax_url' => admin_url('admin-ajax.php'), |
| 588 | 'show_product_strikeout' => $this->discountBase->getConfigData('show_price_discount_on_product_page', 'show') |
| 589 | )); |
| 590 | wp_enqueue_script( 'woo_discount_rules_site'); |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * To load the dynamic data in mini-cart/cart widget while add to cart and remove from cart through widget |
| 595 | * */ |
| 596 | public function applyRulesBeforeMiniCart(){ |
| 597 | WC()->cart->get_cart_from_session(); |
| 598 | $this->discountBase->handlePriceDiscount(); |
| 599 | WC()->cart->calculate_totals(); |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * To display applied discount messages for cart rules |
| 604 | * */ |
| 605 | public function displayAppliedDiscountMessagesForCartRules(){ |
| 606 | $message_on_apply_cart_discount = $this->discountBase->getConfigData('message_on_apply_cart_discount', 'no'); |
| 607 | if($message_on_apply_cart_discount == "yes"){ |
| 608 | if(!empty($this->cart_rules)){ |
| 609 | if(!empty($this->cart_rules->matched_discounts)){ |
| 610 | $matched_discounts = $this->cart_rules->matched_discounts; |
| 611 | if(!empty($matched_discounts['name'])){ |
| 612 | foreach ($matched_discounts['name'] as $key => $matched_discount_name){ |
| 613 | $rule_sets = $this->cart_rules->rule_sets; |
| 614 | $rule_title = $matched_discount_name; |
| 615 | $rule_description = ''; |
| 616 | if(isset($rule_sets[$key])){ |
| 617 | if(!empty($rule_sets[$key]['descr'])) $rule_description = $rule_sets[$key]['descr']; |
| 618 | } |
| 619 | $message_on_apply_cart_discount_text = $this->discountBase->getConfigData('message_on_apply_cart_discount_text', 'Discount <strong>"{{title}}"</strong> has been applied to your cart.'); |
| 620 | $message_on_apply_cart_discount_text = __($message_on_apply_cart_discount_text, 'woo-discount-rules'); |
| 621 | $message_on_apply_cart_discount_text = str_replace('{{title}}', $rule_title, $message_on_apply_cart_discount_text); |
| 622 | $message_on_apply_cart_discount_text = str_replace('{{description}}', $rule_description, $message_on_apply_cart_discount_text); |
| 623 | wc_print_notice( apply_filters('woo_discount_rules_message_on_apply_cart_rules', $message_on_apply_cart_discount_text, $rule_sets), 'success' ); |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * To display applied discount messages for cart rules |
| 633 | * */ |
| 634 | public function displayAppliedDiscountMessagesForPriceRules(){ |
| 635 | $message_on_apply_price_discount = $this->discountBase->getConfigData('message_on_apply_price_discount', 'no'); |
| 636 | if($message_on_apply_price_discount == "yes"){ |
| 637 | $applied_discount_rules = FlycartWooDiscountRulesPricingRules::$applied_discount_rules; |
| 638 | if(!empty($applied_discount_rules)){ |
| 639 | foreach ($applied_discount_rules as $key => $matched_discount_rules){ |
| 640 | $rule_title = $matched_discount_rules['name']; |
| 641 | $rule_description = $matched_discount_rules['descr']; |
| 642 | $message_on_apply_cart_discount_text = $this->discountBase->getConfigData('message_on_apply_price_discount_text', 'Discount <strong>"{{title}}"</strong> has been applied to your cart.'); |
| 643 | $message_on_apply_cart_discount_text = __($message_on_apply_cart_discount_text, 'woo-discount-rules'); |
| 644 | $message_on_apply_cart_discount_text = str_replace('{{title}}', $rule_title, $message_on_apply_cart_discount_text); |
| 645 | $message_on_apply_cart_discount_text = str_replace('{{description}}', $rule_description, $message_on_apply_cart_discount_text); |
| 646 | wc_print_notice( apply_filters('woo_discount_rules_message_on_apply_price_rules', $message_on_apply_cart_discount_text, $matched_discount_rules), 'success' ); |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | add_filter('woocommerce_screen_ids', function($screen_ids){ |
| 655 | $screen_ids[] = 'woocommerce_page_woo_discount_rules'; |
| 656 | return $screen_ids; |
| 657 | }); |
| 658 | |
| 659 | /** |
| 660 | * init Woo Discount Rules |
| 661 | */ |
| 662 | if ( FlycartWooDiscountRulesActivationHelper::isWooCommerceActive() ) { |
| 663 | global $flycart_woo_discount_rules; |
| 664 | $flycart_woo_discount_rules = FlycartWooDiscountRules::init(); |
| 665 | $purchase_helper = new FlycartWooDiscountRulesPurchase(); |
| 666 | if($purchase_helper->isPro()){ |
| 667 | include_once(dirname(__FILE__).'/includes/advanced/free_shipping_method.php'); |
| 668 | include_once(dirname(__FILE__).'/includes/advanced/pricing-productdependent.php'); |
| 669 | include_once(dirname(__FILE__).'/includes/advanced/cart-totals.php'); |
| 670 | include_once(dirname(__FILE__).'/includes/advanced/advanced-helper.php'); |
| 671 | } |
| 672 | } |