Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to apply automatic discount in WooCommerce using dollar amount variable
    primarykey
    data
    text
    <p>In my attempt to find a solution for dollar amount based automatic coupon I am encountering the following problem. First the rules:</p> <ol> <li>If dollar amount less than 53 do nothing.</li> <li>If dollar amount greater than 53 and less than 90 apply "couponX"</li> <li>If dollar amount greater than 90 apply "couponY"</li> </ol> <p>I have been able to get the code to work with the second (2) rule of the code. However the third rule will not be applied. My code below. Any thoughts?</p> <pre><code>// Add coupon when user views cart before checkout add_action('woocommerce_before_cart_table', 'add_coupon_automatically'); add_action('woocommerce_before_checkout_form', 'add_coupon_automatically'); // Check if function? create it. if (!function_exists('add_coupon_automatically')) { function add_coupon_automatically() { global $woocommerce; $eleven = '20plus'; $four = '10plus'; $eleven_minimum = 179; $four_minimum = 89; if($woocommerce-&gt;cart-&gt;get_cart()-&gt;cart_contents_total&gt;$eleven_minimum) { // Apply 20plus if (!$woocommerce-&gt;cart-&gt;add_discount( sanitize_text_field( $eleven ))) $woocommerce-&gt;add_message('20 Plus coupon (11%) automatically applied!'); $woocommerce-&gt;show_messages(); //Recalculate totals $woocommerce-&gt;cart-&gt;calculate_totals(); } elseif ($woocommerce-&gt;cart-&gt;get_cart()-&gt;cart_contents_total&lt;$eleven_minimum &amp;&amp; $woocommerce-&gt;cart-&gt;get_cart()-&gt;cart_contents_total&gt;$four_minimum) { // Apply 10plus if (!$woocommerce-&gt;cart-&gt;add_discount( sanitize_text_field( $four ))) $woocommerce-&gt;add_message('10 Plus coupon (4%) automatically applied!'); $woocommerce-&gt;show_messages(); //Recalculate totals $woocommerce-&gt;cart-&gt;calculate_totals(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload