Skip to content

Commit 0cb2784

Browse files
committed
2.7.3
1 parent c22007a commit 0cb2784

File tree

3 files changed

+48
-26
lines changed

3 files changed

+48
-26
lines changed

src/inc/functions.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ function slw_crons(){
746746
}
747747

748748

749-
function slw_get_locations($taxonomy='location', $additional_meta_query=array(), $enabled_only=true){
749+
function slw_get_locations($taxonomy='location', $additional_meta_query=array(), $enabled_only=true, $product_id=0){
750750

751751
$args = array('hide_empty' => false, 'meta_query' => array());
752752

@@ -766,9 +766,26 @@ function slw_get_locations($taxonomy='location', $additional_meta_query=array(),
766766
$args['meta_query']['relation'] = 'AND';
767767
$args['meta_query'][] = $additional_meta_query;
768768
}
769-
//pree($taxonomy);pree($args);
769+
if($product_id){
770+
$product_terms = wc_get_product_terms($product_id, $taxonomy);
771+
772+
if(!empty($product_terms)){
773+
$include_arr = array();
774+
foreach($product_terms as $product_term){
775+
$include_arr[] = $product_term->term_id;
776+
}
777+
if(!empty($include_arr)){
778+
$args['include'] = $include_arr;
779+
}
780+
}
781+
}
782+
783+
//pree($taxonomy);
784+
//pree($args);
770785
$terms = get_terms($taxonomy, $args);
771-
//pre($terms);
786+
//pree($terms);
787+
788+
772789
return $terms;
773790
}
774791

src/readme.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ On settings page you can define a number. If location stock value will be less t
160160

161161

162162
== Changelog ==
163+
= 2.7.3 =
164+
- Fix: Default stock available at notice will only display the associated locations which are enabled. [Thanks to Villa Bafo][12/06/2024]
165+
163166
= 2.7.2 =
164167
Fix: PHP Fatal error: Uncaught TypeError: method_exists(): Argument #1 ($object_or_class) must be of type object|string, bool given. [Thanks to @araundu][12/06/2024]
165168

src/stock-locations-for-woocommerce.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112

113113
class SlwMain{
114114
// versions
115-
public $version = '2.7.2';
115+
public $version = '2.7.3';
116116
public $import_export_addon_version = '1.1.1';
117117

118118
// others
@@ -294,6 +294,7 @@ public function enqueue_frontend()
294294
wp_enqueue_style( 'slw-common-styles', SLW_PLUGIN_DIR_URL . 'css/common-style.css', array(), time() );
295295

296296

297+
$product_id = (is_product()?$post->ID:0);
297298

298299
$term_id = (int)(is_archive()?get_queried_object_id():0);
299300
$term_id = is_numeric($term_id)?$term_id:0;
@@ -311,7 +312,7 @@ public function enqueue_frontend()
311312
$data['is_cart'] = is_cart();
312313
$data['is_checkout'] = is_checkout();
313314
$data['is_product'] = is_product();
314-
$data['product_id'] = 0;
315+
$data['product_id'] = $product_id;
315316
$data['product_type'] = '';
316317
$data['show_in_product_page'] = (array_key_exists('show_in_product_page', $this->plugin_settings)?$this->plugin_settings['show_in_product_page']:'no');
317318
$data['stock_locations'] = 0;
@@ -329,19 +330,18 @@ public function enqueue_frontend()
329330
$data['stock_location_selected'] = ((isset($woocommerce->session) && $woocommerce->session->has_session())?$woocommerce->session->get('stock_location_selected'):0);
330331

331332
$stock_locations = array();
332-
$stock_locations_obj = slw_get_locations('location', array(), false);
333+
$stock_locations_obj = slw_get_locations('location', array(), false, $data['product_id']);
333334

334335
if( ! empty( $stock_locations_obj ) ) {
335-
foreach( $stock_locations_obj as $location ) {
336-
$location_notice = get_term_meta($location->term_id, 'slw_location_notice', true);
337-
$location_priority = get_term_meta($location->term_id, 'slw_location_priority', true);
336+
foreach( $stock_locations_obj as $location ) { //pree($location);
337+
$location_notice = get_term_meta($location->term_id, 'slw_location_notice', true); //pree($location_notice);
338+
$location_priority = get_term_meta($location->term_id, 'slw_location_priority', true); //pree($location_priority);
338339
$stock_locations[$location->term_id] = array('id'=>$location->term_id, 'name'=>$location->name, 'priority'=>$location_priority, 'notice'=>$location_notice);
339340
}
340341
}
341342

342343

343344
$data['stock_locations_data'] = $stock_locations;
344-
//$data['stock_locations_product_page_notice'] = apply_filters('slw_product_stock_location_notice', 'STOCK_QTY available at LOCATION_NAME'); //12/02/2024
345345

346346

347347

@@ -392,19 +392,19 @@ public function enqueue_frontend()
392392

393393
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' ) {
394394

395-
$product_id = $post->ID;
395+
396396

397397
$everything_stock_status_to_instock = array_key_exists('everything_stock_status_to_instock', $this->plugin_settings);
398398
if($everything_stock_status_to_instock && function_exists('everything_stock_status_to_instock')){
399-
everything_stock_status_to_instock($product_id);
399+
everything_stock_status_to_instock($data['product_id']);
400400
}
401401

402402

403-
$meta_obj = $wpdb->get_row('SELECT COUNT(*) AS total_locations FROM '.$wpdb->prefix.'postmeta pm WHERE pm.post_id="'.esc_sql($product_id).'" AND pm.meta_key LIKE "_stock_at_%" AND pm.meta_value>0');
404-
$wc_product = wc_get_product($product_id);
405-
406-
$terms = slw_get_locations();
403+
$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');
404+
$wc_product = wc_get_product($data['product_id']);
407405

406+
$terms = slw_get_locations('location', array(), true, $data['product_id']);
407+
//pree($terms);
408408

409409
if(!empty($meta_obj)){
410410
if($meta_obj->total_locations>0){
@@ -413,20 +413,21 @@ public function enqueue_frontend()
413413
}
414414
$product_price = trim(str_replace(get_woocommerce_currency_symbol(), '', strip_tags(wc_price($wc_product->get_price()))));
415415
$data['product_type'] = $wc_product->get_type();
416-
$data['product_id'] = $product_id;
416+
417417
$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()))));
418418
$data['product_price_raw'] = $product_price;
419419
$data['product_price'] = $wc_product->get_price();
420-
$data['stock_status'][$product_id] = $wc_product->get_availability();
421-
$data['allow_backorder'][$product_id] = get_post_meta($product_id, '_backorders', true);
420+
$data['stock_status'][$data['product_id']] = $wc_product->get_availability();
421+
$data['allow_backorder'][$data['product_id']] = get_post_meta($data['product_id'], '_backorders', true);
422422
$data['product_price_decimals'] = apply_filters('slw_product_price_decimals', 2, $product_price);
423423

