Reputation: 166
I am trying to create one plugin for crowdfunding site on wordpress. On my site i have different projects (products) and users can donate money. They can choose how much they want to donate. Now i need to create orders from my plugin for some project. I found this code
function create_vip_order() {
global $woocommerce;
$address = array(
'first_name' => '111Joe',
'last_name' => 'Conlin',
'company' => 'Speed Society',
'email' => '[email protected]',
'phone' => '760-555-1212',
'address_1' => '123 Main st.',
'address_2' => '104',
'city' => 'San Diego',
'state' => 'Ca',
'postcode' => '92121',
'country' => 'US',
);
// Now we create the order
$order = wc_create_order();
// The add_product() function below is located in
// /plugins/woocommerce/includes/abstracts/abstract_wc_order.php
// This is an existing SIMPLE product
$order->add_product( wc_get_product( '5617' ), 100 );
$order->set_address( $address, 'billing' );
$order->calculate_totals();
$order->update_status("Completed", 'Imported order', TRUE);
}
add_action( 'init', 'create_vip_order' );
and all works good , but i need to insert also value that user want to donate.
Now this script insert only adress and total 0,00
Please help me to solve this problem.
i did:
$test = 30000;
$order-> ->set_total($test, 'order_discount');
but this didnt helped me
Upvotes: 1
Views: 1018
Reputation: 166
i use this code to set price to product and after it to create an order global $woocommerce;
$product_id = '5617'; // a product ID or a variation ID
$new_product_price = $_POST['amount']; // the new product price <==== <==== <====
$quantity = 1; // The line item quantity
## - - - - - - - - - - - - - - - - - - - - - - - - - - ##
// Get an instance of the WC_Product object
$product = wc_get_product( $product_id );
// Change the product price
$product->set_price( $new_product_price );
## - - - - - - - - - - - - - - - - - - - - - - - - - - ##
// Create the order
$order = wc_create_order();
// Add the product to the order
$order->add_product( $product, $quantity);
## You will need to add customer data, tax line item … ##
$order->calculate_totals(); // updating totals
$order->save(); // Save the order data
Upvotes: 2