Skip to content
Advertisement

Create order programatically upon form submission OVERIDE PRODUCT PRICE – WooCommerce

I have a code that I want to create an order in WooCommerce programatically upon form submission.

I would like my code to determine the product price – so override the default price of the product.

Is this possible?

Not sure where I am going wrong with my code:

// set some variables
$user_id =rgar( $entry, '97' );
$product_id = rgar( $entry, '71' );
$quantity = rgar( $entry, '73' ); 
$price = rgar( $entry, '90' );
$note = rgar( $entry, '53' );

$product = wc_get_product($product_id);
            
$address = array( 'first_name' => rgar( $entry, '98' ), 'last_name' => rgar( $entry, '99' ), 'company' => rgar( $entry, '' ), 'email' => rgar( $entry, '83' ), 'phone' => rgar( $entry, '84' ), 'address_1' => rgar( $entry, '88.1' ), 'address_2' => rgar( $entry, '88.2' ), 'city' => rgar( $entry, '88.3' ), 'state' => rgar( $entry, '88.4' ), 'postcode' => rgar( $entry, '88.5' ), 'country' => rgar( $entry, '88.6' ), );
// Create the order object 
            $order = wc_create_order();
$order->set_customer_id( $user_id );

$order->add_product( wc_get_product($product_id), $quantity, $prices);

foreach ($order->get_items() as $item_key => $item ) {
    $item->add_meta_data( 'Label', $note, true );
}

$order->set_address( $address, 'billing' );
$order->calculate_totals();
$order->update_status( 'pending payment', 'pending', TRUE); 

$order->add_order_note( $note );

                                            $coupon_code = rgar( $entry, '105' ); $order->apply_coupon($coupon_code);                                       
            break;
    
}}

The ’90’ is the field ID of my form with the price I want to use in the order for the product.

Advertisement

Answer

Use: WC_Product::set_price() – Set the product’s active price.

https://woocommerce.wp-a2z.org/oik_api/wc_productset_price/


Next line

$order->add_product( wc_get_product($product_id), $quantity, $prices);

Is going to be

$order->add_product( $product, $quantity );

Because you had already used wc_get_product

$product = wc_get_product($product_id);

Then you get

// set some variables
$user_id =rgar( $entry, '97' );
$product_id = rgar( $entry, '71' );
$quantity = rgar( $entry, '73' ); 
$price = rgar( $entry, '90' );
$note = rgar( $entry, '53' );

$product = wc_get_product($product_id);

// Set price
$product->set_price( $price );

$address = array(
    'first_name' => rgar( $entry, '98' ),
    'last_name' => rgar( $entry, '99' ), 
    'company' => rgar( $entry, '' ),
    'email' => rgar( $entry, '83' ), 
    'phone' => rgar( $entry, '84' ), 
    'address_1' => rgar( $entry, '88.1' ), 
    'address_2' => rgar( $entry, '88.2' ), 
    'city' => rgar( $entry, '88.3' ), 
    'state' => rgar( $entry, '88.4' ), 
    'postcode' => rgar( $entry, '88.5' ), 
    'country' => rgar( $entry, '88.6' ),
);

// Create the order object 
$order = wc_create_order();
$order->set_customer_id( $user_id );

// add product to order
$order->add_product( $product, $quantity );

foreach ($order->get_items() as $item_key => $item ) {
    $item->add_meta_data( 'Label', $note, true );
}

$order->set_address( $address, 'billing' );
$order->calculate_totals();
$order->update_status( 'pending payment', 'pending', TRUE); 

$order->add_order_note( $note );
$coupon_code = rgar( $entry, '105' );
$order->apply_coupon($coupon_code);  
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement