title: Changing WooCommerce cart price after applied coupon code
body: <p>I have created a product on WooCommerce, and added <strong>two options</strong> on product detail page using the hook <strong><code>woocommerce_before_add_to_cart_button</code></strong>. Now when  customers add product to cart from product detail page they have two options their. They can choose one option from these two options.</p>
  
  <p>Then I have stored the user selected value in cart meta using the woocommerce hook woocommerce_add_cart_item_data.</p>
  
  <p>I am using the code from this answer: <a href="https://stackoverflow.com/questions/43423161/save-product-custom-field-radio-button-value-in-cart-and-display-it-on-cart-page/43427686#43427686">Save product custom field radio button value in cart and display it on Cart page</a></p>
  
  <p>This is my code:</p>
  
  <pre><code>// single Product Page options  
  add_action("woocommerce_before_add_to_cart_button", "options_on_single_product");
  function options_on_single_product(){
      $dp_product_id = get_the_ID(); 
      $product_url = get_permalink($dp_product_id);
  
      ?&gt;
          &lt;input type="radio" name="custom_options" checked="checked" value="option1"&gt; option1&lt;br /&gt;
          &lt;input type="radio" name="custom_options" value="option2"&gt; option2
      &lt;?php
  }
  
  
  //Store the custom field
  add_filter( 'woocommerce_add_cart_item_data', 'save_custom_data_with_add_to_cart', 10, 2 );
  function save_custom_data_with_add_to_cart( $cart_item_meta, $product_id ) {
      global $woocommerce;
      $cart_item_meta['custom_options'] = $_POST['custom_options'];
      return $cart_item_meta; 
  }
  </code></pre>
  
  <p>And this is what I have tried:</p>
  
  <pre><code>add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
  function add_custom_price( $cart_obj ) {
  
      if ( is_admin() &amp;&amp; ! defined( 'DOING_AJAX' ) )
          return;
  
      foreach ( $cart_obj-&gt;get_cart() as $key =&gt; $value ) {
          $product_id = $value['product_id'];
          $custom_options = $value['custom_options'];
          $coupon_code = $value['coupon_code'];
          if($custom_options == 'option2')
          {
              if($coupon_code !='')
              {
                  global $woocommerce;
                  if ( WC()-&gt;cart-&gt;has_discount( $coupon_code ) ) return;
                  (WC()-&gt;cart-&gt;add_discount( $coupon_code ))
  
              //code for second discount
              }
              else{
                  $percentage = get_post_meta( $product_id , 'percentage', true );
                  //print_r($value);
                  $old_price = $value['data']-&gt;regular_price;
                  $new_price = ($percentage / 100) * $old_price;
                  $value['data']-&gt;set_price( $new_price );
              }
          } 
      }
  }
  </code></pre>
  
  <p>Now what I am trying to get with that last snippet is:</p>
  
  <ul>
  <li>If Option1 is selected by the customer then woocommerce regular process is run. </li>
  <li>If Option2 is selected then firstly coupon code applied to cart (if code entered by the customer) and then the price is divide by some percentage (stored in product meta) <strong>is applied afterward</strong>.</li>
  </ul>
  
  <p>But it’s not working as expected because the changed product price is maid before and coupon discount is applied after on this changed price.</p>
  
  <p>What I would like is that the coupon discount will be applied first on the product regular  price and then after change this price with my custom product discount.</p>
  
  <p>Is this possible? How can I achieve that?</p>
  
  <p>Thanks.</p>