admin管理员组

文章数量:1328021

I am trying to add order, sale count next to star rating at single product page, my store is built with WordPress and Woocommerce, I have tried a code but it adds this function before add to cart button/form.

Website URL for reference:

and image

Code:

add_action( 'woocommerce_before_add_to_cart_button', 'product_sold_count', 10 );
  function product_sold_count() {
   global $product;
   $units_sold = $product->get_total_sales();
   if ( $units_sold ) echo '<p>' . sprintf( __( 'Sold: %s', 'woocommerce' ), $units_sold ) . '</p>';

I will appreciate if anyone helps me in this matter.

I am trying to add order, sale count next to star rating at single product page, my store is built with WordPress and Woocommerce, I have tried a code but it adds this function before add to cart button/form.

Website URL for reference: https://techcart.pk

and image

Code:

add_action( 'woocommerce_before_add_to_cart_button', 'product_sold_count', 10 );
  function product_sold_count() {
   global $product;
   $units_sold = $product->get_total_sales();
   if ( $units_sold ) echo '<p>' . sprintf( __( 'Sold: %s', 'woocommerce' ), $units_sold ) . '</p>';

I will appreciate if anyone helps me in this matter.

Share Improve this question edited Jul 21, 2020 at 0:22 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jul 20, 2020 at 16:45 Asif MughalAsif Mughal 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

For single product page, I tested it with the themes: ShoppingCart and Storefront and it works fine :

in function.php:

function shoppingcart_setup(){

  add_filter('woocommerce_short_description', function(){
    
     global $product;
     $units_sold = $product->get_total_sales();
     if ( $units_sold ) return '<p>' . sprintf( __( 'Sold: %s', 'woocommerce' ), $units_sold ) . '</p>';             
        
  }, 10, 2);
}
add_action( 'after_setup_theme', 'shoppingcart_setup' );

Note: If you checked: Manage Stock, this code does not work

本文标签: Add sale count next to star rating in woocommerce single product page