woo-discount-rules
Last commit date
assets
8 years ago
helper
8 years ago
i18n
8 years ago
includes
8 years ago
vendor
8 years ago
view
8 years ago
loader.php
8 years ago
readme.txt
8 years ago
woo-discount-rules.php
8 years ago
loader.php
219 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 | } |
| 117 | |
| 118 | /** |
| 119 | * Apply discount rules |
| 120 | * */ |
| 121 | public function applyDiscountRules(){ |
| 122 | $this->discountBase->handlePriceDiscount(); |
| 123 | remove_action('woocommerce_before_calculate_totals', array($this, 'applyDiscountRules'), 1000); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Apply discount rules |
| 128 | * */ |
| 129 | public function applyCartDiscountRules(){ |
| 130 | $this->discountBase->handleCartDiscount(); |
| 131 | remove_action('woocommerce_cart_loaded_from_session', array($this, 'applyCartDiscountRules'), 100); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Load Admin scripts |
| 136 | * */ |
| 137 | protected function loadSiteScripts(){ |
| 138 | $woocommerce_version = '2.0.0'; |
| 139 | $pluginDetails = get_plugin_data(WP_PLUGIN_DIR.'/woocommerce/woocommerce.php'); |
| 140 | if(isset($pluginDetails['Version'])){ |
| 141 | $woocommerce_version = $pluginDetails['Version']; |
| 142 | } |
| 143 | |
| 144 | add_action('woocommerce_after_cart_item_quantity_update', array($this->discountBase, 'handleDiscount'), 100); |
| 145 | if(version_compare($woocommerce_version, '3.0', '>=')){ |
| 146 | add_action('woocommerce_before_calculate_totals', array($this, 'applyDiscountRules'), 1000); |
| 147 | add_action('woocommerce_cart_loaded_from_session', array($this, 'applyCartDiscountRules'), 100); |
| 148 | add_action( 'woocommerce_after_cart_item_quantity_update', array($this->pricingRules, 'handleBOGODiscountOnUpdateQuantity'), 10, 4 ); |
| 149 | } else { |
| 150 | add_action( 'woocommerce_after_cart_item_quantity_update', array($this->pricingRules, 'handleBOGODiscountOnUpdateQuantity'), 10, 3 ); |
| 151 | add_action('woocommerce_cart_loaded_from_session', array($this->discountBase, 'handleDiscount'), 100); |
| 152 | } |
| 153 | |
| 154 | add_action('woocommerce_add_to_cart', array($this->pricingRules, 'handleBOGODiscount'), 10, 6); |
| 155 | |
| 156 | |
| 157 | // Manually Update Line Item Name. |
| 158 | add_filter('woocommerce_cart_item_name', array($this->discountBase, 'modifyName')); |
| 159 | |
| 160 | // Remove Filter to make the previous one as last filter. |
| 161 | remove_filter('woocommerce_cart_item_name', 'filter_woocommerce_cart_item_name', 10, 3); |
| 162 | |
| 163 | // Alter the Display Price HTML. |
| 164 | add_filter('woocommerce_cart_item_price', array($this->pricingRules, 'replaceVisiblePricesCart'), 1000, 3); |
| 165 | |
| 166 | //replace visible price in product page |
| 167 | add_filter('woocommerce_get_price_html', array($this->pricingRules, 'replaceVisiblePrices'), 100, 3); |
| 168 | //replace visible price in product page for variant |
| 169 | add_filter('woocommerce_available_variation', array($this->pricingRules, 'replaceVisiblePricesForVariant'), 100, 3); |
| 170 | |
| 171 | |
| 172 | // Older Version support this hook. |
| 173 | add_filter('woocommerce_cart_item_price_html', array($this->pricingRules, 'replaceVisiblePricesCart'), 1000, 3); |
| 174 | |
| 175 | // Pricing Table of Individual Product. |
| 176 | add_filter('woocommerce_before_add_to_cart_form', array($this->pricingRules, 'priceTable')); |
| 177 | |
| 178 | // Updating Log After Creating Order |
| 179 | add_action('woocommerce_thankyou', array($this->discountBase, 'storeLog')); |
| 180 | |
| 181 | add_action( 'woocommerce_after_checkout_form', array($this->discountBase, 'addScriptInCheckoutPage')); |
| 182 | |
| 183 | //To enable on-sale tag |
| 184 | add_filter('woocommerce_product_is_on_sale', array($this->pricingRules, 'displayProductIsOnSaleTag'), 10, 2); |
| 185 | |
| 186 | $force_refresh_cart_widget = $this->discountBase->getConfigData('force_refresh_cart_widget', 0); |
| 187 | if($force_refresh_cart_widget){ |
| 188 | if (isset($_REQUEST['wc-ajax']) && ($_REQUEST['wc-ajax'] == 'add_to_cart' || $_REQUEST['wc-ajax'] == 'remove_from_cart')) { |
| 189 | add_action('woocommerce_before_mini_cart', array($this, 'applyRulesBeforeMiniCart'), 10); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * To load the dynamic data in mini-cart/cart widget while add to cart and remove from cart through widget |
| 196 | * */ |
| 197 | public function applyRulesBeforeMiniCart(){ |
| 198 | WC()->cart->get_cart_from_session(); |
| 199 | $this->discountBase->handlePriceDiscount(); |
| 200 | WC()->cart->calculate_totals(); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * init Woo Discount Rules |
| 207 | */ |
| 208 | if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) { |
| 209 | global $flycart_woo_discount_rules; |
| 210 | $flycart_woo_discount_rules = FlycartWooDiscountRules::init(); |
| 211 | $purchase_helper = new FlycartWooDiscountRulesPurchase(); |
| 212 | if($purchase_helper->isPro()){ |
| 213 | include_once('includes/advanced/free_shipping_method.php'); |
| 214 | include_once('includes/advanced/pricing-productdependent.php'); |
| 215 | include_once('includes/advanced/cart-totals.php'); |
| 216 | include_once('includes/advanced/advanced-helper.php'); |
| 217 | } |
| 218 | } |
| 219 |