/** * 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)); }
/** * 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; }
/** * * * @param unknown $order_id * @param unknown $product * * @return array */ public static function pps_get_due($order_id, $product) { global $woocommerce; $item_shipping_cost = 0; $shipping_costs = array(); $settings = get_option('woocommerce_per_product_settings'); $taxable = $settings['tax_status']; $order = new WC_Order($order_id); $package['destination']['country'] = $order->shipping_country; $package['destination']['state'] = $order->shipping_state; $package['destination']['postcode'] = $order->shipping_postcode; $product_id = !empty($product['variation_id']) ? $product['variation_id'] : $product['product_id']; if (!empty($product['variation_id'])) { $rule = woocommerce_per_product_shipping_get_matching_rule($product['variation_id'], $package); } if (empty($rule)) { $rule = woocommerce_per_product_shipping_get_matching_rule($product['product_id'], $package); } if (!empty($rule)) { $item_shipping_cost += $rule->rule_item_cost * $product['qty']; if (!empty(self::$pps_shipping_costs[$order_id]) && !in_array($rule->rule_id, self::$pps_shipping_costs[$order_id])) { $item_shipping_cost += $rule->rule_cost; } else { if (empty(self::$pps_shipping_costs[$order_id])) { $item_shipping_cost += $rule->rule_cost; } } self::$pps_shipping_costs[$order_id][] = $rule->rule_id; } $shipping_costs['amount'] = $item_shipping_cost; $shipping_costs['tax'] = 'taxable' === $taxable ? WCV_Shipping::calculate_shipping_tax($item_shipping_cost, $order) : 0; // return $item_shipping_cost; return $shipping_costs; }
/** * * * @param unknown $order_id * @param unknown $product * * @return unknown */ public function pps_get_due($order_id, $product) { global $woocommerce; $item_shipping_cost = 0; $order = new WC_Order($order_id); $package['destination']['country'] = $order->shipping_country; $package['destination']['state'] = $order->shipping_state; $package['destination']['postcode'] = $order->shipping_postcode; $product_id = !empty($product['variation_id']) ? $product['variation_id'] : $product['product_id']; if (!empty($product['variation_id'])) { $rule = woocommerce_per_product_shipping_get_matching_rule($product['variation_id'], $package); } if (empty($rule)) { $rule = woocommerce_per_product_shipping_get_matching_rule($product['product_id'], $package); } if (!empty($rule)) { $item_shipping_cost += $rule->rule_item_cost * $product['qty']; if (!empty(self::$pps_shipping_costs[$order_id]) && !in_array($rule->rule_id, self::$pps_shipping_costs[$order_id])) { $item_shipping_cost += $rule->rule_cost; } else { if (empty(self::$pps_shipping_costs[$order_id])) { $item_shipping_cost += $rule->rule_cost; } } self::$pps_shipping_costs[$order_id][] = $rule->rule_id; } return $item_shipping_cost; }