Display discount on product page
-
I liked the plugin so I created a function to show the discount to single prodcut page. Add it to functions.php of your theme
// for Bulk discounts add_filter( 'woocommerce_variable_price_html', 'webexpert_apply_to_productpage', 10, 2 ); add_filter( 'woocommerce_get_price_html', 'webexpert_apply_to_productpage', 10, 2 ); function webexpert_apply_to_productpage( $price, $product ) { $original_product=$product; $q = array( 0.0 ); $d = array( 0.0 ); $quantity=1; $configurer = get_page_by_title( 'wc_bulk_discount_configurer', OBJECT, 'product' ); if ( $configurer && $configurer->ID && $configurer->post_status == 'private' ) { $product_id = $configurer->ID; } if ($product instanceof WC_Product_Variation) { $product_id = $product->get_parent_id(); }else { $product->get_id(); } /* Find the appropriate discount coefficient by looping through up to the five discount settings */ for ( $i = 1; $i <= 5; $i++ ) { array_push( $q, get_post_meta( $product_id, "_bulkdiscount_quantity_$i", true ) ); if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) { array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_flat_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_flat_$i", true ) : 0.0 ); } else if ( get_option( 'woocommerce_t4m_discount_type', '' ) == '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 ); } else { array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_$i", true ) : 0.0 ); } if ( $quantity >= $q[$i] && $q[$i] > $q[0] ) { $q[0] = $q[$i]; $d[0] = $d[$i]; } } // for percentage discount convert the resulting discount from % to the multiplying coefficient if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'fixed' ) { $coeff= max( 0, $d[0] * $quantity ); } $coeff=( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ? max( 0, $d[0] ) : min( 1.0, max( 0, ( 100.0 - round( $d[0], 2 ) ) / 100.0 ) ); if ($product instanceof WC_Product_Variation) { $min_var_reg_price = $original_product->get_variation_sale_price( 'min', true ); }else { $min_var_reg_price=$product->get_price(); } $min_var_sale_price = $min_var_reg_price * $coeff ; if ( $min_var_sale_price!=$min_var_reg_price ) { $price = sprintf( __( '<del>%1$s</del><ins>%2$s</ins>', 'woocommerce' ), wc_price( $min_var_reg_price ), wc_price( $min_var_sale_price ) ); } else { $price = sprintf( __( '%1$s', 'woocommerce' ), wc_price( $min_var_reg_price ) ); } return $price;
Viewing 10 replies - 1 through 10 (of 10 total)
Viewing 10 replies - 1 through 10 (of 10 total)
- The topic ‘Display discount on product page’ is closed to new replies.