Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasstark/5f4eb4f24a07545bb1bbb51b3bf810c2 to your computer and use it in GitHub Desktop.
Save lucasstark/5f4eb4f24a07545bb1bbb51b3bf810c2 to your computer and use it in GitHub Desktop.
Output Dynamic Pricing rules in JSON format for Variation Specific Rules.
add_action( 'wp_footer', function () {
if ( ! class_exists( 'WC_Dynamic_Pricing_Table' ) ) {
return;
}
$instance = WC_Dynamic_Pricing_Table::instance();
$array_rule_sets = $instance->get_pricing_array_rule_sets();
$json_prices = array();
if ( $array_rule_sets && is_array( $array_rule_sets ) ) {
if ( apply_filters( 'woocommerce_dynamic_pricing_table_filter_rules', true ) ) {
$valid_rules = apply_filters( 'woocommerce_dynamic_pricing_table_get_filtered_rules', $instance->filter_rulesets( $array_rule_sets ), $array_rule_sets );
} else {
$valid_rules = $array_rule_sets;
}
foreach ( $valid_rules as $pricing_rule_set ) {
if ( $pricing_rule_set['mode'] == 'continuous' ) :
$prices = array();
foreach ( $pricing_rule_set['rules'] as $key => $value ) {
// Checks if a product discount group max quantity field is less than 1.
$max_quantity = 0;
if ( $pricing_rule_set['rules'][ $key ]['to'] < 1 ) {
$max_quantity = 99999999;
} else {
$max_quantity = $pricing_rule_set['rules'][ $key ]['to'];
}
$min_quantity = $pricing_rule_set['rules'][ $key ]['from'];
switch ( $pricing_rule_set['rules'][ $key ]['type'] ) {
case 'price_discount':
$amount = $pricing_rule_set['rules'][ $key ]['amount'];
break;
case 'percentage_discount':
$amount = $pricing_rule_set['rules'][ $key ]['amount'];
break;
case 'fixed_price':
$amount = apply_filters( 'wc_dynamic_pricing_table_get_fixed_price', $pricing_rule_set['rules'][ $key ]['amount'] );
break;
default:
$amount = 0;
break;
}
$prices[ $key ]['from'] = $min_quantity;
$prices[ $key ]['to'] = $max_quantity;
$prices[ $key ]['type'] = $pricing_rule_set['rules'][ $key ]['type'];
$prices[ $key ]['amount'] = $amount;
}
if ( isset( $pricing_rule_set['variation_rules'] ) && ! empty( $pricing_rule_set['variation_rules'] ) ) {
if ( isset( $pricing_rule_set['variation_rules']['args']['variations'] ) && ! empty( $pricing_rule_set['variation_rules']['args']['variations'] ) ) {
foreach ( $pricing_rule_set['variation_rules']['args']['variations'] as $variation_id ) {
$json_prices[ $variation_id ] = $prices;
}
}
}
endif;
}
echo '<script type="text/javascript">';
echo 'var _dynamic_pricing = ' . json_encode($json_prices);
echo '</script>';
}
}, 9999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment