0

Looking for some help to show the total number of an item sold on the product page, but when it's less than 2 to have a prompt to be the first with a coupon code.

This is the code I have so far for theme functions

add_action( 'woocommerce_single_product_summary', 'my_units_sold_counts', 19 );
function my_units_sold_counts($units_sold) {
    global $product;
    $units_sold = get_post_meta( $product->id, 'total_sales', true );
    return $units_sold;
}
   function soldUnits ($units_sold) {
if ($units_sold <= 2 ) {

  echo '<p style="font-size: 1.1em; font-weight: 900; color: #908c8c;">No units sold yet. Be the first and use coupon code: xyz</p>';
} else {
 echo '<p style="font-size: 1.1em; font-weight: 900; color: #908c8c;">' . '<i style="margin-right: 10px;"class="fa fa-line-chart"></i>' . sprintf( __( 'Units Sold: %s', 'woocommerce' ), $units_sold ) . '</p>';
}
}

I can't seem to get my head around it and it's hurting.

0

OK, so if anyone else is looking for the answer for this I figured it out after a bit more sleep. I had to get rid of the return and the second function. So the code now looks like this

add_action( 'woocommerce_single_product_summary', 'my_units_sold_counts', 19 );
function my_units_sold_counts($units_sold) {
    global $product;
    $units_sold = get_post_meta( $product->id, 'total_sales', true );

if ($units_sold <= 2 ) {
  echo '<p style="font-size: 1.1em; font-weight: 900; color: #908c8c;">No units sold yet. Be the first and use coupon code: xyz</p>';
} else {
 echo '<p style="font-size: 1.1em; font-weight: 900; color: #908c8c;">' . '<i style="margin-right: 10px;"class="fa fa-line-chart"></i>' . sprintf( __( 'Units Sold: %s', 'woocommerce' ), $units_sold ) . '</p>';
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.