title: Set a discounted cart item price that is reflected on Orders in Woocommerce 3
body: <p>I use this code in my functions.php to apply a 10% discount on my products, starting from the 2nd one in the cart:</p>
  
  <pre><code>function add_discount_price_percent( $cart_object ) {
  
      global $woocommerce;
  
      $pdtcnt=0;
  
      foreach ($woocommerce-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $cart_item) {
          $pdtcnt++;
  
          $oldprice = 0;
          $newprice = 0;
  
  
          if($pdtcnt&gt;1) { // from second product
  
              $oldprice = $cart_item['data']-&gt;price; //original product price      
  
              // echo "$oldprice&lt;br /&gt;";
  
              $newprice = $oldprice*0.9; //discounted price
              $cart_item['data']-&gt;set_sale_price($newprice);
              $cart_item['data']-&gt;set_price($newprice);
              $cart_item['data']-&gt;set_regular_price($oldprice);
  
          }     
      }
  
      WC()-&gt;cart-&gt;calculate_totals();
  
  }
  
  
  add_action( 'woocommerce_before_cart', 'add_discount_price_percent', 1);
  
  add_action( 'woocommerce_before_checkout_form', 'add_discount_price_percent', 99 );
  </code></pre>
  
  <p>Prices are displayed correctly both in the cart and in the checkout page, but when I test my payment with PayPal sandbox I see and must pay the full price because the discount is ignored. </p>
  
  <p>If I echo the discount prices just before the submit button I get the correct prices:</p>
  
  <pre><code>function echo_discount_before_checkout_submit() {
  
      if ( is_admin() &amp;&amp; ! defined( 'DOING_AJAX' ) )
          return;
  
      foreach ( WC()-&gt;cart-&gt;get_cart() as $key =&gt; $value ) {
          echo $value['data']-&gt;price . "&lt;br /&gt;";
      }
  
  }
  add_action( 'woocommerce_review_order_before_submit', 'echo_discount_before_checkout_submit', 99 );
  </code></pre>
  
  <p>How can I send the correct, discounted prices to PayPal? </p>
  
  <p>EDIT: @LoisTheAtzec reply is really good but I need to get a 10% discount even on first product if quantity is over 2: I'm trying this code but I can't get correct values. </p>
  
  <pre><code>// If it is the first product and quantity is over 1
  if ($count === 1 &amp;&amp; $cart_item['quantity'] &gt;= 2) {
  
          // get unit price
          $unit_price = $cart_item['data']-&gt;get_price();
  
          // get quantity to discount (total - 1)
          $discounted_quantity = $cart_item['quantity'] - 1;
  
          // get total discount amount (on total quantity - 1) 
          $discounted_amount = ($unit_price * $discounted_quantity) * 0.9;
  
          // add first non discounted price to total discount amount
          $total_discounted_price = $unit_price + $discounted_amount;
  
          // distribute discount over total quantity and get new unit price 
          $distributed_unit_discount = $total_discounted_price / $cart_item['quantity'];
  
          // set new unit price
          $cart_item['data']-&gt;set_price($distributed_unit_discount);
      }
  </code></pre>
  
  <p><strong>UPDATE 09-06-2018</strong></p>
  
  <p>I got a strange behavior with logged in users, maybe depending on some conflict between plugins or with the theme I used (Avada): the discount applied twice so I had to prevent this adding this code to my function:</p>
  
  <pre><code>// Set the discounted price on 2nd item and
  add_action('woocommerce_before_calculate_totals', 'add_discount_percentage_on_2nd_item', 999, 1);  
  
  function add_discount_percentage_on_2nd_item($cart) {
      if (is_admin() &amp;&amp; !defined('DOING_AJAX'))
          return;
  
  if ( did_action( 'woocommerce_before_calculate_totals' ) &gt;= 2 )
      return; 
  </code></pre>
  
  <p>Hope it helps.</p>