woo-discount-rules
Last commit date
assets
8 years ago
helper
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
196 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 | |
| 36 | /** |
| 37 | * To run the plugin |
| 38 | * */ |
| 39 | public static function init() { |
| 40 | if ( self::$instance == null ) { |
| 41 | self::$instance = new FlycartWooDiscountRules(); |
| 42 | } |
| 43 | return self::$instance; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * FlycartWooDiscountRules constructor |
| 48 | * */ |
| 49 | public function __construct() { |
| 50 | $this->includeFiles(); |
| 51 | $this->runUpdater(); |
| 52 | $this->discountBase = new FlycartWooDiscountBase(); |
| 53 | $this->pricingRules = new FlycartWooDiscountRulesPricingRules(); |
| 54 | if (is_admin()) { |
| 55 | $this->loadAdminScripts(); |
| 56 | } |
| 57 | if(FlycartWooDiscountRulesGeneralHelper::doIHaveToRun()){ |
| 58 | $this->loadSiteScripts(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * To include Files |
| 64 | * */ |
| 65 | protected function includeFiles(){ |
| 66 | include_once('helper/woo-function.php'); |
| 67 | include_once('includes/pricing-rules.php'); |
| 68 | include_once('helper/general-helper.php'); |
| 69 | include_once('includes/cart-rules.php'); |
| 70 | include_once('includes/discount-base.php'); |
| 71 | include_once('helper/purchase.php'); |
| 72 | require_once __DIR__ . '/vendor/autoload.php'; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Run Plugin updater |
| 77 | * */ |
| 78 | protected function runUpdater(){ |
| 79 | try{ |
| 80 | require plugin_dir_path( __FILE__ ).'/vendor/yahnis-elsts/plugin-update-checker/plugin-update-checker.php'; |
| 81 | |
| 82 | $purchase_helper = new FlycartWooDiscountRulesPurchase(); |
| 83 | $purchase_helper->init(); |
| 84 | $update_url = $purchase_helper->getUpdateURL(); |
| 85 | |
| 86 | $myUpdateChecker = Puc_v4_Factory::buildUpdateChecker( |
| 87 | $update_url, |
| 88 | plugin_dir_path( __FILE__ ).'woo-discount-rules.php', |
| 89 | 'woo-discount-rules' |
| 90 | ); |
| 91 | add_action( 'after_plugin_row', array($purchase_helper, 'woodisc_after_plugin_row'),10,3 ); |
| 92 | |
| 93 | add_action('wp_ajax_forceValidateLicenseKey', array($purchase_helper, 'forceValidateLicenseKey')); |
| 94 | |
| 95 | add_action( 'admin_notices', array($purchase_helper, 'errorNoticeInAdminPages')); |
| 96 | } catch (Exception $e){} |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Load Admin scripts |
| 101 | * */ |
| 102 | protected function loadAdminScripts(){ |
| 103 | // Init in Admin Menu |
| 104 | add_action('admin_menu', array($this->discountBase, 'adminMenu')); |
| 105 | add_action('wp_ajax_savePriceRule', array($this->discountBase, 'savePriceRule')); |
| 106 | add_action('wp_ajax_saveCartRule', array($this->discountBase, 'saveCartRule')); |
| 107 | add_action('wp_ajax_saveConfig', array($this->discountBase, 'saveConfig')); |
| 108 | |
| 109 | add_action('wp_ajax_UpdateStatus', array($this->discountBase, 'updateStatus')); |
| 110 | add_action('wp_ajax_RemoveRule', array($this->discountBase, 'removeRule')); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Apply discount rules |
| 115 | * */ |
| 116 | public function applyDiscountRules(){ |
| 117 | $this->discountBase->handlePriceDiscount(); |
| 118 | remove_action('woocommerce_before_calculate_totals', array($this, 'applyDiscountRules'), 1000); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Apply discount rules |
| 123 | * */ |
| 124 | public function applyCartDiscountRules(){ |
| 125 | $this->discountBase->handleCartDiscount(); |
| 126 | remove_action('woocommerce_cart_loaded_from_session', array($this, 'applyCartDiscountRules'), 100); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Load Admin scripts |
| 131 | * */ |
| 132 | protected function loadSiteScripts(){ |
| 133 | $woocommerce_version = '2.0.0'; |
| 134 | $pluginDetails = get_plugin_data(WP_PLUGIN_DIR.'/woocommerce/woocommerce.php'); |
| 135 | if(isset($pluginDetails['Version'])){ |
| 136 | $woocommerce_version = $pluginDetails['Version']; |
| 137 | } |
| 138 | |
| 139 | $postData = \FlycartInput\FInput::getInstance(); |
| 140 | // Handling Tight update with wooCommerce Changes. |
| 141 | $empty_add_to_cart = $postData->get('add-to-cart'); |
| 142 | $empty_apply_coupon = $postData->get('apply_coupon'); |
| 143 | $empty_update_cart = $postData->get('update_cart'); |
| 144 | $empty_proceed = $postData->get('proceed'); |
| 145 | if ((!empty($empty_add_to_cart) && is_numeric($postData->get('add-to-cart'))) || $postData->get('action', false) == 'woocommerce_add_to_cart') { |
| 146 | |
| 147 | } else if (!empty($empty_apply_coupon) || !empty($empty_update_cart) || !empty($empty_proceed)) { |
| 148 | add_action('woocommerce_after_cart_item_quantity_update', array($this->discountBase, 'handleDiscount'), 100); |
| 149 | } else { |
| 150 | if(version_compare($woocommerce_version, '3.0', '>=')){ |
| 151 | add_action('woocommerce_before_calculate_totals', array($this, 'applyDiscountRules'), 1000); |
| 152 | add_action('woocommerce_cart_loaded_from_session', array($this, 'applyCartDiscountRules'), 100); |
| 153 | } else { |
| 154 | add_action('woocommerce_cart_loaded_from_session', array($this->discountBase, 'handleDiscount'), 100); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Manually Update Line Item Name. |
| 159 | add_filter('woocommerce_cart_item_name', array($this->discountBase, 'modifyName')); |
| 160 | |
| 161 | // Remove Filter to make the previous one as last filter. |
| 162 | remove_filter('woocommerce_cart_item_name', 'filter_woocommerce_cart_item_name', 10, 3); |
| 163 | |
| 164 | // Alter the Display Price HTML. |
| 165 | add_filter('woocommerce_cart_item_price', array($this->pricingRules, 'replaceVisiblePricesCart'), 100, 3); |
| 166 | |
| 167 | //replace visible price in product page |
| 168 | add_filter('woocommerce_get_price_html', array($this->pricingRules, 'replaceVisiblePrices'), 100, 3); |
| 169 | //replace visible price in product page for variant |
| 170 | add_filter('woocommerce_available_variation', array($this->pricingRules, 'replaceVisiblePricesForVariant'), 100, 3); |
| 171 | |
| 172 | |
| 173 | // Older Version support this hook. |
| 174 | add_filter('woocommerce_cart_item_price_html', array($this->pricingRules, 'replaceVisiblePricesCart'), 100, 3); |
| 175 | |
| 176 | // Pricing Table of Individual Product. |
| 177 | add_filter('woocommerce_before_add_to_cart_form', array($this->pricingRules, 'priceTable')); |
| 178 | |
| 179 | // Updating Log After Creating Order |
| 180 | add_action('woocommerce_thankyou', array($this->discountBase, 'storeLog')); |
| 181 | |
| 182 | add_action( 'woocommerce_after_checkout_form', array($this->discountBase, 'addScriptInCheckoutPage')); |
| 183 | |
| 184 | //To enable on-sale tag |
| 185 | add_filter('woocommerce_product_is_on_sale', array($this->pricingRules, 'displayProductIsOnSaleTag'), 10, 2); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * init Woo Discount Rules |
| 192 | */ |
| 193 | if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) { |
| 194 | FlycartWooDiscountRules::init(); |
| 195 | } |
| 196 |