I created an order in WooCommerce programmatically using wc_create_order()
, and I also would like to apply coupon in some cases. I did it on my own way, but I'm not sure it's correct:
// Products come in $_GET
foreach($_GET['product'] as $p) {
if(!isset($p['name'])) {
continue;
}
if(!empty($p['name'])) {
// Discount
$discount = (int)$p['discount']/100;
$product->set_name($p['name']);
// I set the discount price
$product->set_price(str_replace(' ', '', $p['price'])*(1-$discount));
$discount_total += str_replace(' ', '', $p['price'])*$discount;
$order->add_product( $product, $p['qty']);
}
}
// I apply the coupon code
if(!empty($discount)) {
$order->add_coupon('vip', $discount_total);
}
It's almost perfect, but I don't see the discount amount under summary and items table.
Is there an other way to apply the coupon?