Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. $OrderOBJ = wc_get_order(2343);
  2. $item = new WC_Order_Item_Shipping();
  3.  
  4. $new_ship_price = 10;
  5.  
  6. $shippingItem = $OrderOBJ->get_items('shipping');
  7.  
  8. $item->set_method_title( "Amazon shipping rate" );
  9. $item->set_method_id( "amazon_flat_rate:17" );
  10. $item->set_total( $new_ship_price );
  11. $OrderOBJ->update_item( $item );
  12.  
  13. $OrderOBJ->calculate_totals();
  14. $OrderOBJ->save()
  15.  
  16. $order_id = 2343;
  17. $order = wc_get_order($order_id);
  18. $cost = 10;
  19. $items = (array) $order->get_items('shipping');
  20. $country = $order->get_shipping_country();
  21.  
  22. // Set the array for tax calculations
  23. $calculate_tax_for = array(
  24. 'country' => $country_code,
  25. 'state' => '', // Can be set (optional)
  26. 'postcode' => '', // Can be set (optional)
  27. 'city' => '', // Can be set (optional)
  28. );
  29.  
  30. if ( sizeof( $items ) == 0 ) {
  31. $item = new WC_Order_Item_Shipping();
  32. $items = array($item);
  33. $new_item = true;
  34. }
  35.  
  36. // Loop through shipping items
  37. foreach ( $items as $item ) {
  38. $item->set_method_title( __("Amazon shipping rate") );
  39. $item->set_method_id( "amazon_flat_rate:17" ); // set an existing Shipping method rate ID
  40. $item->set_total( $cost ); // (optional)
  41.  
  42. $item->calculate_taxes( $calculate_tax_for ); // Calculate taxes
  43.  
  44. if( isset($new_item) && $new_item ) {
  45. $order->add_item( $item );
  46. } else {
  47. $item->save()
  48. }
  49. }
  50. $order->calculate_totals();
  51. $order->save();
  52.  
  53. $order_id = 2343;
  54. $order = wc_get_order($order_id);
  55. $items = (array) $order->get_items('shipping');
  56.  
  57. if ( sizeof( $items ) > 0 ) {
  58. // Loop through shipping items
  59. foreach ( $items as $item_id => $item ) {
  60. $order->remove_item( $item_id );
  61. }
  62. $order->calculate_totals();
  63. $order->save();
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement