Function showing same result count product have left WooCommerce

1 answer

I want showing products have left / total product and show by the progress bar, on product page work fine, but on loop page (category, shop page) just showing the same result of one product, this should be a different result on a product. Here is the code

jQuery(document).ready(function(){   'use strict';     var wbpsSale = jQuery('#wpbsc_total_sale').attr('total-sale');     var wbpsStock = jQuery('#wpbsc_total_sale').attr('total-stock');     var jqmeter_params = {         goal:wbpsStock,         raised:wbpsSale,         meterOrientation:'horizontal',         displayTotal: true,           width:'100%',         height:'17px'         };   jQuery("corergb-bar").jQMeter(jqmeter_params);  }); 
    function wpbsc_show_stock_shop() {         global $product;         global $post;         $manage_stock = get_post_meta( $post->ID, '_manage_stock', true );         if( $product->is_on_sale() && $product && $manage_stock == 'yes'){          $stock = get_post_meta( $post->ID, '_stock', true );         $wpbscSale = $product->get_total_sales();         $wpbscTotalStock = intval($stock)+ intval($wpbscSale);         ?>          <div class="wpbsc-stock-counter">            <div id="wpbsc_total_sale" class="total-sale-stat" total-sale="<?php echo esc_attr( $wpbscSale ); ?>" total-stock="<?php echo esc_attr($wpbscTotalStock) ?>"></div>             <p class="stock-progressbar-status"><span class="total-sold"> <?php echo esc_html__("Đã bán: {$wpbscSale}",'wpbsc'); ?></span></p>             <div id="corergb-bar" class="bar-progress-stat"></div>         </div>          <?php       }     } 

Pls help me, i want showing to hook "woocommerce_after_shop_loop_item_title" with different result on a product

image example:

here is showing on product page

showing on category but only progress bar not with percent

when i change jQuery("[id='corergb-bar']") to jQuery('.bar-progress-stat') the result progress bar showing but same result for all product have manage_stock

image example: should be different result on a product

All answers to this question, which has the identifier 61230352

The best answer:

EDIT: A very simple alternative would be to use a progress bar provided by your CSS framework as most of them do include one. For example, if you use bootstrap then you use find the documentation for it's progress bar here.

It is very simple:

<div class="progress"> <div class="progress-bar" role="progressbar" style="width: <?php echo ((esc_attr( $wpbscSale )*100)/esc_attr( $wpbscTotalStock )); ?>%" aria-valuenow="<?php echo esc_attr( $wpbscSale ); ?>" aria-valuemin="0" aria-valuemax="<?php echo esc_attr($wpbscTotalStock) ?>"> </div> 

Previous Incorrect deduction: After taking a quick look, I think you might not be getting a value for your $stock variable. Could you try to use $product->get_id() instead of $post->ID at $stock = get_post_meta( $post->ID, '_stock', true ); and then try again?

So it should look like: $stock = get_post_meta( $product->get_id(), '_stock', true );

You could also try to use [get_the_id()][1] instead. Edit: You are supposed to use get_the_id() without the object reference.

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data