424424

425425

426-
if($data['product_type']=='variable' && $product_id>0){
426+
if($data['product_type']=='variable' && $data['product_id']>0){
427427

428-
$product_variations_ids = $wpdb->get_results("SELECT ID AS variation_id FROM $wpdb->posts WHERE post_parent IN ($product_id) AND post_type='product_variation'");
428+
$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'");
429429
//$product_variations_ids = $wc_product->get_children();
430+
//pree($product_variations_ids);
430431
$product_variations = array();
431432

432433
//pre($product_variations_ids);
@@ -442,7 +443,7 @@ public function enqueue_frontend()
442443

443444
$data['stock_status'][$variation_id] = $wc_variation->get_availability();
444445
$data['allow_backorder'][$variation_id] = get_post_meta($variation_id, '_backorders', true);
445-
$data['stock_quantity'][$product_id][$term->term_id] = get_post_meta($product_id, '_stock_at_'.$term->term_id, true);
446+
$data['stock_quantity'][$data['product_id']][$term->term_id] = get_post_meta($data['product_id'], '_stock_at_'.$term->term_id, true);
446447
$data['stock_quantity'][$variation_id][$term->term_id] = get_post_meta($variation_id, '_stock_at_'.$term->term_id, true);
447448

448449
$data['stock_quantity_sum'] += ((float)$data['stock_quantity'][$variation_id][$term->term_id])*1;
@@ -454,15 +455,16 @@ public function enqueue_frontend()
454455

455456
}else{
456457
if(!empty($terms)){
457-
$data['stock_quantity'][$product_id][0] = 0;
458+
$data['stock_quantity'][$data['product_id']][0] = 0;
458459
foreach($terms as $term){
459-
$data['stock_quantity'][$product_id][$term->term_id] = get_post_meta($product_id, '_stock_at_'.$term->term_id, true);
460-
$data['stock_quantity_sum'] += ((float)$data['stock_quantity'][$product_id][$term->term_id])*1;
460+
$data['stock_quantity'][$data['product_id']][$term->term_id] = get_post_meta($data['product_id'], '_stock_at_'.$term->term_id, true);
461+
$data['stock_quantity_sum'] += ((float)$data['stock_quantity'][$data['product_id']][$term->term_id])*1;
461462
}
462-
$data['stock_quantity'][$product_id][0] = $data['stock_quantity_sum'];
463+
$data['stock_quantity'][$data['product_id']][0] = $data['stock_quantity_sum'];
463464
}
464465
}
465466

467+
//pree($data);
466468

467469
$data['stock_locations_product_page_notice'] = apply_filters('slw_product_stock_location_notice', sprintf(__('%s available at %s', 'stock-locations-for-woocommerce'), 'STOCK_QTY', 'LOCATION_NAME'), $data['product_id'], $terms, $data['stock_quantity']);
468470

0 commit comments

Comments
 (0)