Support » Plugin: WooCommerce Bulk Discount » 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)
  • Hi
    Im also looking for this solution. I copied the code, but nothing changed on product page. Is it possible I’m missing something?

    Best

    Hi it says there is a curly bracket missing from this code?

    Would really love to be able to display the discount options!

    Add a } on the bottom of the code

    I also include it again here, as I cannot edit my original post

    <?php
    
    // 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;
    }
    • This reply was modified 1 year, 3 months ago by  Web Expert.

    I can’t get this to quite work? I add it in my theme functions but it then won’t allow me to add to the cart or display a price at all? Please help?!

    Hi Seldimi
    If have tried to add it to my functions.php of my theme

    But nothing happens…..?

    Best regards
    AppWeb
    Denmark

    Hi there,

    I got it working by changing something. In Line 20 change

    $product->get_id();
    to
    $product_id = $product->get_id();

    Good Luck!

    Hi Gerdski
    Super…!
    do you know how to make a Price / Discount list Table on Singleproduct page
    with price (data) from WooCommerce Bulk Discount
    Best regards
    AppWeb
    Denmark

    Hello, could someone make the code work? I have tried but it does not work on the product page after changing the quantities.

    • This reply was modified 8 months, 3 weeks ago by  jocampo.
    lepress

    (@lepress)

    @gerdski @seldimi
    Fatal error: Uncaught Error: Call to undefined method WC_Product_Variation::get_variation_sale_price() in

    • This reply was modified 8 months ago by  lepress.

    Hello guys.
    I stopped using this plugin and Woocommerce is udpated regurarly, so the above snippet does not work any more

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Display discount on product page’ is closed to new replies.