Skip to content

Instantly share code, notes, and snippets.

@solepixel
Last active April 20, 2018 19:11
Show Gist options
  • Save solepixel/7048531 to your computer and use it in GitHub Desktop.
Save solepixel/7048531 to your computer and use it in GitHub Desktop.
dynamic pricing table
<?php
function display_dynamic_pricing_table(){
global $post;
# see line 42 of woocommerce_pricing_by_product.class.php
$pricing_rule_sets = get_option('_a_category_pricing_rules', array());
$found = false;
if(count($pricing_rule_sets)){
global $woocommerce_pricing;
foreach ($pricing_rule_sets as $pricing_rule_set) {
// check for category
$collector = isset($pricing_rule_set['collector']) ? $pricing_rule_set['collector'] : array();
if($collector){
if($collector['type'] == 'cat_product'){
$in = false;
foreach($collector['args']['cats'] as $cat){
if(has_term($cat, 'product_cat', $post->ID)) $in = true;
}
if(!$in) continue;
}
}
$pricing_rule_sets = array(array('rules' => $pricing_rule_set['rules']));
$found = true;
break;
}
}
if(!$found){
$pricing_rule_sets = NULL;
}
$product_pricing_rule_sets = get_post_meta($post->ID, '_pricing_rules', true);
if($product_pricing_rule_sets){
$pricing_rule_sets = $product_pricing_rule_sets;
}
if(count($pricing_rule_sets)){
$pricing = '';
foreach($pricing_rule_sets as $ruleset){
$rules = $ruleset['rules'];
foreach($rules as $rule){
$pricing .= '<tr>';
$pricing .= '<td class="col1">'.$rule['from'];
$pricing .= $rule['to'] ? ' - '.$rule['to'] : '+';
$pricing .= '</td>';
$pricing .= '<td>$'. number_format($rule['amount'], 2).'</td>';
$pricing .= '</tr>';
}
}
if($pricing){
$output = <<<DYNAMICPRICING
<div class="dynamic-pricing">
<table class="pricing-table">
<thead>
<tr>
<th class="col1">quantity</th>
<th>price</th>
</tr>
</thead>
<tbody>
$pricing
</tbody>
</table>
</div><!-- .dynamic-pricing -->
DYNAMICPRICING;
echo $output;
}
}
}
@johnzhel
Copy link

johnzhel commented Aug 2, 2016

Where should I put this php file to make it work? Thank you for your help.

@ahait
Copy link

ahait commented Mar 19, 2018

NICE WORK!!! This was a real time-consuming issue for me, especially the general category discounts. I didn't know how to call upon them and use them for calculation. But you've done it here. Thanks.

@sam0389
Copy link

sam0389 commented Apr 20, 2018

I am sorry but this code doesn't seem to be working for me, I cannot find the woocommerce_pricing_by_product.class.php file anywhere and I figure that is why it isn't working. any help will be greatly appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment