Last active
August 17, 2021 05:57
-
-
Save stuartduff/6f4f17205b847868478dd8164885d3f3 to your computer and use it in GitHub Desktop.
WooCommerce Dynamic Pricing display a table of dynamically generated discounts above the add to cart button http://cld.wthms.co/1ksKo/4u9gVw0f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'woocommerce_before_add_to_cart_button', 'sd_display_bulk_discount_table' ); | |
function sd_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>Quantity</th><th>Discount</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>'.$pricing_rule_sets['rules'][$key]['from']." - ".$pricing_rule_sets['rules'][$key]['to']."</td>"; | |
$tempstring .= '<td><span class="amount">' . $pricing_rule_sets['rules'][$key]['amount'] . "" . $woosymbol . "</span></td>"; | |
$tempstring .= '</tr>'; | |
} | |
} | |
$tempstring .= "</table>"; | |
echo $tempstring; | |
} | |
} |
Great! Works for me, thank you!
Great! Works for me, too. But this only works for percentage discount. It does not work for fixed price or price discount. Can you advise how to make it work for those cases? Thanks!
I have now created a plugin that adds a discount table for bulk pricing and special offers which you can download from below.
https://github.com/stuartduff/woocommerce-dynamic-pricing-table
cc: @Cronbread @onegent
Have made a solution for multible Tables with headings. Get the headings dynamicly from a ACF-repeater field....
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Outputs tables for products with multiple variables nicely, but without variable-combination "headings". Is there a simple way to preface each table with variable-combination names so that the long list of prices is broken into several tables? Cheers.