Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

disount table for woocommerce dynamic pricing plugin WordPress

  • SOLVED

I need the solution to this problem:

I use [[LINK href="http://www.woothemes.com/products/dynamic-pricing/"]]woocommerce dynamic pricing plugin by Lucas Stark[[/LINK]] (I can provide the plugin if you need to test), the problem is that it doesn't display the discounts as a table on the product pages which isn't great from a marketing perspective. The problem is highlighted well [[LINK href="http://chrislema.com/the-flaw-in-the-woocommerce-dynamic-pricing-extension/"]]here[[/LINK]]

[[LINK href="http://pastebin.com/mnFpKHCi"]]This code[[/LINK]], found [[LINK href="http://kentzo.wordpress.com/2013/10/16/how-to-display-bulk-discount-table-for-dynamic-pricing/"]]here[[/LINK]] does display a discount table. BUT I use % discounts and this code doesn't work with that, it uses static pricing. e.g. if a product's normal price is $100, 5% off if they buy more than 5, 10% off if they buy 10 or more, shows as price for 5 or more $5 10 or more $10 (i.e. the percentage number discount not the actual price at 5% or 10% off). Clearly this isn't right it should display 5 or more is $95 10 or more is $90 - How do I achieve that?

Lastly from a marketing perspective it would be good if all products with a quantity discount displayed the cheapest price on the main price. e.g a product that is $100 normally, but with 5% off if you buy 5 or more or 10% off if you buy 10 or more should display the discount table as above but should also display from $90 i.e. the cheapest price, as the main price instead of $100 - How do I achieve that?

I need you to provide me with the code that I put in my themes functions.php file to achieve the above.

Answers (4)

2013-11-06

phppoet answers:

Hello , please add the below code at the bottom of your theme's functions.php

add_action('woocommerce_before_add_to_cart_button', 'display_bulk_discount_table');

function display_bulk_discount_table() {
global $woocommerce, $post, $product;
$array_rule_sets = get_post_meta($post->ID, '_pricing_rules', true);
if ($array_rule_sets && is_array($array_rule_sets) && sizeof($array_rule_sets) > 0){
$tempstring .= '<table>';
$tempstring .= '<th>&emsp;&emsp;&emsp;Quantity&emsp;&emsp;&emsp;</th>
<th>&emsp;&emsp;&emsp;Discount&emsp;&emsp;&emsp;</th>';








foreach($array_rule_sets as $pricing_rule_sets)
{

foreach ( $pricing_rule_sets['rules'] as $key => $value ) {

switch ($pricing_rule_sets['rules'][$key]['type']) {
case 'percentage_discount':
$woosymbol='%';
break;
case 'price_discount':
$woosymbol=get_woocommerce_currency_symbol();
break;

}
$tempstring .= '<tr>';
$tempstring .= '<td>&emsp;&emsp;&emsp;'.$pricing_rule_sets['rules'][$key]['from']."- ".$pricing_rule_sets['rules'][$key]['to']."</td>&emsp;&emsp;&emsp;";
$tempstring .= '<td>&emsp;&emsp;&emsp;<span class="amount">'.$pricing_rule_sets['rules'][$key]['amount']."".$woosymbol."</span></td>&emsp;&emsp;&emsp;";
$tempstring .= '</tr>';

}
}
$tempstring .= "</table>";
echo $tempstring;
}
}


It will also show percentage discount .

regards
phppoet


willcm comments:

Whilst this does say what discount they get when they order a certain amount of products, it's not quite what I wanted. If you re-read the question you'll see I wanted the actual price they pay when they order a certain amount of products:

e.g a product worth $100, if they order 5 they get 5% off, if they order 10 they get 10% off.

With your code it displays like this:

Quantity Discount
5-9 5%
10+ 10%

What it should display like is:

Quantity Price
5-9 $95
10+ $90

Also the main price on the page is unchanged it still says $100 What I need it to say IF there is a quantity discount for that product (not otherwise) is from "insert cheapest price here. So in the example I gave above it would say:

From $90 (because the cheapest they can get the $100 product is $90 when they buy 10 or more)


phppoet comments:

I have modified code according to your need . Please try this one

add_action('woocommerce_before_add_to_cart_button', 'display_bulk_discount_table');



function display_bulk_discount_table() {

global $woocommerce, $post, $product;

$array_rule_sets = get_post_meta($post->ID, '_pricing_rules', true);

$_regular_price = get_post_meta($post->ID, '_regular_price', true);

if ($array_rule_sets && is_array($array_rule_sets) && sizeof($array_rule_sets) > 0){

$tempstring .= '&emsp;&emsp;&emsp;Bulk Purchase Savings&emsp;&emsp;&emsp;';
$tempstring .= '<table>';

$tempstring .= '<th>&emsp;&emsp;&emsp;Quantity&emsp;&emsp;&emsp;</th>

<th>&emsp;&emsp;&emsp;Price&emsp;&emsp;&emsp;</th>';





foreach($array_rule_sets as $pricing_rule_sets)

{



foreach ( $pricing_rule_sets['rules'] as $key => $value ) {





$tempstring .= '<tr>';
if ($pricing_rule_sets['rules'][$key]['to']) {
$tempstring .= '<td>&emsp;&emsp;&emsp;'.$pricing_rule_sets['rules'][$key]['from']."- ".$pricing_rule_sets['rules'][$key]['to']."</td>&emsp;&emsp;&emsp;";
} else {
$tempstring .= '<td>&emsp;&emsp;&emsp;'.$pricing_rule_sets['rules'][$key]['from']."+</td>&emsp;&emsp;&emsp;";
}

$finalprice=$_regular_price;
$finalprice -= $pricing_rule_sets['rules'][$key]['amount'];
$tempstring .= '<td>&emsp;&emsp;&emsp;<span class="amount">'.get_woocommerce_currency_symbol().''.$finalprice."</span></td>&emsp;&emsp;&emsp;";

$tempstring .= '</tr>';
}

}

$tempstring .= "</table>";


echo $tempstring;

}

}


regards


phppoet comments:

Please clear your cart before testing. and use 10-20 or 10-100 instead of 10+ .


phppoet comments:

I tested above code on my localhost with 4-5 products and it works as per your expectation . it even works with 10+ type discount pricing (correction in my previous post) .

regards


willcm comments:

okay so the quantity discount table is now showing correctly, but I need the main product price to say from the lowest price.

See here for an example of what I mean:

[[LINK href="http://www.screwfix.com/p/phosphate-twin-thread-drywall-screws-3-5-x-38mm-pack-of-1000/12984"]]http://www.screwfix.com/p/phosphate-twin-thread-drywall-screws-3-5-x-38mm-pack-of-1000/12984[[/LINK]]


willcm comments:

In fact the quantity discount isn't right, the percentage price change isn't working its still showing price reduction not percentage reduction...


phppoet comments:

Okay releasing the final code . Its shows lowest price plus deduction issue is also corrected .


add_action('woocommerce_before_add_to_cart_button', 'display_bulk_discount_table');
add_filter( 'woocommerce_get_price_html', 'wpa83367_price_html', 100, 2 );



function display_bulk_discount_table() {

global $woocommerce, $post, $product;

$array_rule_sets = get_post_meta($post->ID, '_pricing_rules', true);

$_regular_price = get_post_meta($post->ID, '_regular_price', true);

if ($array_rule_sets && is_array($array_rule_sets) && sizeof($array_rule_sets) > 0){

$tempstring .= '&emsp;&emsp;&emsp;Bulk Purchase Savings&emsp;&emsp;&emsp;';
$tempstring .= '<table>';

$tempstring .= '<th>&emsp;&emsp;&emsp;Quantity&emsp;&emsp;&emsp;</th>

<th>&emsp;&emsp;&emsp;Price&emsp;&emsp;&emsp;</th>';





foreach($array_rule_sets as $pricing_rule_sets)

{



foreach ( $pricing_rule_sets['rules'] as $key => $value ) {





$tempstring .= '<tr>';
if ($pricing_rule_sets['rules'][$key]['to']) {
$tempstring .= '<td>&emsp;&emsp;&emsp;'.$pricing_rule_sets['rules'][$key]['from']."- ".$pricing_rule_sets['rules'][$key]['to']."</td>&emsp;&emsp;&emsp;";
} else {
$tempstring .= '<td>&emsp;&emsp;&emsp;'.$pricing_rule_sets['rules'][$key]['from']."+</td>&emsp;&emsp;&emsp;";
}

$finalprice=$_regular_price;
$finalprice = $finalprice - ($finalprice/100)*$pricing_rule_sets['rules'][$key]['amount'];
$tempstring .= '<td>&emsp;&emsp;&emsp;<span class="amount">'.get_woocommerce_currency_symbol().''.$finalprice."</span></td>&emsp;&emsp;&emsp;";

$tempstring .= '</tr>';

}



}

$tempstring .= "</table>";


echo $tempstring;

}

}


function wpa83367_price_html( $finalprice ){
global $woocommerce, $post, $product;
$array_rule_sets = get_post_meta($post->ID, '_pricing_rules', true);
$_regular_price = get_post_meta($post->ID, '_regular_price', true);

foreach($array_rule_sets as $pricing_rule_sets)

{
foreach ( $pricing_rule_sets['rules'] as $key => $value ) {


$finalprice=$_regular_price;
$finalprice = $finalprice - ($finalprice/100)*$pricing_rule_sets['rules'][$key]['amount'];
if($key != count($pricing_rule_sets['rules'])-1) {
return ''.get_woocommerce_currency_symbol().''.$finalprice.'';
}

}
}
}


regards


willcm comments:

Discount table working perfectly - thanks!

From price still showing original and not lowest?


phppoet comments:

Its showing lowest in my localhost . did you copied entire code correctly into functions.php

http://pastebin.com/vr35i66Y

regards