Reputation: 5732
I'm creating a website which handles payment from stripe using wp simple pay plugin.
I created a webhook that will allow me to know if the payment is successful or not. If it is success, I will create an order that has data in it but postman always returning 500 Internal Server Error and I cannot see the error from it.
If I remove the wc_create_order()
and return the $address, it worked perfectly. I am suspecting that I've doing something wrong in my code.
Here's the code I'd created
add_action('woocommerce_checkout_process', 'pinion_add_order');
function pinion_add_order($m, $a) {
global $woocommerce;
$address = array(
'first_name' => 'Project Paid ',
'last_name' => $m
);
$order = wc_create_order();
$order->add_product(($a == '100000' ? get_product('2858') : get_product('2859')), 1);
$order->set_address($address, 'billing');
$order->set_address($address, 'shipping');
// $order->set_total($amount);
$order->calculate_totals();
$order->update_status("Completed", 'Imported order', TRUE);
return $order;
}
Any help would be appreciated. Thanks
Upvotes: 1
Views: 5260
Reputation: 469
Although LoicTheAztek is correct and there is probably a better way of doing this, the answer to your question is that you haven't saved the Order object.
So before your return statement, try adding $order->save();
This will actually save the values to the database.
If that doesn't work, you may also need to adding some additional properties, such as a payment method using $order->set_payment_method($string);
Hope that helps.
Upvotes: 2
Reputation: 1502
I hope the below code help you.
function pinion_add_order() {
global $current_user;
$a = '100000';
$order = wc_create_order();
$order->add_product(($a == '100000' ? get_product('2858') : get_product('2859')), 1);
//$order->add_product( $_product, $item_quantity );
$order->calculate_totals();
$fname = get_user_meta( $current_user->ID, 'first_name', true );
$lname = get_user_meta( $current_user->ID, 'last_name', true );
$email = $current_user->user_email;
$address_1 = get_user_meta( $current_user->ID, 'billing_address_1', true );
$address_2 = get_user_meta( $current_user->ID, 'billing_address_2', true );
$city = get_user_meta( $current_user->ID, 'billing_city', true );
$postcode = get_user_meta( $current_user->ID, 'billing_postcode', true );
$country = get_user_meta( $current_user->ID, 'billing_country', true );
$state = get_user_meta( $current_user->ID, 'billing_state', true );
$billing_address = array(
'first_name' => $fname,
'last_name' => $lname,
'email' => $email,
'address_1' => $address_1,
'address_2' => $address_2,
'city' => $city,
'state' => $state,
'postcode' => $postcode,
'country' => $country,
);
$address = array(
'first_name' => $fname,
'last_name' => $lname,
'email' => $email,
'address_1' => $address_1,
'address_2' => $address_2,
'city' => $city,
'state' => $state,
'postcode' => $postcode,
'country' => $country,
);
$shipping_cost = 5;
$shipping_method = 'Fedex';
$order->add_shipping($shipping_cost);
$order->set_address($billing_address,'billing');
$order->set_address($address,'shipping');
$order->set_payment_method('check');//
$order->shipping_method_title = $shipping_method;
$order->calculate_totals();
$order->update_status('on-hold');
$order->save();
}
add_action('woocommerce_checkout_process', 'pinion_add_order'); //it does not take any parameters.
For more help please see the file and class structure in file : \wp-content\plugins\woocommerce\includes\class-wc-checkout.php
Upvotes: 1