Woocommerce: custom price based on user input

后端 未结 4 1515
深忆病人
深忆病人 2021-02-04 08:24

I did not want to post here but I could not find the answer I was looking for and I do not have enough reputation to comment on other VERY SIMILAR questions to get my exact answ

4条回答
  •  礼貌的吻别
    2021-02-04 09:05

    This code will create order with custom price:

    $product = wc_get_product($product_id);
    $order = wc_create_order();
    try {
        $order->add_product($product);
        //$order->set_customer_id($user->ID);
        $order->set_billing_email($customer_email);
    } catch (WC_Data_Exception $e) {
        wp_send_json_error("Failed to create order");
    }
    
    $order->calculate_totals();    
    
    try {
        $order->set_total($custom_price); // $custom_price should be float value
    } catch (WC_Data_Exception $e) {
        wp_send_json_error("Failed to change order details");
    }
    
    $order->save();
    
    $order->update_status( 'completed' );
    

提交回复
热议问题