Skip to content

Instantly share code, notes, and snippets.

@greguly
Last active August 16, 2021 08:41
Show Gist options
  • Save greguly/7212457 to your computer and use it in GitHub Desktop.
Save greguly/7212457 to your computer and use it in GitHub Desktop.
Woocommerce Dynamic Pricing table price view
add_filter( 'woocommerce_get_price_html', 'omniwp_credit_dollars_price', 10, 2 );
function omniwp_credit_dollars_price( $price, $product ) {
$pricing_rule_sets = get_post_meta( $product->post->ID, '_pricing_rules', true );
$pricing_rule_sets = array_shift( $pricing_rule_sets );
if ( $pricing_rule_sets
&& is_array( $pricing_rule_sets )
&& sizeof( $pricing_rule_sets ) ) {
ob_start();
?>
<table>
<thead>
<tr>
<th><?php _e('Quantity', 'omniwp_core_functionality' ) ?></th>
<th><?php _e('Price', 'omniwp_core_functionality' ) ?></th>
</tr>
</thead>
<?php
foreach ( $pricing_rule_sets['rules'] as $key => $value ) {
if ( '*' == $pricing_rule_sets['rules'][$key]['to'] ) {
?>
<tr>
<td><?php printf( __( '%s units or more', 'omniwp_core_functionality' ) , $pricing_rule_sets['rules'][$key]['from'] ) ?></td>
<td><?php echo woocommerce_price( $pricing_rule_sets['rules'][$key]['amount'] ); ?></td>
</tr>
<?php
} else {
?>
<tr>
<td><?php printf( __( 'From %s to %s units', 'omniwp_core_functionality' ) , $pricing_rule_sets['rules'][$key]['from'], $pricing_rule_sets['rules'][$key]['to'] ) ?></td>
<td><?php echo woocommerce_price( $pricing_rule_sets['rules'][$key]['amount'] ); ?></td>
</tr>
<?php
}
}
?>
</table>
<?php
$price = ob_get_clean();
}
return $price;
}
?>
@eiertee
Copy link

eiertee commented Feb 5, 2018

Thx for this! Quick question : the provided code also forces showing the table on the product catalogue page - how can we keep the table showing on single product page only? Thank you!!

@senoux
Copy link

senoux commented Mar 31, 2021

@pamolade for decimals correct, i change on line 235 to:
case 'fixed_price':
$output .= '' . sprintf( $pricing_rule_sets['rules'][$key]['amount'] ) . __( ' Per item', 'woocommerce-dynamic-pricing-table' ) . '';
break;

@greguly
Copy link
Author

greguly commented Mar 31, 2021

Hiya,

I am amazed that this piece of code was used by others.

Where do you see line 235 @senoux?

Cheers,
Gabriel

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