A
A
Alexander Bondarenko2020-02-25 18:49:16
WordPress
Alexander Bondarenko, 2020-02-25 18:49:16

How to add cart items to woocommerce orders?

How can I add products to woocommerce orders via code? I have a code for displaying goods in the cart, I need to add an order but without payment, the order comes, but the status is constantly paid and there is no data about the person, what's the problem?

<?
$order_args = array(
'status' => '',
'customer_id' => get_current_user_id(), // id пользователя который сделал заказ
'customer_note' => null,
'order_id' => 0,
'created_via' => '',
'parent' => 0
);
$order = wc_create_order($order_args);
// Получаем и сразу же добавляем данные о продукте который покупает пользователь get_current_user_id()
$order->add_product(get_product(28), 1);
// Заполняем информацию о пользователе который сделал покупку и информацию о доставке
$address_billing = array(
'first_name' => 'Name',
'last_name' => 'LastName',
'email' => get_user_meta(get_current_user_id(), 'billing_email', true),
'phone' => get_user_meta(get_current_user_id(), 'billing_phone', true)
);
$order->set_address($address_billing, 'pending');
$order->update_status('processing');
// Обновим данные о покупке и рассчитаем итоговою стоимость
$order->calculate_shipping();
?>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question