/**
  * Create a order.
  *
  * @since 2.4
  *
  * @return WC_Order Order object.
  */
 public static function create_order($customer_id = 1)
 {
     // Create product
     $product = WC_Helper_Product::create_simple_product();
     WC_Helper_Shipping::create_simple_flat_rate();
     $order_data = array('status' => 'pending', 'customer_id' => $customer_id, 'customer_note' => '', 'total' => '');
     $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
     // Required, else wc_create_order throws an exception
     $order = wc_create_order($order_data);
     // Add order products
     $order->add_product($product, 4);
     // Set billing address
     $order->set_billing_first_name('Jeroen');
     $order->set_billing_last_name('Sormani');
     $order->set_billing_company('WooCompany');
     $order->set_billing_address_1('WooAddress');
     $order->set_billing_address_2('');
     $order->set_billing_city('WooCity');
     $order->set_billing_state('NY');
     $order->set_billing_postcode('123456');
     $order->set_billing_country('US');
     $order->set_billing_email('*****@*****.**');
     $order->set_billing_phone('555-32123');
     // Add shipping costs
     $shipping_taxes = WC_Tax::calc_shipping_tax('10', WC_Tax::get_shipping_tax_rates());
     $rate = new WC_Shipping_Rate('flat_rate_shipping', 'Flat rate shipping', '10', $shipping_taxes, 'flat_rate');
     $item = new WC_Order_Item_Shipping();
     $item->set_props(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);
     // Set payment gateway
     $payment_gateways = WC()->payment_gateways->payment_gateways();
     $order->set_payment_method($payment_gateways['bacs']);
     // Set totals
     $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
     $order->save();
     return $order;
 }
 /**
  * Add a shipping rate. If taxes are not set they will be calculated based on cost.
  * @param array $args (default: array())
  */
 public function add_rate($args = array())
 {
     $args = wp_parse_args($args, array('id' => $this->get_rate_id(), 'label' => '', 'cost' => '0', 'taxes' => '', 'calc_tax' => 'per_order', 'meta_data' => array(), 'package' => false));
     // ID and label are required
     if (!$args['id'] || !$args['label']) {
         return;
     }
     // Total up the cost
     $total_cost = is_array($args['cost']) ? array_sum($args['cost']) : $args['cost'];
     $taxes = $args['taxes'];
     // Taxes - if not an array and not set to false, calc tax based on cost and passed calc_tax variable. This saves shipping methods having to do complex tax calculations.
     if (!is_array($taxes) && $taxes !== false && $total_cost > 0 && $this->is_taxable()) {
         $taxes = 'per_item' === $args['calc_tax'] ? $this->get_taxes_per_item($args['cost']) : WC_Tax::calc_shipping_tax($total_cost, WC_Tax::get_shipping_tax_rates());
     }
     // Round the total cost after taxes have been calculated.
     $total_cost = wc_format_decimal($total_cost, wc_get_price_decimals());
     // Create rate object
     $rate = new WC_Shipping_Rate($args['id'], $args['label'], $total_cost, $taxes, $this->id);
     if (!empty($args['meta_data'])) {
         foreach ($args['meta_data'] as $key => $value) {
             $rate->add_meta_data($key, $value);
         }
     }
     // Store package data
     if ($args['package']) {
         $items_in_package = array();
         foreach ($args['package']['contents'] as $item) {
             $product = $item['data'];
             $items_in_package[] = $product->get_title() . ' × ' . $item['quantity'];
         }
         $rate->add_meta_data(__('Items', 'woocommerce'), implode(', ', $items_in_package));
     }
     $this->rates[$args['id']] = $rate;
 }
/**
 * Get a shipping methods full label including price
 * @param  WC_Shipping_Rate $method
 * @return string
 */
function wc_cart_totals_shipping_method_label($method)
{
    $label = $method->get_label();
    if ($method->cost > 0) {
        if (WC()->cart->tax_display_cart == 'excl') {
            $label .= ': ' . wc_price($method->cost);
            if ($method->get_shipping_tax() > 0 && WC()->cart->prices_include_tax) {
                $label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
            }
        } else {
            $label .= ': ' . wc_price($method->cost + $method->get_shipping_tax());
            if ($method->get_shipping_tax() > 0 && !WC()->cart->prices_include_tax) {
                $label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
            }
        }
    } elseif ($method->id !== 'free_shipping') {
        $label .= ' (' . __('Free', 'woocommerce') . ')';
    }
    return apply_filters('woocommerce_cart_shipping_method_full_label', $label, $method);
}
 /**
  * Add a shipping rate. If taxes are not set they will be calculated based on cost.
  * @param array $args (default: array())
  */
 public function add_rate($args = array())
 {
     $args = wp_parse_args($args, array('id' => '', 'label' => '', 'cost' => '0', 'taxes' => '', 'calc_tax' => 'per_order', 'meta_data' => array()));
     // ID and label are required
     if (!$args['id'] || !$args['label']) {
         return;
     }
     // Total up the cost
     $total_cost = is_array($args['cost']) ? array_sum($args['cost']) : $args['cost'];
     $taxes = $args['taxes'];
     // Taxes - if not an array and not set to false, calc tax based on cost and passed calc_tax variable. This saves shipping methods having to do complex tax calculations.
     if (!is_array($taxes) && $taxes !== false && $total_cost > 0 && $this->is_taxable()) {
         $taxes = 'per_item' === $args['calc_tax'] ? $this->get_taxes_per_item($args['cost']) : WC_Tax::calc_shipping_tax($total_cost, WC_Tax::get_shipping_tax_rates());
     }
     // Round the total cost after taxes have been calculated.
     $total_cost = wc_format_decimal($total_cost, wc_get_price_decimals());
     // Create rate object
     $rate = new WC_Shipping_Rate($args['id'], $args['label'], $total_cost, $taxes, $this->id);
     if (!empty($args['meta_data'])) {
         foreach ($args['meta_data'] as $key => $value) {
             $rate->add_meta_data($key, $value);
         }
     }
     $this->rates[$args['id']] = $rate;
 }
Esempio n. 5
0
 /**
  * Test: has_shipping_method
  */
 function test_has_shipping_method()
 {
     $object = new WC_Order();
     $object->save();
     $this->assertFalse($object->has_shipping_method('flat_rate_shipping'));
     $rate = new WC_Shipping_Rate('flat_rate_shipping:1', 'Flat rate shipping', '10', array(), 'flat_rate');
     $item = new WC_Order_Item_Shipping();
     $item->set_props(array('method_title' => $rate->label, 'method_id' => $rate->id, 'total' => wc_format_decimal($rate->cost), 'taxes' => $rate->taxes, 'meta_data' => $rate->get_meta_data()));
     $object->add_item($item);
     $object->save();
     $this->assertTrue($object->has_shipping_method('flat_rate_shipping'));
 }