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;
}
}
}
@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