show discount info
-
is there a way to automatically show the discount info in a table at the product single info page?
-
??
There are a bunch of ways to do it but I added a filter on the get_price_html
Here is my code which I have tested for the percentage option not fixed or flat.
Should give you the basic idea.
I edited the Bulk Discount plugin directly which is probably not advisable. should put it in a separate plugin.
add_filter( 'woocommerce_get_price_html', array( $this, 'filter_get_price_html' ), 10, 2);
/** * Hook to woocommerce_get_price_html filter. * * @param $price * @param $_product * @return string */ public function filter_get_price_html( $price , $_product) { if ( !$_product ) { return $price; } if ( $this->coupon_check() ) { return $price; } if ( get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) != '' && get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) !== 'yes' ) { return $price; } $bulkDiscountType = get_option( 'woocommerce_t4m_discount_type', '' ); /* if ($bulkDiscountType == 'flat'){ echo '<p>Flat bulk discount</p>'; } else if ($bulkDiscountType == 'fixed') { echo '<p>Fixed bulk discount</p>'; } else { echo '<p>Percentage bulk discount</p>'; } */ //get the array of discounts $origPrice = $_product->get_price(); //echo "<p>original Price = $origPrice </p>"; $q = array( 0.0 ); $d = array( 0.0 ); $priceList = array(0.0); $product_id = $_product->get_id(); //echo "<p>product ID = $product_id </p>"; /* get all the possible discounts */ for ( $i = 1; $i <= 5; $i++ ) { array_push( $q, get_post_meta( $product_id, "_bulkdiscount_quantity_$i", true ) ); if ( $bulkDiscountType == 'flat' ) { array_push( $priceList, get_post_meta( $product_id, "_bulkdiscount_discount_flat_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_flat_$i", true ) : 0.0 ); } else if ( $bulkDiscountType == 'fixed' ) { array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_fixed_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_fixed_$i", true ) : 0.0 ); array_push( $priceList, $origPrice - $d[$i] ); } else { array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_$i", true ) : 0.0 ); array_push( $priceList, $origPrice * min( 1.0, max( 0, ( 100.0 - round( $d[$i], 2 ) ) / 100.0 ) )); } } $priceTable = <<<TABLEOPEN <table class="woocommerce-bulk-pricing-table"> <tr> <th>Quantity</th> <th>Price</th> </tr> <tr> <td>1</td> <td>$price</td> </tr> TABLEOPEN; for ( $j = 1; $j <= 5; $j++ ) { if ($q[$j] > 0.0) { $priceTable .="<tr>\n"; $priceTable .="<td>$q[$j]</td>\n"; $priceTable .="<td>".wc_price($priceList[$j]); if ($bulkDiscountType == 'flat'){ $priceTable .=" rebate"; } $priceTable .="</td>\n"; $priceTable .="</tr>\n"; } } $priceTable .="</table>\n"; return $priceTable; }
thanks for the reply,
but as i really don’t know how to change your code to one for fixed discount, i would prefer if the plugin creator would respond hahaha ??
but thanks anyway!
I did make it work for fixed and flat but not sure if I got them correct I just haven’t tested. I assumed that fixed meant you specify the price above a certain quantity. e.g. 1 = $2.00 10 = $1.83
flat was once you reached a certain quantity you got money back e.g. 10$ off if you buy 100If the creator is OK with my code, I am willing to put in the work to add this feature properly into the plugin.
Leo
should i add complete code in functions.php?
I fixed it so that it doesn’t display on admin pages and this code will work in functions.php
add_filter( 'woocommerce_get_price_html', 'filter_get_price_html', 10, 2); /** * Hook to woocommerce_get_price_html filter. * * @param $price * @param $_product * @return string */ function filter_get_price_html( $price , $_product) { if ( !$_product ) { return $price; } if (is_admin()){ return $price; } global $woocommerce; if ( get_option( 'woocommerce_t4m_remove_discount_on_coupon', 'yes' ) == 'yes' && !( empty( $woocommerce->cart->applied_coupons ) )) { return $price; } /* if ( $this->coupon_check() ) { return $price; } */ if ( get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) != '' && get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) !== 'yes' ) { return $price; } $bulkDiscountType = get_option( 'woocommerce_t4m_discount_type', '' ); /* if ($bulkDiscountType == 'flat'){ echo '<p>Flat bulk discount</p>'; } else if ($bulkDiscountType == 'fixed') { echo '<p>Fixed bulk discount</p>'; } else { echo '<p>Percentage bulk discount</p>'; } */ //get the array of discounts $origPrice = $_product->get_price(); //echo "<p>original Price = $origPrice </p>"; $q = array( 0.0 ); $d = array( 0.0 ); $priceList = array(0.0); $foundPriceBreak = FALSE; $product_id = $_product->get_id(); //echo "<p>product ID = $product_id </p>"; /* get all the possible discounts */ for ( $i = 1; $i <= 5; $i++ ) { array_push( $q, get_post_meta( $product_id, "_bulkdiscount_quantity_$i", true ) ); if ( $bulkDiscountType == 'flat' ) { array_push( $priceList, get_post_meta( $product_id, "_bulkdiscount_discount_flat_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_flat_$i", true ) : 0.0 ); } else if ( $bulkDiscountType == 'fixed' ) { array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_fixed_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_fixed_$i", true ) : 0.0 ); array_push( $priceList, $origPrice - $d[$i] ); } else { array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_$i", true ) : 0.0 ); array_push( $priceList, $origPrice * min( 1.0, max( 0, ( 100.0 - round( $d[$i], 2 ) ) / 100.0 ) )); } } $priceTable = <<<TABLEOPEN <table class="woocommerce-bulk-pricing-table"> <tr> <th>Quantity</th> <th>Price</th> </tr> <tr> <td>1</td> <td>$price</td> </tr> TABLEOPEN; for ( $j = 1; $j <= 5; $j++ ) { if ($q[$j] > 0.0) { $foundPriceBreak = TRUE; $priceTable .="<tr>\n"; $priceTable .="<td>$q[$j]</td>\n"; $priceTable .="<td>".wc_price($priceList[$j]); if ($bulkDiscountType == 'flat'){ $priceTable .=" rebate"; } $priceTable .="</td>\n"; $priceTable .="</tr>\n"; } } $priceTable .="</table>\n"; if ($foundPriceBreak){ return $priceTable; } return $price; }
thanks a lot! this works great!
Hi Leo, great and working code for single products, without variations. Do you have an idea how it could be modified to work also with different product variations? Thanks for your time! Adam
Hi Leo, it is a great code, but I just need something lite. If you could give an advice, how to show “from – to” price, on the shop page and product page. For example 100$-300$, where 100$ is lowest price and 300$ is a biggest price for variable products.
Thank you a lot in advance!Basil
- The topic ‘show discount info’ is closed to new replies.