woocommerce/tests/framework/helpers/class-wc-helper-order.php

104 lines
2.6 KiB
PHP
Raw Normal View History

2015-05-23 08:52:06 +00:00
<?php
/**
2015-11-03 13:31:20 +00:00
* Class WC_Helper_Order.
2015-05-23 08:52:06 +00:00
*
2015-11-03 13:31:20 +00:00
* This helper class should ONLY be used for unit tests!.
2015-05-23 08:52:06 +00:00
*/
class WC_Helper_Order {
/**
* Delete a product.
*
* @param int $order_id ID of the order to delete.
*/
public static function delete_order( $order_id ) {
$order = wc_get_order( $order_id );
2015-05-23 08:52:06 +00:00
// Delete all products in the order
foreach ( $order->get_items() as $item ) :
WC_Helper_Product::delete_product( $item['product_id'] );
endforeach;
WC_Helper_Shipping::delete_simple_flat_rate();
// Delete the order post
wp_delete_post( $order_id, true );
}
/**
* Create a order.
*
* @since 2.4
*
* @return WC_Order Order object.
*/
2016-03-15 18:37:42 +00:00
public static function create_order( $customer_id = 1 ) {
2015-05-23 08:52:06 +00:00
// Create product
$product = WC_Helper_Product::create_simple_product();
WC_Helper_Shipping::create_simple_flat_rate();
$order_data = array(
'status' => 'pending',
2016-03-15 18:37:42 +00:00
'customer_id' => $customer_id,
2015-05-23 08:52:06 +00:00
'customer_note' => '',
'total' => '',
2015-05-23 08:52:06 +00:00
);
$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Required, else wc_create_order throws an exception
$order = wc_create_order( $order_data );
// Add order products
2016-08-22 15:48:19 +00:00
$item = new WC_Order_Item_Product( array(
'product' => $product,
'quantity' => 4,
) );
$order->add_item( $item );
2015-05-23 08:52:06 +00:00
// Set billing address
$billing_address = array(
'country' => 'US',
'first_name' => 'Jeroen',
'last_name' => 'Sormani',
'company' => 'WooCompany',
'address_1' => 'WooAddress',
'address_2' => '',
'postcode' => '123456',
'city' => 'WooCity',
'state' => 'NY',
'email' => 'admin@example.org',
'phone' => '555-32123',
2015-05-23 08:52:06 +00:00
);
$order->set_address( $billing_address, 'billing' );
// Add shipping costs
$shipping_taxes = WC_Tax::calc_shipping_tax( '10', WC_Tax::get_shipping_tax_rates() );
2016-08-22 15:48:19 +00:00
$rate = new WC_Shipping_Rate( 'flat_rate_shipping', 'Flat rate shipping', '10', $shipping_taxes, 'flat_rate' );
$item = new WC_Order_Item_Shipping( array(
'method_title' => $rate->label,
'method_id' => $rate->id,
'total' => wc_format_decimal( $rate->cost ),
'taxes' => $rate->taxes,
'meta_data' => $rate->get_meta_data(),
) );
$order->add_item( $item );
2015-05-23 08:52:06 +00:00
// Set payment gateway
$payment_gateways = WC()->payment_gateways->payment_gateways();
$order->set_payment_method( $payment_gateways['bacs'] );
// Set totals
2016-06-22 11:29:23 +00:00
$order->set_shipping_total( 10 );
$order->set_discount_total( 0 );
$order->set_discount_tax( 0 );
$order->set_cart_tax( 0 );
$order->set_shipping_tax( 0 );
$order->set_total( 40 ); // 4 x $10 simple helper product
2016-08-15 10:15:03 +00:00
$order->save();
2016-06-22 11:29:23 +00:00
2016-08-15 10:15:03 +00:00
return $order;
2015-05-23 08:52:06 +00:00
}
}