Okay, my main problem here is that it does not read the if statement, but only the else. The else works fine and shows up on simple product pages, but not on products which have variations. The code looks good, but idk what is preventing it from showing the product price rules for specific quantities for variation products. I would be delighted if anyone has an answer to this or if anyone could show me the way to fix it. I’m using the latest WooCommerce Version 3.0.7.

// Add the function to hook

add_action( 'woocommerce_after_add_to_cart_form', 'show_pricing_rules' );

function show_pricing_rules() {

global $woocommerce, $product, $post, $woocommerce_loop;

$terms = $product->has_child();

if( $terms ) { // this is a variable product

// get the pricing groups

$pricing_groups = get_post_meta( get_the_ID(), '_pricing_rules', true );

// reset variables

$table_count = 0;

// each pricing group

foreach ($pricing_groups as $pricing_rules) {

$table_count ++;

// reset variables

$var_ids = '';

$cfs = '';

$attribute_slugs = '';

$attr_slugs = '';

$variation_names = '';

$name = '';

// get the product variation IDs

$var_ids = $pricing_rules['variation_rules']['args']['variations'];

// getting the product variation name(s) that this pricing rule is assigned to

foreach ($var_ids as $var_id) {

$cfs = get_post_custom($var_id); // get all of the custom fields for this product variation

$attribute_slugs[] = end($cfs); // the last entry in the $cfs array contains the slug(s) of the attributes value(s) that this pricing rule is applied to

// the key of the last array in $cfs is 'atrribute_pa_sex', 'sex' being the attribute name created by us

// thus, the values of this array may also be attained by:

// $attribute_slugs[] = $cfs['attribute_pa_sex'];

}

// getting the array values from inside the main array

foreach ($attribute_slugs as $attr_slugs) {

// cleaning up the slugs, creating array with clean variation names for this pricing rule

foreach ($attr_slugs as $attr_slug) {

$variation_name = str_replace("-", " ", $attr_slug); // replace dashed with spaces

$variation_names[] = ucwords($variation_name); // uppercase the first letter of a word

}

}

?>

<table class="dynamic_pricing_table" id="<?php echo 'table_'.$table_count; ?>">

<tr ><td colspan="2">

<span class="title">

<?php

// reset variables

$count = 0;

// add an "&" between multiple variation names

foreach ($variation_names as $name) {

$count ++;

if ( $count > 1 ) {

    echo " &amp; ".$name;

} else {

  echo $name;

  }

  }

  ?>

  </span></td></tr>

  <tr><th>Quantity</th><th>Cost Per</th></tr>

  <?php

  // get all of the pricing rules

  $rules = $pricing_rules['rules'];

  // each individual pricing rule

  foreach ($rules as $rule) {

  $from = $rule['from'];

  $to = $rule['to'];

  $amount = $rule['amount'];

  ?><tr><td><?php echo $from.' - '.$to; ?></td><td><?php echo $amount; ?></td></tr><?php

  }

  ?>

  <tr><td colspan="2"><small>*Price adjustments will be reflected in the cart.</small></td></tr>

  </table>

  <?php

}

} else { // this is a simple product, no variations

// get all of the the pricing groups

$id = get_the_ID();

$pricing_groups = get_post_meta( $id, '_pricing_rules', true );

// getting the title, replacing spaces with underscores and making it lowercase to use as the table id

$name = get_the_title();

$table_id = str_replace(' ', '_', $name);

$table_id = strtolower($table_id);

// each pricing group

foreach ($pricing_groups as $pricing_rules) : ?>

<table class="dynamic_pricing_table" id="<?php echo $table_id; ?>">

<tr ><td colspan="2"><span class="title"><?php the_title(); ?></span></td></tr>

<tr><th>Quantity</th><th>Cost Per</th></tr>

<?php

// each individual pricing rule

$rules = $pricing_rules['rules'];

foreach ($rules as $rule) :

$from = $rule['from'];

$to = $rule['to'];

$amount = $rule['amount']; ?>

<tr><td><?php echo $from.' - '.$to; ?></td><td><?php echo $amount; ?></td></tr><?php

endforeach;

?>

<tr><td colspan="2"><small>*Price adjustments will be reflected in the cart.</small></td></tr>

</table><?php

endforeach;

}

}

Read more here: Woocommerce display product pricing rules


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: