stock-locations-for-woocommerce
Last commit date
app
6 years ago
assets
4 years ago
css
5 months ago
images
3 years ago
inc
3 weeks ago
js
1 month ago
languages
3 years ago
src
3 weeks ago
vendor
5 years ago
views
3 weeks ago
webfonts
4 years ago
composer.json
4 years ago
composer.lock
6 years ago
index.php
4 years ago
readme.txt
3 weeks ago
stock-locations-for-woocommerce.php
3 weeks ago
stock-locations-for-woocommerce.php
657 lines
| 1 | <?php if ( ! defined( 'ABSPATH' ) ){ exit; }else{ clearstatcache(); } |
| 2 | /** |
| 3 | * Plugin Name: Stock Locations for WooCommerce |
| 4 | * Description: This plugin will help you to manage WooCommerce Products stocks through locations. |
| 5 | * Version: 3.1.9 |
| 6 | * Requires at least: 4.9 |
| 7 | * Requires PHP: 7.2 |
| 8 | * Author: Fahad Mahmood & Alexandre Faustino |
| 9 | * Author URI: https://profiles.wordpress.org/fahadmahmood/#content-plugins |
| 10 | * License: GPL v2 or later |
| 11 | * License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 12 | * Text Domain: stock-locations-for-woocommerce |
| 13 | * Domain Path: /languages |
| 14 | * WC requires at least: 3.4 |
| 15 | * WC tested up to: 5.9 |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * If this file is called directly, abort. |
| 20 | * |
| 21 | * @since 1.0.0 |
| 22 | */ |
| 23 | if ( !defined( 'WPINC' ) ) { |
| 24 | die; |
| 25 | } |
| 26 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 27 | |
| 28 | //use Automattic\WooCommerce\Utilities\OrderUtil; |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | global $wc_slw_hpos, $wc_slw_data, $wc_slw_pro, $wc_slw_premium_copy, $slw_plugin_settings, $slw_gkey, $slw_api_valid_keys, $slw_crons_valid_keys, $slw_widgets_arr, $slw_wc_stock_format, $slw_theme_name, $slw_order_id, $slw_logs_status, $slw_woocommerce_product_form_hooks, $slw_wc_hide_out_of_stock, $slw_proceed_order_note, $slw_optional_features_key; |
| 35 | |
| 36 | $slw_crons_valid_keys = array( |
| 37 | 'action' => array('type'=>'string', 'options'=>'update-stock'), |
| 38 | 'limit' => array('type'=>'int', 'options'=>'Default: 10'), |
| 39 | 'reconsider' => array('type'=>'string', 'options'=>'second|minute|hour|day|month|year|once'), |
| 40 | 'product_id' => array('type'=>'int', 'options'=>'Default: 0 <small>(When you need to update just one product.)</small>'), |
| 41 | ); |
| 42 | |
| 43 | //$wc_slw_hpos = OrderUtil::custom_orders_table_usage_is_enabled(); |
| 44 | $slw_optional_features_key = 'slw_optional_features'; |
| 45 | $slw_wc_hide_out_of_stock = get_option( 'woocommerce_hide_out_of_stock_items' ); |
| 46 | $slw_logs_status = (get_option('slw_logs_status')==true); |
| 47 | $slw_wc_stock_format = get_option('woocommerce_stock_format'); |
| 48 | $slw_gkey = get_option('slw-google-api-key'); |
| 49 | $slw_plugin_settings = get_option( 'slw_settings' ); |
| 50 | $slw_plugin_settings = is_array($slw_plugin_settings)?$slw_plugin_settings:array(); |
| 51 | $slw_proceed_order_note = ( isset($slw_plugin_settings['turn_order_notes_off_from_this_plugin']) && $slw_plugin_settings['turn_order_notes_off_from_this_plugin'] == 'yes' ); |
| 52 | $wc_slw_data = get_plugin_data(__FILE__, true, false); |
| 53 | define( 'SLW_PLUGIN_DIR', dirname( __FILE__ ) ); |
| 54 | define( 'SLW_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); |
| 55 | |
| 56 | //$slw_order_id = ((is_admin() && isset($_GET['page']) && $_GET['page']=='wc-orders' && isset($_GET['action']) && $_GET['action']=='edit' && isset($_GET['id']) && is_numeric($_GET['id']))?$_GET['id']:0); |
| 57 | |
| 58 | |
| 59 | $wp_theme_installed = wp_get_theme(); |
| 60 | $slw_theme_name = str_replace(array('-'), '_', esc_html( $wp_theme_installed->get_stylesheet())); |
| 61 | |
| 62 | |
| 63 | $addons_file = realpath(SLW_PLUGIN_DIR . '/inc/addons.php'); |
| 64 | if(file_exists($addons_file)){ |
| 65 | include_once($addons_file); |
| 66 | } |
| 67 | |
| 68 | $wc_slw_premium_copy = 'https://shop.androidbubbles.com/product/stock-locations-for-woocommerce/'; |
| 69 | |
| 70 | $wc_slw_pro_file = realpath(SLW_PLUGIN_DIR . '/pro/functions.php'); |
| 71 | $wc_slw_pro = file_exists($wc_slw_pro_file); |
| 72 | |
| 73 | |
| 74 | if($wc_slw_pro){ |
| 75 | include_once(SLW_PLUGIN_DIR . '/pro/functions.php'); |
| 76 | } |
| 77 | require_once(realpath(SLW_PLUGIN_DIR . '/inc/functions.php')); |
| 78 | |
| 79 | |
| 80 | if(!class_exists('SlwMain')) { |
| 81 | |
| 82 | class SlwMain{ |
| 83 | // versions |
| 84 | public $version = '3.1.9'; |
| 85 | public $import_export_addon_version = '1.1.1'; |
| 86 | |
| 87 | // others |
| 88 | protected static $instance = null; |
| 89 | private $plugin_settings; |
| 90 | |
| 91 | /** |
| 92 | * Class Constructor. |
| 93 | * @since 1.0.0 |
| 94 | */ |
| 95 | public function __construct(){ |
| 96 | |
| 97 | global $slw_woocommerce_product_form_hooks, $slw_api_valid_keys, $slw_widgets_arr; |
| 98 | |
| 99 | define( 'SLW_PLUGIN_VERSION', $this->version ); |
| 100 | |
| 101 | $this->init(); |
| 102 | |
| 103 | add_action('init', function() use (&$slw_woocommerce_product_form_hooks, &$slw_api_valid_keys, &$slw_widgets_arr) { |
| 104 | |
| 105 | $slw_woocommerce_product_form_hooks = [ |
| 106 | '' => __('Select Position', 'stock-locations-for-woocommerce'), |
| 107 | 'woocommerce_before_add_to_cart_button' => __('Before Add to Cart Button', 'stock-locations-for-woocommerce'), |
| 108 | 'woocommerce_before_variations_form' => __('Before Variations Form', 'stock-locations-for-woocommerce'), |
| 109 | 'woocommerce_product_additional_information' => __('Additional Information', 'stock-locations-for-woocommerce'), |
| 110 | 'woocommerce_before_single_variation' => __('Before Single Variation', 'stock-locations-for-woocommerce'), |
| 111 | 'woocommerce_after_single_variation' => __('After Single Variation', 'stock-locations-for-woocommerce'), |
| 112 | 'woocommerce_after_variations_form' => __('After Variations Form', 'stock-locations-for-woocommerce'), |
| 113 | 'woocommerce_after_add_to_cart_button' => __('After Add to Cart Button', 'stock-locations-for-woocommerce') |
| 114 | ]; |
| 115 | |
| 116 | $slw_api_valid_keys = array( |
| 117 | 'id' => array('type'=>'int', 'options'=>'<input type="text" placeholder="'.__('Insert Product ID', 'stock-locations-for-woocommerce').'" class="slw-api-id-input" /> <input type="button" value="Try" class="slw-api-id-try" />', 'tooltip'=>__('When item is a location, so ID is location_id and when item is product so ID is considered as a product_id.', 'stock-locations-for-woocommerce'), 'scope'=>''), |
| 118 | 'value' => array('type'=>'int', 'options'=>'', 'scope'=>''), |
| 119 | 'action' => array('type'=>'string', 'options'=>'get|set', 'scope'=>''), |
| 120 | 'item' => array('type'=>'string', 'options'=>'location|product|stock|price', 'tooltip'=>__('When item is other than location and product so product_id or location_id would be required as a parameter, ID parameter will not be adequate.', 'stock-locations-for-woocommerce'), 'scope'=>''), |
| 121 | 'format' => array('type'=>'string', 'options'=>'json|default', 'scope'=>''), |
| 122 | 'product_id'=>array('type'=>'int', 'options'=>'', 'tooltip'=>__('When item is other than product.', 'stock-locations-for-woocommerce'), 'scope'=>''), |
| 123 | 'location_id'=>array('type'=>'int', 'options'=>'', 'tooltip'=>__('When item is other than location.', 'stock-locations-for-woocommerce'), 'scope'=>''), |
| 124 | 'location'=>array('type'=>'json', 'options'=>'location_id, stock_value', 'tooltip'=>__('For variable products.', 'stock-locations-for-woocommerce'), 'scope'=>'variable'), |
| 125 | 'api_key'=>array('type'=>'string', 'options'=>'<small>'.slw_get_api_key().'</small>'), |
| 126 | ); |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | $slw_widgets_arr = array( |
| 132 | 'slw-map' => array( |
| 133 | 'type' => __('Premium', 'stock-locations-for-woocommerce'), |
| 134 | 'input' => array('name'=>'slw-google-api-key', 'type'=>'text', 'caption'=>__('Please enter Google API key here', 'stock-locations-for-woocommerce')), |
| 135 | 'title' => __('Google Map for Stock Locations', 'stock-locations-for-woocommerce'), |
| 136 | 'description' => __('This widget will detect the user location and zoom to current user latitude longitude by default.', 'stock-locations-for-woocommerce'), |
| 137 | 'shortcode' => array('<strong>Shortcode:</strong><br />[SLW-MAP search-field="yes" locations-list="yes" map="yes" map-width="68%" list-width="400px" diameter-range="100" distance-unit="km" zoom="13" search-field-placeholder="" shop-button-text="Shop This Location" directions-button-text="Directions" shop-location-link="default|shop|previous|store-link"]<br /><br />', '<strong>Hooks:</strong><br />add_action("before_slw_shop_button", function($location_data){ }, 11, 1);', 'add_action("after_slw_shop_button", function($location_data){ }, 11, 1);', 'add_filter("slw-map-location-label", function($label, $name, $location_id){ }, 11, 3);', 'add_filter("slw-map-location-name", function($name, $label, $location_id){ }, 11, 3);', 'add_action("slw-map-before-search-box", function($placeholder){ }, 11, 1);', 'add_action("slw-map-after-search-box", function($placeholder){ }, 11, 1);',), |
| 138 | 'screenshot' => array(SLW_PLUGIN_URL.'images/slw-map-thumb.png', SLW_PLUGIN_URL.'images/slw-map-popup-thumb.png'), |
| 139 | |
| 140 | ), |
| 141 | 'slw-archives' => array( |
| 142 | 'type' => __('Premium', 'stock-locations-for-woocommerce'), |
| 143 | 'input' => array('name'=>'slw-archives-status', 'type'=>'toggle', 'caption'=>''), |
| 144 | 'title' => __('Stock Locations Archive', 'stock-locations-for-woocommerce'), |
| 145 | 'description' => __('This widget will display the product items category wise on location specific archives.', 'stock-locations-for-woocommerce'), |
| 146 | 'shortcode' => array('add_action("<strong>slw_archive_items_below_title</strong>", "yourtheme_archive_items_below_title", 11, 3);','add_action("<strong>slw_archive_items_below_qty</strong>", "yourtheme_archive_items_below_qty", 11, 3);', 'add_filter("<strong>slw_archive_product_image</strong>", "yourtheme_archive_product_image_callback", 11, 2);', 'add_action("<strong>slw_archive_before_wrapper</strong>", "yourtheme_archive_before_wrapper_callback", 11, 1);', 'add_action("<strong>slw_archive_after_wrapper</strong>", "yourtheme_archive_after_wrapper_callback", 11, 1);', 'add_action("<strong>slw-archive-wrapper</strong>", "yourtheme_archive_wrapper_classes", 11, 1);','add_action("<strong>slw_archive_inside_wrapper_start</strong>", "yourtheme_archive_inside_wrapper_start_callback", 11, 3);','add_action("<strong>slw_archive_inside_wrapper_end</strong>", "yourtheme_archive_inside_wrapper_end_callback", 11, 3);<br /><br /><strong>Shortcodes:</strong><br />[slw-archive-meta meta_key="location_address"]'), |
| 147 | 'screenshot' => array(SLW_PLUGIN_URL.'images/slw-archives-thumb.png'), |
| 148 | |
| 149 | |
| 150 | ), |
| 151 | 'slw-location-selection' => array( |
| 152 | 'type' => __('Premium', 'stock-locations-for-woocommerce'), |
| 153 | 'input' => array('name'=>'slw-location-selection', 'type'=>'toggle', 'caption'=>''), |
| 154 | 'title' => __('Stock Locations Selection (Popup)', 'stock-locations-for-woocommerce'), |
| 155 | 'description' => __('This widget will implement a popup with the location names to land on specific location archives.', 'stock-locations-for-woocommerce'), |
| 156 | 'shortcode' => array( |
| 157 | |
| 158 | 'add_filter("<strong>slw_location_selection_popup_content</strong>", "<strong>yourtheme_location_selection_popup_content_callback</strong>", 10, 2);', |
| 159 | 'add_filter("<strong>slw_location_selection_popup_display</strong>", "<strong>yourtheme_location_selection_popup_display_callback</strong>", 10, 2);'), |
| 160 | 'screenshot' => array(SLW_PLUGIN_URL.'images/slw-location-popup-thumb.png', 'https://ps.w.org/stock-locations-for-woocommerce/assets/screenshot-17.png'), |
| 161 | |
| 162 | ) |
| 163 | ); |
| 164 | |
| 165 | }, 1); |
| 166 | |
| 167 | // Instantiate classes |
| 168 | new SLW\SRC\Classes\SlwLocationTaxonomy; |
| 169 | new SLW\SRC\Classes\SlwStockLocationsTab; |
| 170 | new SLW\SRC\Classes\SlwOrderItem; |
| 171 | new SLW\SRC\Classes\SlwShortcodes; |
| 172 | new SLW\SRC\Classes\SlwProductListing; |
| 173 | new SLW\SRC\Classes\SlwProductRest; |
| 174 | new SLW\SRC\Classes\SlwSettings; |
| 175 | // Frontend |
| 176 | new SLW\SRC\Classes\Frontend\SlwFrontendCart; |
| 177 | new SLW\SRC\Classes\Frontend\SlwFrontendProduct; |
| 178 | |
| 179 | // get settings |
| 180 | $this->plugin_settings = get_option( 'slw_settings', array() ); |
| 181 | $this->plugin_settings = is_array($this->plugin_settings)?$this->plugin_settings:array(); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Ensures only one instance of our plugin is loaded or can be loaded. |
| 186 | * |
| 187 | * @since 1.0.0 |
| 188 | * @return object |
| 189 | */ |
| 190 | public static function instance() |
| 191 | { |
| 192 | |
| 193 | if ( is_null( self::$instance ) ) { |
| 194 | self::$instance = new self(); |
| 195 | } |
| 196 | |
| 197 | return self::$instance; |
| 198 | |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Initiates the hooks. |
| 203 | * |
| 204 | * @since 1.0.0 |
| 205 | * @return void |
| 206 | */ |
| 207 | public function init() |
| 208 | { |
| 209 | // Enqueue scripts and styles |
| 210 | |
| 211 | add_action( 'admin_enqueue_scripts', array($this, 'enqueue_admin') ); |
| 212 | |
| 213 | add_action( 'wp_enqueue_scripts', array($this, 'enqueue_frontend') ); |
| 214 | |
| 215 | // Prevent WooCommerce from reduce stock |
| 216 | add_filter( 'woocommerce_can_reduce_order_stock', '__return_false', 999 ); // Since WC 3.0.2 |
| 217 | |
| 218 | // Display admin notices |
| 219 | add_action( 'admin_notices', [new SLW\SRC\Classes\SlwAdminNotice(), 'displayAdminNotice'] ); |
| 220 | |
| 221 | // Fix for Point of Sale for WooCommerce (https://woocommerce.com/products/point-of-sale-for-woocommerce/) |
| 222 | if( class_exists('WC_POS') ) { |
| 223 | remove_filter( 'woocommerce_stock_amount', 'floatval', 99 ); |
| 224 | add_filter( 'woocommerce_stock_amount', 'intval' ); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Adds scripts and styles for Admin. |
| 230 | * |
| 231 | * @since 1.0.0 |
| 232 | * @return void |
| 233 | */ |
| 234 | public function enqueue_admin() |
| 235 | { |
| 236 | global $current_screen, $post, $slw_gkey, $wc_slw_pro, $slw_api_valid_keys; |
| 237 | |
| 238 | |
| 239 | wp_enqueue_style( 'slw-admin-styles', SLW_PLUGIN_DIR_URL . 'css/admin-style.css', array(), time() ); |
| 240 | |
| 241 | wp_enqueue_style( 'slw-common-styles', SLW_PLUGIN_DIR_URL . 'css/common-style.css', array(), time() ); |
| 242 | wp_register_script( 'slw-admin-scripts', SLW_PLUGIN_DIR_URL . 'js/admin-scripts.js', array( 'jquery', 'jquery-blockui' ), time(), true ); |
| 243 | |
| 244 | if($wc_slw_pro) |
| 245 | wp_register_script( 'slw-pro-admin-scripts', SLW_PLUGIN_DIR_URL . 'pro/js/admin-scripts.js', array( 'jquery', 'jquery-blockui' ), time(), true ); |
| 246 | |
| 247 | $slw_location_statuses = array(); |
| 248 | |
| 249 | $terms = slw_get_locations('location', array(), false); |
| 250 | if( ! empty( $terms ) ) { |
| 251 | foreach( $terms as $location ) { |
| 252 | $slw_location_status = get_term_meta($location->term_id, 'slw_location_status', true); |
| 253 | $slw_location_statuses[$location->term_id] = ($slw_location_status=='1'?'yes':'no'); |
| 254 | |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | $data = array( |
| 260 | 'slug' => SLW_PLUGIN_SLUG, |
| 261 | 'home_url' => trailingslashit( home_url() ), |
| 262 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 263 | 'nonce' => wp_create_nonce( 'slw_nonce' ), |
| 264 | 'slw_gkey' => $slw_gkey, |
| 265 | 'stock_locations' => false, |
| 266 | 'wc_slw_pro' => $wc_slw_pro, |
| 267 | 'wc_slw_api_valid_keys'=>$slw_api_valid_keys, |
| 268 | 'wc_slw_premium_feature' => __('This is a premium feature!', 'stock-locations-for-woocommerce'), |
| 269 | 'wc_slw_stock_reset_msg' => __('This action will restore the stock values to the product. Do you want to proceed?', 'stock-locations-for-woocommerce'), |
| 270 | 'wc_slw_product_id' => (is_object($post)?$post->ID:0), |
| 271 | 'wc_slw_location_status' => $slw_location_statuses, |
| 272 | 'wc_slw_location_disabled_msg' => __('Enable this location from edit location page to contribute in the total stock value.', 'stock-locations-for-woocommerce'), |
| 273 | 'slw_import_export_tutorial' => __('CSV Import/Export Stock Locations?', 'stock-locations-for-woocommerce'), |
| 274 | 'slw_error_occurred' => __('An error occurred while processing your request.', 'stock-locations-for-woocommerce'), |
| 275 | ); |
| 276 | $data['currency_symbol'] = get_woocommerce_currency_symbol(); |
| 277 | |
| 278 | |
| 279 | if(is_object($post) && $post->post_type=='product'){ |
| 280 | |
| 281 | $terms = wp_get_post_terms( $post->ID, 'location', array('meta_key'=>'slw_location_status', 'meta_value'=>true, 'meta_compare'=>'=') ); |
| 282 | if(!empty($terms)){ |
| 283 | $data['stock_locations'] = true; |
| 284 | } |
| 285 | } |
| 286 | wp_localize_script( |
| 287 | 'slw-admin-scripts', |
| 288 | 'slw_admin_scripts', |
| 289 | $data |
| 290 | ); |
| 291 | wp_enqueue_script( 'slw-admin-scripts' ); |
| 292 | |
| 293 | if($wc_slw_pro){ |
| 294 | wp_localize_script( |
| 295 | 'slw-pro-admin-scripts', |
| 296 | 'slw_admin_scripts', |
| 297 | $data |
| 298 | ); |
| 299 | wp_enqueue_script( 'slw-pro-admin-scripts' ); |
| 300 | } |
| 301 | |
| 302 | if( |
| 303 | (isset($_GET['page']) && $_GET['page']=='slw-settings') |
| 304 | || |
| 305 | (isset($_GET['taxonomy']) && $_GET['taxonomy']=='location') |
| 306 | ){ |
| 307 | wp_enqueue_style( 'slw-bootstrap-styles', SLW_PLUGIN_DIR_URL . 'css/bootstrap.min.css', array(), date('m') ); |
| 308 | wp_enqueue_style( 'font-awesome', SLW_PLUGIN_DIR_URL . 'css/fontawesome.min.css', array(), date('Ymdh') ); |
| 309 | |
| 310 | wp_enqueue_script( 'font-awesome', SLW_PLUGIN_DIR_URL . 'js/fontawesome.min.js', array( 'jquery' ), date('Ymdh') ); |
| 311 | wp_enqueue_script( 'bootstrap', SLW_PLUGIN_DIR_URL . 'js/bootstrap.min.js', array( 'jquery' ), date('m') ); |
| 312 | |
| 313 | if($slw_gkey){ |
| 314 | wp_enqueue_script( 'slw-googleapis-scripts', 'https://maps.googleapis.com/maps/api/js?key='.$slw_gkey.'&libraries=places', array(), time() ); |
| 315 | } |
| 316 | |
| 317 | wp_enqueue_style( 'slw-magnific-popup', SLW_PLUGIN_DIR_URL . 'css/magnific-popup.css', array(), time() ); |
| 318 | wp_enqueue_script( 'magnific-popup', SLW_PLUGIN_DIR_URL . 'js/jquery.magnific-popup.min.js', array( 'jquery' ), date('m') ); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Adds scripts and styles for Frontend. |
| 324 | * |
| 325 | * @since 1.2.0 |
| 326 | * @return void |
| 327 | */ |
| 328 | |
| 329 | public function enqueue_frontend() |
| 330 | { |
| 331 | global $post, $wpdb, $wc_slw_pro, $woocommerce, $slw_wc_hide_out_of_stock; |
| 332 | wp_enqueue_style( 'slw-frontend-styles', SLW_PLUGIN_DIR_URL . 'css/frontend-style.css', null, time() ); |
| 333 | wp_enqueue_style( 'slw-common-styles', SLW_PLUGIN_DIR_URL . 'css/common-style.css', array(), time() ); |
| 334 | |
| 335 | |
| 336 | $product_id = (is_product()?$post->ID:0); |
| 337 | |
| 338 | $term_id = (int)(is_archive()?get_queried_object_id():0); |
| 339 | $term_id = is_numeric($term_id)?$term_id:0; |
| 340 | $term_link = ($term_id?get_term_link($term_id):''); |
| 341 | $term_link = (!is_wp_error($term_link)?$term_link:''); |
| 342 | |
| 343 | $data = (is_array($this->plugin_settings)?$this->plugin_settings:array()); |
| 344 | $data['slw_wc_hide_out_of_stock'] = $slw_wc_hide_out_of_stock; |
| 345 | $data['slw_location_selection'] = get_option('slw-location-selection', 'no'); |
| 346 | $data['slw_location_selection_popup'] = (apply_filters('slw_location_selection_popup_display', is_front_page(), is_shop()) && function_exists('slw_location_selection_popup') && (get_option('slw-location-selection', 'no')=='yes')?slw_location_selection_popup($product_id):''); |
| 347 | $data['is_home_page'] = (is_home()?'yes':'no'); |
| 348 | $data['is_front_page'] = (is_front_page()?'yes':'no'); |
| 349 | $data['ajaxurl'] = admin_url( 'admin-ajax.php' ); |
| 350 | $data['cart_url'] = wc_get_cart_url(); |
| 351 | $data['wc_slw_pro'] = $wc_slw_pro; |
| 352 | $data['is_cart'] = is_cart(); |
| 353 | $data['is_checkout'] = is_checkout(); |
| 354 | $data['is_product'] = is_product(); |
| 355 | $data['product_id'] = $product_id; |
| 356 | $data['product_type'] = ''; |
| 357 | $data['show_in_product_page'] = (array_key_exists('show_in_product_page', $this->plugin_settings)?$this->plugin_settings['show_in_product_page']:'no'); |
| 358 | $data['show_in_product_page_pos'] = (array_key_exists('show_in_product_page_pos', $this->plugin_settings)?$this->plugin_settings['show_in_product_page_pos']:''); |
| 359 | $data['stock_locations'] = 0; |
| 360 | $data['stock_quantity'] = array(); |
| 361 | $data['stock_status'] = array(); |
| 362 | $data['stock_quantity_sum'] = 0; |
| 363 | $data['out_of_stock'] = __('Out of stock', 'woocommerce'); |
| 364 | $data['in_stock'] = __('In stock', 'woocommerce'); |
| 365 | $data['backorder'] = __('Available on backorder', 'woocommerce'); |
| 366 | $data['max_available_qty_msg'] = __('Maximum available quantity has already been added to the cart.', 'woocommerce'); |
| 367 | $data['currency_symbol'] = get_woocommerce_currency_symbol(); |
| 368 | $data['slw_term_url'] = $term_link; |
| 369 | $data['slw_term_id'] = $term_id; |
| 370 | $data['slw_term_add_to_cart_url'] = $data['slw_term_url'].'?stock-location='.$data['slw_term_id'].'&add-to-cart='; |
| 371 | $data['stock_location_selected'] = get_slw_stock_location_selected(); |
| 372 | |
| 373 | |
| 374 | $stock_locations = array(); |
| 375 | $stock_locations_obj = slw_get_locations('location', array(), false, $data['product_id']); |
| 376 | |
| 377 | if( ! empty( $stock_locations_obj ) ) { |
| 378 | foreach( $stock_locations_obj as $location ) { //pree($location); |
| 379 | $location_notice = get_term_meta($location->term_id, 'slw_location_notice', true); //pree($location_notice); |
| 380 | $location_priority = get_term_meta($location->term_id, 'slw_location_priority', true); //pree($location_priority); |
| 381 | $stock_locations[$location->term_id] = array('id'=>$location->term_id, 'name'=>$location->name, 'priority'=>$location_priority, 'notice'=>$location_notice); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | |
| 386 | $data['stock_locations_data'] = $stock_locations; |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | $data['slw_allow_geo'] = __('Allow current location', 'stock-locations-for-woocommerce'); |
| 393 | $data['slw_allow_geo_tip'] = __('Allow current location to calculate the distance and sort by nearest', 'stock-locations-for-woocommerce'); |
| 394 | $data['dummy_price'] = wc_format_localized_price(111); |
| 395 | $data['nonce'] = wp_create_nonce( 'slw_nonce' ); |
| 396 | |
| 397 | $data['slw_archive_items_halt_msg'] = __('Sorry! You have already added the available stock quantity to your cart.', 'stock-locations-for-woocommerce'); |
| 398 | $data['slw_archive_items_max_msg'] = __('Sorry! You can add only the available stock quantity to your cart.', 'stock-locations-for-woocommerce'); |
| 399 | |
| 400 | $data['slw_cart_items'] = array(); |
| 401 | if(is_object($woocommerce)){ |
| 402 | $items = (isset($woocommerce->cart) && method_exists($woocommerce->cart, 'get_cart')?$woocommerce->cart->get_cart():array()); |
| 403 | |
| 404 | if(!empty($items)){ |
| 405 | $slw_cart_items = $data['slw_cart_items']; |
| 406 | foreach($items as $item => $values) { |
| 407 | $product_id = $values['product_id']; |
| 408 | $variation_id = $values['variation_id']; |
| 409 | $stock_location_id = array_key_exists('stock_location', $values)?$values['stock_location']:0; |
| 410 | $stock_location_id = (is_array($stock_location_id)?$stock_location_id[$product_id]:$stock_location_id); |
| 411 | |
| 412 | $quantity = array_key_exists('quantity', $values)?$values['quantity']:0; |
| 413 | |
| 414 | $slw_cart_items[$product_id][$variation_id][$stock_location_id] = $quantity; |
| 415 | } |
| 416 | $data['slw_cart_items'] = $slw_cart_items; |
| 417 | |
| 418 | |
| 419 | } |
| 420 | |
| 421 | } |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | if($term_id && isset($this->plugin_settings['extra_assets_settings']) && isset($this->plugin_settings['extra_assets_settings']['font_awesome']) && $this->plugin_settings['extra_assets_settings']['font_awesome'] == 'on'){ |
| 427 | wp_enqueue_style( 'font-awesome', SLW_PLUGIN_DIR_URL . 'css/fontawesome.min.css', array(), date('Ymdh') ); |
| 428 | wp_enqueue_script( 'font-awesome', SLW_PLUGIN_DIR_URL . 'js/fontawesome.min.js', array( 'jquery' ), date('Ymdh') ); |
| 429 | } |
| 430 | |
| 431 | $data['stock_locations_product_page_notice'] = ''; |
| 432 | |
| 433 | |
| 434 | |
| 435 | if($data['is_product'] && (is_object($post) && $post->post_type=='product')){// && isset($this->plugin_settings['show_in_product_page']) && $this->plugin_settings['show_in_product_page'] == 'yes' ) { |
| 436 | |
| 437 | |
| 438 | |
| 439 | $everything_stock_status_to_instock = array_key_exists('everything_stock_status_to_instock', $this->plugin_settings); |
| 440 | if($everything_stock_status_to_instock && function_exists('everything_stock_status_to_instock')){ |
| 441 | everything_stock_status_to_instock($data['product_id']); |
| 442 | } |
| 443 | |
| 444 | |
| 445 | $meta_obj = $wpdb->get_row('SELECT COUNT(*) AS total_locations FROM '.$wpdb->prefix.'postmeta pm WHERE pm.post_id="'.esc_sql($data['product_id']).'" AND pm.meta_key LIKE "_stock_at_%" AND pm.meta_value>0'); |
| 446 | //pree($data['product_id']); |
| 447 | $wc_product = wc_get_product($data['product_id']); |
| 448 | |
| 449 | $terms = slw_get_locations('location', array(), true, $data['product_id']); |
| 450 | //pree($terms); |
| 451 | |
| 452 | if(!empty($meta_obj)){ |
| 453 | if($meta_obj->total_locations>0){ |
| 454 | $data['stock_locations'] = $meta_obj->total_locations; |
| 455 | } |
| 456 | } |
| 457 | $product_price = trim(str_replace(get_woocommerce_currency_symbol(), '', strip_tags(wc_price($wc_product->get_price())))); |
| 458 | $data['product_type'] = $wc_product->get_type(); |
| 459 | |
| 460 | $data['product_price_format'] = trim(str_replace(array(get_woocommerce_currency_symbol(), $product_price), array('_CURRENCY_SYMBOL', '_PRODUCT_PRICE'), strip_tags(wc_price($wc_product->get_price())))); |
| 461 | $data['product_price_raw'] = $product_price; |
| 462 | $data['product_min_max_prices'] = slw_get_min_max_location_prices($wc_product); |
| 463 | $data['product_price'] = $wc_product->get_price(); |
| 464 | $data['stock_status'][$data['product_id']] = $wc_product->get_availability(); |
| 465 | $data['allow_backorder'][$data['product_id']] = get_post_meta($data['product_id'], '_backorders', true); |
| 466 | $data['product_price_decimals'] = apply_filters('slw_product_price_decimals', 2, $product_price); |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | $extra_data = slw_get_product_stock_by_location($data['product_id'], $data['product_type'], $terms); |
| 472 | $data = array_merge( $data, $extra_data ); |
| 473 | |
| 474 | /* |
| 475 | if($data['product_type']=='variable' && $data['product_id']>0){ |
| 476 | |
| 477 | $product_variations_ids = $wpdb->get_results("SELECT ID AS variation_id FROM $wpdb->posts WHERE post_parent IN (".$data['product_id'].") AND post_type='product_variation'"); |
| 478 | //$product_variations_ids = $wc_product->get_children(); |
| 479 | //pree($product_variations_ids); |
| 480 | $product_variations = array(); |
| 481 | |
| 482 | //pre($product_variations_ids); |
| 483 | //pre($terms); |
| 484 | $locations = array(); |
| 485 | |
| 486 | foreach( $product_variations_ids as $variation_obj ) { |
| 487 | $variation_id = $variation_obj->variation_id; |
| 488 | if(!empty($terms)){ |
| 489 | $data['stock_quantity'][$variation_id][0] = 0; |
| 490 | foreach($terms as $term){ |
| 491 | $wc_variation = wc_get_product($variation_id); |
| 492 | |
| 493 | $data['stock_status'][$variation_id] = $wc_variation->get_availability(); |
| 494 | $data['allow_backorder'][$variation_id] = get_post_meta($variation_id, '_backorders', true); |
| 495 | $data['stock_quantity'][$data['product_id']][$term->term_id] = get_post_meta($data['product_id'], '_stock_at_'.$term->term_id, true); |
| 496 | $data['stock_quantity'][$variation_id][$term->term_id] = get_post_meta($variation_id, '_stock_at_'.$term->term_id, true); |
| 497 | |
| 498 | $data['stock_quantity_sum'] += ((float)$data['stock_quantity'][$variation_id][$term->term_id])*1; |
| 499 | |
| 500 | } |
| 501 | $data['stock_quantity'][$variation_id][0] = $data['stock_quantity_sum']; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | }else{ |
| 506 | |
| 507 | if(!empty($terms)){ |
| 508 | $data['stock_quantity'][$data['product_id']][0] = 0; |
| 509 | foreach($terms as $term){ |
| 510 | $data['stock_quantity'][$data['product_id']][$term->term_id] = get_post_meta($data['product_id'], '_stock_at_'.$term->term_id, true); |
| 511 | $data['stock_quantity_sum'] += ((float)$data['stock_quantity'][$data['product_id']][$term->term_id])*1; |
| 512 | } |
| 513 | $data['stock_quantity'][$data['product_id']][0] = $data['stock_quantity_sum']; |
| 514 | } |
| 515 | }*/ |
| 516 | |
| 517 | |
| 518 | //pree($data); |
| 519 | |
| 520 | $data['stock_locations_product_page_notice'] = apply_filters('slw_product_stock_location_notice', sprintf('%s '.__('available at', 'stock-locations-for-woocommerce').' %s', 'STOCK_QTY', 'LOCATION_NAME'), $data['product_id'], $terms, $data['stock_quantity']); |
| 521 | |
| 522 | wp_enqueue_script( 'slw-frontend-product-underscore', SLW_PLUGIN_DIR_URL . 'js/underscore-min.js', array( 'jquery' ), time(), true ); |
| 523 | wp_enqueue_script( 'slw-frontend-product-scripts', SLW_PLUGIN_DIR_URL . 'js/product.js', array( 'jquery-blockui' ), time(), true ); |
| 524 | wp_localize_script( |
| 525 | 'slw-frontend-product-scripts', |
| 526 | 'slw_frontend', |
| 527 | $data |
| 528 | ); |
| 529 | } |
| 530 | |
| 531 | |
| 532 | if( is_archive() && is_tax( 'location' ) && $data['slw_term_id'] && function_exists('slw_get_products_by_location_term_id')) { |
| 533 | $products_by_location_term_id = slw_get_products_by_location_term_id($data['slw_term_id']); |
| 534 | |
| 535 | if(!empty($products_by_location_term_id)){ |
| 536 | |
| 537 | foreach($products_by_location_term_id as $product_id_by_location){ |
| 538 | $data['stock_quantity'][$product_id_by_location][$data['slw_term_id']] = get_post_meta($product_id_by_location, '_stock_at_'.$data['slw_term_id'], true); |
| 539 | //$data['stock_quantity_sum'] += ((float)$data['stock_quantity'][$product_id_by_location][$data['slw_term_id']])*1; |
| 540 | } |
| 541 | |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | wp_enqueue_script( |
| 549 | 'slw-common-scripts', |
| 550 | SLW_PLUGIN_DIR_URL . 'js/common.js', |
| 551 | array('jquery'), |
| 552 | time(), |
| 553 | true |
| 554 | ); |
| 555 | wp_enqueue_script( |
| 556 | 'slw-jquery-blockui', |
| 557 | SLW_PLUGIN_DIR_URL . 'js/jquery.blockUI.js', |
| 558 | array('jquery'), |
| 559 | time(), |
| 560 | true |
| 561 | ); |
| 562 | wp_localize_script( |
| 563 | 'slw-common-scripts', |
| 564 | 'slw_frontend', |
| 565 | $data |
| 566 | ); |
| 567 | |
| 568 | if( is_archive() && is_tax( 'location' )) { |
| 569 | //if(get_option('slw-archives-status')=='yes'){ |
| 570 | wp_enqueue_script( |
| 571 | 'slw-archive-scripts', |
| 572 | SLW_PLUGIN_DIR_URL . 'js/archive.js', |
| 573 | array('jquery-blockui'), |
| 574 | time(), |
| 575 | true |
| 576 | ); |
| 577 | //} |
| 578 | } |
| 579 | |
| 580 | if( isset($this->plugin_settings['show_in_cart']) && $this->plugin_settings['show_in_cart'] == 'yes' ) { |
| 581 | wp_enqueue_script( |
| 582 | 'slw-frontend-cart-scripts', |
| 583 | SLW_PLUGIN_DIR_URL . 'js/cart.js', |
| 584 | array('jquery-blockui'), |
| 585 | time(), |
| 586 | true |
| 587 | ); |
| 588 | |
| 589 | |
| 590 | }else{ |
| 591 | |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | } |
| 596 | |
| 597 | } |
| 598 | |
| 599 | |
| 600 | /** |
| 601 | * Initiate the plugin. |
| 602 | * |
| 603 | * @since 1.0.0 |
| 604 | */ |
| 605 | |
| 606 | add_action( 'plugins_loaded', 'initiate_slw_plugin' ); |
| 607 | function initiate_slw_plugin(){ |
| 608 | |
| 609 | // check if WooCommerce is active |
| 610 | if ( ! class_exists( 'woocommerce' ) ) { |
| 611 | |
| 612 | include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 613 | |
| 614 | // deactivate the plugin |
| 615 | deactivate_plugins( plugin_basename( __FILE__ ) ); |
| 616 | |
| 617 | // show error |
| 618 | echo '<div class="error"><p>' . __('Stock Locations for WooCommerce requires WooCommerce to be activaded. Please active WooCommerce plugin first.', 'stock-locations-for-woocommerce') . '</p></div>'; |
| 619 | |
| 620 | } else { |
| 621 | |
| 622 | // require autoload |
| 623 | require plugin_dir_path( __FILE__ ) . 'vendor/autoload.php'; |
| 624 | |
| 625 | // define constants |
| 626 | define( 'SLW_PLUGIN_SLUG', dirname( plugin_basename( __FILE__ ) ) ); |
| 627 | define( 'SLW_PLUGIN_DIR_URL', plugin_dir_url( __FILE__ ) ); |
| 628 | define( 'SLW_PLUGIN_DIR_URL_ABSOLUTE_PATH', realpath( plugin_dir_path( __FILE__ ) ) ); |
| 629 | define( 'SLW_PLUGIN_DIR_PATH', plugin_dir_path( __FILE__ ) ); |
| 630 | define( 'SLW_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); |
| 631 | |
| 632 | // intantiate |
| 633 | SlwMain::instance(); |
| 634 | |
| 635 | |
| 636 | } |
| 637 | |
| 638 | |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * Return SlwMain instance |
| 643 | * |
| 644 | * @return object|SlwMain |
| 645 | */ |
| 646 | function Slw() |
| 647 | { |
| 648 | return SlwMain::instance(); |
| 649 | } |
| 650 | |
| 651 | add_action( 'before_woocommerce_init', function() { |
| 652 | if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { |
| 653 | \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); |
| 654 | } |
| 655 | } ); |
| 656 | |
| 657 |