Reputation: 41
Can not get coupons to apply the discount to an order. (programmatically creating a new order)
Here's the code:
$order = wc_create_order();
$order->add_product( get_product( $pid ), $item['quantity'] ); // pid 8 & qty 1
$order->set_address( $address_billing, 'billing' );
$order->set_address( $address_shipping, 'shipping' );
$order->add_coupon( $discount['code'], ($discount['amount']/100) ); // not pennies (use dollars amount)
$order->set_total( ($discount['amount']/100) , 'order_discount'); // not pennies (use dollar amount)
$order->set_payment_method($this);
$rate = new WC_Shipping_Rate( $response_body['shippingMethodCode'] , $ship_method_title, ($response_body['shippingCost']/100), array(), $response_body['shippingMethodCode'] );
$order->add_shipping( $rate );
$order->calculate_totals();
$return_url = $this->get_return_url( $order );
The order is created in Woocommerce and everything looks good except the coupon code being applied does not reflect the discount amount on the return url thank you page -and- not in the wp-admin when viewing the Woocommerce order -and- not in the New customer order email that gets sent out ....
It does show the coupon code in the wp-admin but the discount line still shows $0 and the total doesn't show any amount subtracted.
Anyone know what is being done wrong here? been at this for a couple weeks now and can't seem to resolve.
Upvotes: 1
Views: 5763
Reputation: 79
You can use WC_Abstract_Order::apply_coupon
<?php
$order = wc_create_order(['customer_id' => $customer_id]);
$order->add_product( wc_get_product(1), 1);
$order->calculate_totals();
$applied = $order->apply_coupon('coupon_code');
if (is_wp_error($applied)) {
//$applied->get_error_message();
}
Upvotes: 1
Reputation: 51
To achieve that, you will need to calculate the discount on each product you add to the order programmatically, passing it as additional parameter. See updated example:
$order = wc_create_order();
$product_to_add = get_product( $pid );
$sale_price = $product_to_add->get_price();
// Here we calculate the final price with the discount
$final_price = round($sale_price * ((100-$discount['amount']) / 100), 2);
// Create the price params that will be passed with add_product(), if you have taxes you will need to calculate them here too
$price_params = array( 'totals' => array( 'subtotal' => $sale_price, 'total' => $final_price ) );
$order->add_product( get_product( $pid ), $item['quantity'], $price_params ); // pid 8 & qty 1
$order->set_address( $address_billing, 'billing' );
$order->set_address( $address_shipping, 'shipping' );
$order->add_coupon( $discount['code'], ($discount['amount']/100) ); // not pennies (use dollars amount)
$order->set_total( ($discount['amount']/100) , 'order_discount'); // not pennies (use dollar amount)
$order->set_payment_method($this);
$rate = new WC_Shipping_Rate( $response_body['shippingMethodCode'] , $ship_method_title, ($response_body['shippingCost']/100), array(), $response_body['shippingMethodCode'] );
$order->add_shipping( $rate );enter code here
$order->calculate_totals();
$return_url = $this->get_return_url( $order );
Upvotes: 5