$order = wc_create_order();
$order_id = trim(str_replace('#', '', $order->get_order_number()));
update_post_meta($order_id, '_customer_user', $user_id);
$linked_product = (int)get_post_meta( $course_id, 'linked_product', true );
$order->add_product( get_product($linked_product), 1); // This is an existing SIMPLE product
$coupon_code = $rec_coupon;
$coupon = new WC_Coupon($coupon_code);
$coupon_post = get_post($coupon->id);
if($coupon_post) {
$discount_total = $coupon->get_amount();
$item = new WC_Order_Item_Coupon();
$item->set_props(array('code' => $coupon_code, 'discount' => $discount_total, 'discount_tax' => 0));
$order->add_item($item);
$order->save();
foreach ($order->get_items() as $order_item) {
$total = $order_item->get_total();
$order_item->set_subtotal($total);
$calc_total = $total - $discount_total;
$calc_total = ($calc_total > 0) ? $calc_total : 0;
$order_item->set_total($calc_total);
$order_item->save();
}
$order->save();
}
$order->calculate_totals();
$order->update_status("completed", 'order_note', true);
Last active
November 21, 2019 08:31
-
-
Save saodat1998/b300d885a05c652c1d2f820b5ecbbab6 to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment