/** * Create a order. * * @since 2.4 * * @return WC_Order Order object. */ public static function create_order() { // Create product $product = WC_Helper_Product::create_simple_product(); WC_Helper_Shipping::create_simple_flat_rate(); $order_data = array('status' => 'pending', 'customer_id' => 1, '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 $item_id = $order->add_product($product, 4); // 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' => '*****@*****.**', 'phone' => '555-32123'); $order->set_address($billing_address, 'billing'); // Add shipping costs $shipping_taxes = WC_Tax::calc_shipping_tax('10', WC_Tax::get_shipping_tax_rates()); $order->add_shipping(new WC_Shipping_Rate('flat_rate_shipping', 'Flat rate shipping', '10', $shipping_taxes, 'flat_rate')); // Set payment gateway $payment_gateways = WC()->payment_gateways->payment_gateways(); $order->set_payment_method($payment_gateways['bacs']); // Set totals $order->set_total(10, 'shipping'); $order->set_total(0, 'cart_discount'); $order->set_total(0, 'cart_discount_tax'); $order->set_total(0, 'tax'); $order->set_total(0, 'shipping_tax'); $order->set_total(40, 'total'); // 4 x $10 simple helper product return wc_get_order($order->id); }
/** * 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 rate * * A a shipping rate. If taxes are not set they will be calculated based on cost. */ function add_rate($args = array()) { global $woocommerce; $defaults = array('id' => '', 'label' => '', 'cost' => '0', 'taxes' => '', 'calc_tax' => 'per_order'); $args = wp_parse_args($args, $defaults); extract($args); // Id and label are required if (!$id || !$label) { return; } // Handle cost $total_cost = is_array($cost) ? array_sum($cost) : $cost; // 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 compelex tax calculations if (!is_array($taxes) && $taxes !== false && $total_cost > 0 && get_option('woocommerce_calc_taxes') == 'yes' && $this->tax_status == 'taxable') { $_tax = new WC_Tax(); $taxes = array(); switch ($calc_tax) { case "per_item": // If we have an array of costs we can look up each items tax class and add tax accordingly if (is_array($cost)) { $cart = $woocommerce->cart->get_cart(); foreach ($cost as $cost_key => $amount) { if (!isset($cart[$cost_key])) { continue; } $_product = $cart[$cost_key]['data']; $rates = $_tax->get_shipping_tax_rates($_product->get_tax_class()); $item_taxes = $_tax->calc_shipping_tax($amount, $rates); // Sum the item taxes foreach (array_keys($taxes + $item_taxes) as $key) { $taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0); } } // Add any cost for the order - order costs are in the key 'order' if (isset($cost['order'])) { $rates = $_tax->get_shipping_tax_rates(); $item_taxes = $_tax->calc_shipping_tax($cost['order'], $rates); // Sum the item taxes foreach (array_keys($taxes + $item_taxes) as $key) { $taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0); } } } break; default: $rates = $_tax->get_shipping_tax_rates(); $taxes = $_tax->calc_shipping_tax($total_cost, $rates); break; } } $this->rates[] = new WC_Shipping_Rate($id, $label, $total_cost, $taxes); }
/** * Calculate shipping when this method is used standalone. */ public function calculate_shipping($package) { $_tax = new WC_Tax(); $taxes = array(); $shipping_cost = 0; // This shipping method loops through products, adding up the cost if (sizeof($package['contents']) > 0) { foreach ($package['contents'] as $item_id => $values) { if ($values['quantity'] > 0) { if ($values['data']->needs_shipping()) { $rule = false; $item_shipping_cost = 0; if ($values['variation_id']) { $rule = woocommerce_per_product_shipping_get_matching_rule($values['variation_id'], $package); } if ($rule === false) { $rule = woocommerce_per_product_shipping_get_matching_rule($values['product_id'], $package); } if ($rule) { $item_shipping_cost += $rule->rule_item_cost * $values['quantity']; $item_shipping_cost += $rule->rule_cost; } elseif ($this->cost === '0' || $this->cost > 0) { // Use default $item_shipping_cost += $this->cost * $values['quantity']; } else { // NO default and nothing found - abort return; } // Fee $item_shipping_cost += $this->get_fee($this->fee, $item_shipping_cost) * $values['quantity']; $shipping_cost += $item_shipping_cost; if (get_option('woocommerce_calc_taxes') == 'yes' && $this->tax_status == 'taxable') { $rates = $_tax->get_shipping_tax_rates($values['data']->get_tax_class()); $item_taxes = $_tax->calc_shipping_tax($item_shipping_cost, $rates); // Sum the item taxes foreach (array_keys($taxes + $item_taxes) as $key) { $taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0); } } } } } } // Add order shipping cost + tax if ($this->order_fee) { $order_fee = $this->get_fee($this->order_fee, $shipping_cost); $shipping_cost += $order_fee; if (get_option('woocommerce_calc_taxes') == 'yes' && $this->tax_status == 'taxable') { $rates = $_tax->get_shipping_tax_rates(); $item_taxes = $_tax->calc_shipping_tax($order_fee, $rates); // Sum the item taxes foreach (array_keys($taxes + $item_taxes) as $key) { $taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0); } } } // Add rate $this->add_rate(array('id' => $this->id, 'label' => $this->title, 'cost' => $shipping_cost, 'taxes' => $taxes)); }
/** * Get rates. */ public function test_get_shipping_tax_rates() { global $wpdb; $wpdb->query("DELETE FROM {$wpdb->prefix}woocommerce_tax_rates"); $wpdb->query("DELETE FROM {$wpdb->prefix}woocommerce_tax_rate_locations"); $customer_location = WC_Tax::get_tax_location(); $tax_rate = array('tax_rate_country' => $customer_location[0], 'tax_rate_state' => '', 'tax_rate' => '20.0000', 'tax_rate_name' => 'VAT', 'tax_rate_priority' => '1', 'tax_rate_compound' => '0', 'tax_rate_shipping' => '1', 'tax_rate_order' => '1', 'tax_rate_class' => ''); $tax_rate_id = WC_Tax::_insert_tax_rate($tax_rate); $tax_rates = WC_Tax::get_shipping_tax_rates(); $this->assertEquals($tax_rates, array($tax_rate_id => array('rate' => '20.0000', 'label' => 'VAT', 'shipping' => 'yes', 'compound' => 'no'))); WC_Tax::_delete_tax_rate($tax_rate_id); }
/** * calculate_shipping function. * * @access public * * @param mixed $package * * @return void */ public function calculate_shipping($package) { $products = $package['contents']; $destination_country = isset($package['destination']['country']) ? $package['destination']['country'] : ''; $destination_state = isset($package['destination']['state']) ? $package['destination']['state'] : ''; $amount = 0.0; if ($products) { $amount = $this->calculate_per_seller($products, $destination_country, $destination_state); } $tax_rate = $this->tax_status == 'none' ? false : ''; if ($tax_rate === '') { $rates = WC_Tax::get_shipping_tax_rates(); $taxRate = reset($rates); $taxkey = key($rates); $tax_rates = array(); $shippingTaxes = 0; $price = $this->calculate_per_seller_prices($products, $destination_country, $destination_state); //var_dump($price); // var_dump($rates); if (!empty($price)) { foreach ($price as $seller_id => $value) { //TODO: Check if taxxing from same state; //var_dump($_product->post->post_author); $country_obj = new WC_Countries(); $countries = $country_obj->countries; $states = $country_obj->states; $selleraddress = dokan_get_seller_address($seller_id, true); $sellerState = $selleraddress['state']; $stateCode = array_search(ucfirst(strtolower($sellerState)), $states['US']); if ($stateCode !== false) { $sellerState = $stateCode; } $location = WC()->customer->get_taxable_address(); $buyerState = $location[1]; if ($buyerState == $sellerState) { $shippingCost = array_sum($value['addition_price']) + $value['default'] + array_sum($value['qty']) + $value['add_product'] + (isset($value['state_rates']) ? $value['state_rates'] : 0); $shippingTaxes += $shippingCost * $taxRate['rate'] / 100; //var_dump($shippingCost, $taxRate); } } } $tax_rate[$taxkey] = $shippingTaxes; //var_dump($tax_rate); } $rate = array('id' => $this->id, 'label' => $this->title, 'cost' => $amount, 'taxes' => $tax_rate); // Register the rate $this->add_rate($rate); }
/** * Calc taxes per item being shipping in costs array. * @since 2.6.0 * @access protected * @param array $costs * @return array of taxes */ protected function get_taxes_per_item($costs) { $taxes = array(); // If we have an array of costs we can look up each items tax class and add tax accordingly if (is_array($costs)) { $cart = WC()->cart->get_cart(); foreach ($costs as $cost_key => $amount) { if (!isset($cart[$cost_key])) { continue; } $item_taxes = WC_Tax::calc_shipping_tax($amount, WC_Tax::get_shipping_tax_rates($cart[$cost_key]['data']->get_tax_class())); // Sum the item taxes foreach (array_keys($taxes + $item_taxes) as $key) { $taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0); } } // Add any cost for the order - order costs are in the key 'order' if (isset($costs['order'])) { $item_taxes = WC_Tax::calc_shipping_tax($costs['order'], WC_Tax::get_shipping_tax_rates()); // Sum the item taxes foreach (array_keys($taxes + $item_taxes) as $key) { $taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0); } } } return $taxes; }
/** * Add a rate * * 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')); // Id and label are required if (!$args['id'] || !$args['label']) { return; } // Handle 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 = array(); switch ($args['calc_tax']) { case "per_item": // If we have an array of costs we can look up each items tax class and add tax accordingly if (is_array($args['cost'])) { $cart = WC()->cart->get_cart(); foreach ($args['cost'] as $cost_key => $amount) { if (!isset($cart[$cost_key])) { continue; } $item_taxes = WC_Tax::calc_shipping_tax($amount, WC_Tax::get_shipping_tax_rates($cart[$cost_key]['data']->get_tax_class())); // Sum the item taxes foreach (array_keys($taxes + $item_taxes) as $key) { $taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0); } } // Add any cost for the order - order costs are in the key 'order' if (isset($args['cost']['order'])) { $item_taxes = WC_Tax::calc_shipping_tax($args['cost']['order'], WC_Tax::get_shipping_tax_rates()); // Sum the item taxes foreach (array_keys($taxes + $item_taxes) as $key) { $taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0); } } } break; default: $taxes = WC_Tax::calc_shipping_tax($total_cost, WC_Tax::get_shipping_tax_rates()); break; } } $this->rates[] = new WC_Shipping_Rate($args['id'], $args['label'], $total_cost, $taxes, $this->id); }
/** * woocommerce_per_product_shipping_addition function. * * @access public * @return void */ function woocommerce_per_product_shipping_addition($rates, $package) { global $woocommerce; $_tax = new WC_Tax(); if ($rates) { foreach ($rates as $rate_id => $rate) { // Skip free shipping if ($rate->cost == 0 && apply_filters('woocommerce_per_product_shipping_skip_free_method_' . $rate->method_id, true)) { continue; } // Skip self if ($rate->method_id == 'per_product') { continue; } if (sizeof($package['contents']) > 0) { foreach ($package['contents'] as $item_id => $values) { if ($values['quantity'] > 0) { if ($values['data']->needs_shipping()) { $item_shipping_cost = 0; $rule = false; if ($values['variation_id']) { $rule = woocommerce_per_product_shipping_get_matching_rule($values['variation_id'], $package, false); } if ($rule === false) { $rule = woocommerce_per_product_shipping_get_matching_rule($values['product_id'], $package, false); } if (empty($rule)) { continue; } $item_shipping_cost += $rule->rule_item_cost * $values['quantity']; $item_shipping_cost += $rule->rule_cost; $rate->cost += $item_shipping_cost; if ($woocommerce->shipping->shipping_methods[$rate->method_id]->tax_status == 'taxable') { $tax_rates = $_tax->get_shipping_tax_rates($values['data']->get_tax_class()); $item_taxes = $_tax->calc_shipping_tax($item_shipping_cost, $tax_rates); // Sum the item taxes foreach (array_keys($rate->taxes + $item_taxes) as $key) { $rate->taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($rate->taxes[$key]) ? $rate->taxes[$key] : 0); } } } } } } } } return $rates; }
function calculate_shipping($paquete = array()) { //Peso total del pedido $peso_total = WC()->cart->cart_contents_weight; //Variables $volumen = $largo = $ancho = $alto = 0; $clases = $medidas = array(); //Toma distintos datos de los productos foreach (WC()->cart->get_cart() as $identificador => $valores) { $producto = $valores['data']; $peso = $producto->get_weight() * $valores['quantity']; //Arregla un problema con los pesos en variaciones virtuales if ($producto->is_virtual()) { $peso_total -= $peso; } //Medidas y volúmenes if ($producto->length && $producto->width && $producto->height) { $volumen += $producto->length * $producto->width * $producto->height * $valores['quantity']; } $medidas[] = array('largo' => $producto->length, 'ancho' => $producto->width, 'alto' => $producto->height, 'cantidad' => $valores['quantity']); if ($producto->length > $largo) { $largo = $producto->length; } if ($producto->width > $ancho) { $ancho = $producto->width; } if ($producto->height > $alto) { $alto = $producto->height; } //Clase de producto if ($producto->needs_shipping()) { $clase = $producto->get_shipping_class() ? $producto->get_shipping_class() : 'todas'; if (!isset($clases['todas'])) { $clases['todas'] = 0; } $clases['todas'] += $peso; if (!isset($clases[$clase])) { $clases[$clase] = $peso; } else { if ($clase != 'todas') { $clases[$clase] += $peso; } } } } if ($this->clases_excluidas) { //Toma distintos datos de los productos foreach (WC()->cart->get_cart() as $identificador => $valores) { $producto = $valores['data']; //Clase de producto if (in_array($producto->get_shipping_class(), $this->clases_excluidas)) { return false; //No atiende a las clases de envío excluidas } } } $grupos = $this->dame_grupos($paquete, $clases); if (empty($grupos)) { return false; //No hay resultados } $grupos_excluidos = explode(',', preg_replace('/\\s+/', '', $this->grupos_excluidos)); foreach ($grupos_excluidos as $grupo_excluido) { if (in_array($grupo_excluido, $grupos)) { return false; //No atiende a los grupos excluidos } } if ($this->apg_free_shipping && $this->muestra == 'yes') { return false; //Sólo muestra el envío gratuito } $tarifas = $this->dame_tarifas($grupos); //Recoge las tarifas programadas $precios = $this->dame_tarifa_mas_barata($tarifas, $peso_total, $volumen, $largo, $ancho, $alto, $grupos, $medidas, $clases); //Filtra las tarifas if (empty($precios)) { return false; //No hay tarifa } //Calculamos el precio $precio_total = $suma_impuestos = $suma_cargos = 0; $impuestos_parciales = $impuestos_totales = array(); if ($this->tax_status != 'none') { $impuestos = new WC_Tax(); } //Cargos adicionales if ($this->fee > 0) { $suma_cargos += $this->fee; } if ($this->cargo > 0 && !strpos($this->cargo, '%')) { $suma_cargos += $this->cargo; } foreach ($precios as $grupo => $precio) { if ($this->cargo > 0 && strpos($this->cargo, '%')) { $suma_cargos += $precio * (str_replace('%', '', $this->cargo) / 100); //Cargos adicionales } $precio += $suma_cargos; $precio_total += $precio; if ($this->tax_status != 'none') { $impuestos_parciales[$grupo] = $impuestos->calc_shipping_tax($precio, $impuestos->get_shipping_tax_rates($this->settings['Tax_' . $grupo])); } } if ($this->tax_status != 'none') { foreach ($impuestos_parciales as $impuesto_parcial) { foreach ($impuesto_parcial as $clave => $impuesto) { $suma_impuestos += $impuesto; $impuestos_totales[$clave] = $suma_impuestos; } } } $tarifa = array('id' => $this->id, 'label' => $this->title, 'cost' => $precio_total, 'taxes' => $impuestos_totales, 'calc_tax' => 'per_order'); $this->add_rate($tarifa); }
public static function createSimpleOrder($total = 40) { $product = self::createSimpleProduct(); self::createSimpleShippingFlatRate(); $order_data = array('status' => 'pending', 'customer_id' => 1, 'customer_note' => '', 'total' => ''); // Required, else wc_create_order throws an exception $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; $order = wc_create_order($order_data); // Add order products $order->add_product($product, 4); // Set billing address $shipping_address = array('country' => 'US', 'first_name' => 'Jeroen', 'last_name' => 'Sormani', 'company' => 'WooCompany', 'address_1' => 'WooAddress', 'address_2' => '', 'postcode' => '123456', 'city' => 'WooCity', 'state' => 'NY', 'email' => '*****@*****.**', 'phone' => '555-32123'); $order->set_address($shipping_address, 'shipping'); // Add shipping costs $shipping_taxes = \WC_Tax::calc_shipping_tax('10', \WC_Tax::get_shipping_tax_rates()); $order->add_shipping(new \WC_Shipping_Rate('flagship_shipping_method|Purolator|PurolatorExpress|Purolator Express|1473811200', 'Purolator - Purolator Express', '10', $shipping_taxes, 'flagship_shipping_method')); // Set totals $order->set_total(10, 'shipping'); $order->set_total(0, 'cart_discount'); $order->set_total(0, 'cart_discount_tax'); $order->set_total(0, 'tax'); $order->set_total(0, 'shipping_tax'); $order->set_total($total, 'total'); // 4 x $10 simple helper product return wc_get_order($order->id); }