Skip to content

Instantly share code, notes, and snippets.

@albertpak
Forked from solepixel/dynamic-pricing-table.php
Created October 18, 2013 21:33
Show Gist options
  • Select an option

  • Save albertpak/7048599 to your computer and use it in GitHub Desktop.

Select an option

Save albertpak/7048599 to your computer and use it in GitHub Desktop.
<?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;
}
}
}
@albertpak
Copy link
Copy Markdown
Author

This is for WooCommerce Dynamic Pricing

To display pricing table on product page:

<?php display_dynamic_pricing_table(); ?>

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