Last active
May 27, 2019 23:47
-
-
Save robertdevore/aa6b00b7e53f514e0e6c3a72eeaac48d to your computer and use it in GitHub Desktop.
Add a product to an order in WooCommerce programatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Get order data. | |
$order = wc_get_order( 1246 ); | |
// Order total. | |
$total = $order->get_total(); | |
// Add Product data. | |
$product_id = 8; | |
$quantity = 1; | |
$args = ''; | |
// Add Product to order. | |
$order->add_product( wc_get_product( $product_id ), $quantity, $args ); | |
// Get product details. | |
$product = wc_get_product( $product_id ); | |
// Product price. | |
$product_price = $product->get_price(); | |
// New order total. | |
$new_total = ( $product_price * $quantity ) + $total; | |
// Set new order total. | |
$order->set_total( $new_total ); | |
// Save order. | |
$order->save(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment