Skip to content

Instantly share code, notes, and snippets.

@robertdevore
Last active May 27, 2019 23:47
Show Gist options
  • Save robertdevore/aa6b00b7e53f514e0e6c3a72eeaac48d to your computer and use it in GitHub Desktop.
Save robertdevore/aa6b00b7e53f514e0e6c3a72eeaac48d to your computer and use it in GitHub Desktop.
Add a product to an order in WooCommerce programatically
<?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