title: WooCommerce custom Ajax add to cart: How to set a custom price?
body: <p>I am trying to add a product from an on_click event at checkout.
  That product's price is calculated dynamically with an external API so it is registered as having a price of 0 in woocommerce itself. I am trying to add this product to the cart (working) and then set that product's price to the one I receive from the API (not working). Here is the relevant code:</p>
  <p>AJAX call on click:</p>
  <pre><code>       jQuery('#theftdaminsurance').on('click', function(){
          jQuery.ajax({
          type: &quot;POST&quot;,
          url: &quot;/wp-admin/admin-ajax.php&quot;,
          data: {
              action: 'add_product_to_cart_checkout',
              // add your parameters here
              price: &lt;?php echo $year_dam_price; ?&gt;,
              productid: ins_product_id
          },
          success: function (output) {
          document.getElementById(&quot;loaderdiv&quot;).style.display = &quot;none&quot;;
          jQuery(document.body).trigger(&quot;update_checkout&quot;);
          }
          });
          });
  </code></pre>
  <p>I checked everything here and the call is made properly with all the right info being passed on.
  Here is the function being called:</p>
  <pre><code>add_action('wp_ajax_add_product_to_cart_checkout', 'add_product_to_cart_checkout');
  
  // register the ajax action for unauthenticated users
  add_action('wp_ajax_nopriv_add_product_to_cart_checkout', 'add_product_to_cart_checkout');
  
  function add_product_to_cart_checkout() {
      $product_id = $_REQUEST['productid'];
      $price = floatval($_REQUEST['price']);
      $ebike_ids = array(17386,17385,17382,17378,17375,17372,17370,17369,17364,16132,16130,4561,4550,3490,2376);
      $found = false;
      //check if there is something in cart
      if ( sizeof( WC()-&gt;cart-&gt;get_cart() ) &gt; 0 ) {
          //Delete all preexisting insurance products and find qty of ebikes
          foreach ( WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $cart_item ) {
              $product_idebike = $cart_item['product_id'];
              if ( ($cart_item['product_id'] == &quot;16600&quot;) || ($cart_item['product_id'] == &quot;16653&quot;) || ($cart_item['product_id'] == &quot;16654&quot;) || ($cart_item['product_id'] == &quot;16655&quot;) ||($cart_item['product_id'] == &quot;16659&quot;) || ($cart_item['product_id'] == &quot;16660&quot;)) {
                          WC()-&gt;cart-&gt;remove_cart_item( $cart_item_key );
              }           
          //get ebike quantity
      for ($x = 0; $x &lt;= 14; $x++) {
          if ($product_idebike == $ebike_ids[$x]) {
              $quantity =  $cart_item['quantity'];
              break;
          }
      }
          }
              // if command is not to remove add relevant products
                  if ($price != &quot;-1&quot;) {
                  WC()-&gt;cart-&gt;add_to_cart( $product_id, $quantity );
                  foreach( WC()-&gt;cart-&gt;get_cart() as $cart_item ){
                      $id = $cart_item['product_id'];
                      if ($product_id == $id) {
                          $cart_item['data']-&gt;set_price( $price );
                      }
                  }
                  }
              
      }
  
  }
  </code></pre>
  <p>Here I basically debugged everythingcarefully and everything behaves as it should but the set_price function. I cannot get it to update the product's price to the one passed in the AJAX call. The price remains 0 once the checkout is updated and before it is as well. Am i calling or using this function the wrong way?</p>
  <p>Thanks a lot for your help!</p>