Search code examples
phpwordpresswoocommerceorders

Edit the price and name of the product in woocommerce order -WORDPRESS


I am trying to create a order and then add products to it which works perfectly. Now I am trying to edit the price and name of the product before adding the product. But for some reason I am not able to do it

Below is what I am trying to do :

     $order = wc_create_order();
    
    $item = wc_get_product( $components[$comp]['component_product_id_2'] );
    //var_dump($item); exit;

   /*_____BELOW IS WHAT I HAVE TRIED NEW_________*/                                 
    $item_args = array(
             'name'         => 'Product A',
             'total'        => wc_get_price_excluding_tax( $item, array( 'qty' => 1, 'price' => 245 ) ),
             );
          
    /*_____ABOVE IS WHAT I HAVE TRIED NEW_________*/ 
                      
    $order->add_product(  $item, $components[$comp]['quantity'], $item_args );

I have tried the above with reference with this add_product info

Without $item_args everything work perfectly. Items gets adding in the woocommerce order. So that means rest of the code is ok, other than $item_args.So I think there's no need to put $components codes.

I want to edit the price and name of the product of the woocom order.


Solution

  • Here's how its done

    $prices = array( 'totals' => array( 
                    'subtotal' => $custom_price, 
                    'total' => $custom_price
                ) );
    
    
    $order->add_product($product, $quantity, $prices);