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

Received answer to this question:
The best answer according to the author of the question:

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.