/** * 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; }
/** * 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; }