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
285 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_loadProductSelectBox', array($this->discountBase, 'loadProductSelectBox')); |
| 113 | |
| 114 | add_action('wp_ajax_UpdateStatus', array($this->discountBase, 'updateStatus')); |
| 115 | add_action('wp_ajax_RemoveRule', array($this->discountBase, 'removeRule')); |
| 116 | add_action('wp_ajax_doBulkAction', array($this->discountBase, 'doBulkAction')); |
| 117 | add_action('wp_ajax_createDuplicateRule', array($this->discountBase, 'createDuplicateRule')); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Apply discount rules |
| 122 | * */ |
| 123 | public function applyDiscountRules(){ |
| 124 | $this->discountBase->handlePriceDiscount(); |
| 125 | remove_action('woocommerce_before_calculate_totals', array($this, 'applyDiscountRules'), 1000); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Apply discount rules |
| 130 | * */ |
| 131 | public function applyCartDiscountRules(){ |
| 132 | $this->discountBase->handleCartDiscount(); |
| 133 | remove_action('woocommerce_cart_loaded_from_session', array($this, 'applyCartDiscountRules'), 97); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Script on product page for loading variant strikeout |
| 138 | * */ |
| 139 | public function script_on_product_page(){ |
| 140 | $script = '<script>'; |
| 141 | $script .= 'jQuery( document ).ready( function() {'; |
| 142 | $do_product_page_strikeout = $this->discountBase->getConfigData('show_price_discount_on_product_page', 'dont'); |
| 143 | if($do_product_page_strikeout == 'show') { |
| 144 | $script .= ' jQuery( ".single_variation_wrap" ).on( "show_variation", function ( event, variation, purchasable ) {'; |
| 145 | $script .= ' var container = jQuery(".single_variation .woocommerce-variation-price");'; |
| 146 | $script .= ' container.hide("slow");'; |
| 147 | $script .= ' jQuery.ajax({ |
| 148 | url: wc_add_to_cart_params.ajax_url, |
| 149 | dataType: "json", |
| 150 | type: "POST", |
| 151 | data: {action: "loadWooDiscountedPriceForVariant", id: variation.variation_id, price_html: variation.price_html}, |
| 152 | beforeSend: function() { |
| 153 | }, |
| 154 | complete: function() { |
| 155 | }, |
| 156 | success: function (response) { |
| 157 | if(response.status == 1){ |
| 158 | jQuery(".single_variation .woocommerce-variation-price").html(response.price_html); |
| 159 | } |
| 160 | container.show("slow"); |
| 161 | } |
| 162 | });'; |
| 163 | $script .= ' });'; |
| 164 | } |
| 165 | $script .= ' if(jQuery(".woo_discount_rules_variant_table").length > 0){ |
| 166 | var p_id = jQuery( ".woo_discount_rules_variant_table" ).attr("data-id");'; |
| 167 | $script .= ' jQuery.ajax({ |
| 168 | url: wc_add_to_cart_params.ajax_url, |
| 169 | type: "POST", |
| 170 | data: {action: "loadWooDiscountedDiscountTable", id: p_id}, |
| 171 | beforeSend: function() { |
| 172 | }, |
| 173 | complete: function() { |
| 174 | }, |
| 175 | success: function (response) { |
| 176 | jQuery(".woo_discount_rules_variant_table").html(response); |
| 177 | } |
| 178 | });'; |
| 179 | $script .= ' }'; |
| 180 | $script .= '});'; |
| 181 | $script .= '</script>'; |
| 182 | |
| 183 | echo $script; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Load Admin scripts |
| 188 | * */ |
| 189 | protected function loadSiteScripts(){ |
| 190 | $woocommerce_version = '2.0.0'; |
| 191 | $pluginDetails = get_plugin_data(WP_PLUGIN_DIR.'/woocommerce/woocommerce.php'); |
| 192 | if(isset($pluginDetails['Version'])){ |
| 193 | $woocommerce_version = $pluginDetails['Version']; |
| 194 | } |
| 195 | |
| 196 | add_action('woocommerce_after_cart_item_quantity_update', array($this->discountBase, 'handleDiscount'), 100); |
| 197 | if(version_compare($woocommerce_version, '3.0', '>=')){ |
| 198 | add_action('woocommerce_before_calculate_totals', array($this, 'applyDiscountRules'), 1000); |
| 199 | add_action('woocommerce_cart_loaded_from_session', array($this, 'applyCartDiscountRules'), 97); |
| 200 | add_action( 'woocommerce_after_cart_item_quantity_update', array($this->pricingRules, 'handleBOGODiscountOnUpdateQuantity'), 10, 4 ); |
| 201 | } else { |
| 202 | add_action( 'woocommerce_after_cart_item_quantity_update', array($this->pricingRules, 'handleBOGODiscountOnUpdateQuantity'), 10, 3 ); |
| 203 | add_action('woocommerce_cart_loaded_from_session', array($this->discountBase, 'handleDiscount'), 100); |
| 204 | } |
| 205 | |
| 206 | add_action('woocommerce_add_to_cart', array($this->pricingRules, 'handleBOGODiscount'), 10, 6); |
| 207 | |
| 208 | |
| 209 | // Manually Update Line Item Name. |
| 210 | add_filter('woocommerce_cart_item_name', array($this->discountBase, 'modifyName')); |
| 211 | |
| 212 | // Remove Filter to make the previous one as last filter. |
| 213 | remove_filter('woocommerce_cart_item_name', 'filter_woocommerce_cart_item_name', 10, 3); |
| 214 | |
| 215 | // Alter the Display Price HTML. |
| 216 | add_filter('woocommerce_cart_item_price', array($this->pricingRules, 'replaceVisiblePricesCart'), 1000, 3); |
| 217 | |
| 218 | //replace visible price in product page |
| 219 | add_filter('woocommerce_get_price_html', array($this->pricingRules, 'replaceVisiblePricesOptimized'), 100, 3); |
| 220 | //replace visible price in product page for variant |
| 221 | add_filter('woocommerce_available_variation', array($this->pricingRules, 'replaceVisiblePricesForVariant'), 100, 3); |
| 222 | |
| 223 | // Older Version support this hook. |
| 224 | add_filter('woocommerce_cart_item_price_html', array($this->pricingRules, 'replaceVisiblePricesCart'), 1000, 3); |
| 225 | |
| 226 | // Pricing Table of Individual Product. |
| 227 | add_filter('woocommerce_before_add_to_cart_form', array($this->pricingRules, 'priceTable')); |
| 228 | add_filter('woocommerce_before_add_to_cart_form', array($this, 'script_on_product_page')); |
| 229 | |
| 230 | // Updating Log After Creating Order |
| 231 | add_action('woocommerce_thankyou', array($this->discountBase, 'storeLog')); |
| 232 | |
| 233 | add_action( 'woocommerce_after_checkout_form', array($this->discountBase, 'addScriptInCheckoutPage')); |
| 234 | |
| 235 | //To enable on-sale tag |
| 236 | add_filter('woocommerce_product_is_on_sale', array($this->pricingRules, 'displayProductIsOnSaleTagOptimized'), 10, 2); |
| 237 | |
| 238 | $force_refresh_cart_widget = $this->discountBase->getConfigData('force_refresh_cart_widget', 0); |
| 239 | if($force_refresh_cart_widget){ |
| 240 | if (isset($_REQUEST['wc-ajax']) && ($_REQUEST['wc-ajax'] == 'add_to_cart' || $_REQUEST['wc-ajax'] == 'remove_from_cart')) { |
| 241 | add_action('woocommerce_before_mini_cart', array($this, 'applyRulesBeforeMiniCart'), 10); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | add_action('wp_ajax_loadWooDiscountedPriceForVariant', array($this->pricingRules, 'getWooDiscountedPriceForVariant')); |
| 246 | add_action('wp_ajax_nopriv_loadWooDiscountedPriceForVariant', array($this->pricingRules, 'getWooDiscountedPriceForVariant')); |
| 247 | add_action('wp_ajax_loadWooDiscountedDiscountTable', array($this->pricingRules, 'getWooDiscountedPriceTableForVariant')); |
| 248 | add_action('wp_ajax_nopriv_loadWooDiscountedDiscountTable', array($this->pricingRules, 'getWooDiscountedPriceTableForVariant')); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * To load the dynamic data in mini-cart/cart widget while add to cart and remove from cart through widget |
| 253 | * */ |
| 254 | public function applyRulesBeforeMiniCart(){ |
| 255 | WC()->cart->get_cart_from_session(); |
| 256 | $this->discountBase->handlePriceDiscount(); |
| 257 | WC()->cart->calculate_totals(); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * init Woo Discount Rules |
| 264 | */ |
| 265 | if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) { |
| 266 | global $flycart_woo_discount_rules; |
| 267 | $flycart_woo_discount_rules = FlycartWooDiscountRules::init(); |
| 268 | $purchase_helper = new FlycartWooDiscountRulesPurchase(); |
| 269 | if($purchase_helper->isPro()){ |
| 270 | include_once('includes/advanced/free_shipping_method.php'); |
| 271 | include_once('includes/advanced/pricing-productdependent.php'); |
| 272 | include_once('includes/advanced/cart-totals.php'); |
| 273 | include_once('includes/advanced/advanced-helper.php'); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | add_filter( 'woocommerce_add_to_cart_fragments', 'iconic_cart_count_fragments', 10, 1 ); |
| 278 | |
| 279 | function iconic_cart_count_fragments( $fragments ) { |
| 280 | |
| 281 | $fragments['div.header-cart-count'] = '<div class="header-cart-count">' . WC()->cart->get_cart_contents_count() . '</div>'; |
| 282 | |
| 283 | return $fragments; |
| 284 | |
| 285 | } |