/** * Check if the contents of the current cart are valid for multiple shipping * * To pass, there must be 1 or more items in the cart that passes the @see WC_Cart::needs_shipping() test. * If there is only 1 item in the cart, it must have a quantity of 2 or more. And child items * from Bundles and Composite Products are excluded from the count. * * This method will automatically return false if the only available shipping method is Local Pickup * * @return bool */ public function cart_is_eligible_for_multi_shipping() { global $woocommerce; $sess_item_address = wcms_session_get('cart_item_addresses'); $has_item_address = !wcms_session_isset('cart_item_addresses') || empty($sess_item_address) ? false : true; $item_allowed = false; $contents = wcms_get_real_cart_items(); if (count($contents) > 1) { $item_allowed = true; } else { $content = current($contents); if ($content && $content['quantity'] > 1) { $item_allowed = true; } } // do not allow to set multiple addresses if only local pickup is available if (function_exists('wc_add_notice')) { $available_methods = $this->get_available_shipping_methods(); } else { $available_methods = $woocommerce->shipping->get_available_shipping_methods(); } if (count($available_methods) == 1 && (isset($available_methods['local_pickup']) || isset($available_methods['local_pickup_plus']))) { $item_allowed = false; } elseif (isset($_POST['shipping_method']) && ($_POST['shipping_method'] == 'local_pickup' || $_POST['shipping_method'] == 'local_pickup_plus')) { $item_allowed = false; } // do not allow if any of the cart items is in the excludes list $settings = get_option('woocommerce_multiple_shipping_settings', array()); $excl_products = isset($settings['excluded_products']) ? $settings['excluded_products'] : array(); $excl_categories = isset($settings['excluded_categories']) ? $settings['excluded_categories'] : array(); if ($excl_products || $excl_categories) { foreach ($contents as $cart_item) { if (in_array($cart_item['product_id'], $excl_products)) { $item_allowed = false; break; } // item categories $cat_ids = wp_get_object_terms($cart_item['product_id'], 'product_cat', array('fields' => 'ids')); foreach ($cat_ids as $cat_id) { if (in_array($cat_id, $excl_categories)) { $item_allowed = false; break 2; } } } } return apply_filters('wc_ms_cart_is_eligible', $item_allowed); }
function calculate_totals($cart) { global $woocommerce; if (isset($_POST['action']) && $_POST['action'] == 'woocommerce_update_shipping_method') { return $cart; } $shipping_total = 0; $shipping_taxes = array(); $shipping_tax_total = 0; $_tax = new WC_Tax(); if (!wcms_session_isset('wcms_packages')) { return $cart; } if (!wcms_session_isset('shipping_methods')) { return $cart; } $packages = wcms_session_get('wcms_packages'); $chosen = wcms_session_get('shipping_methods'); $rates = array(); if (!$packages) { $packages = $woocommerce->cart->get_shipping_packages(); } $woocommerce->shipping->calculate_shipping($packages); foreach ($packages as $x => $package) { if (isset($chosen[$x])) { $woocommerce->customer->calculated_shipping(true); $woocommerce->customer->set_shipping_location($package['destination']['country'], $package['destination']['state'], $package['destination']['postcode']); $ship = $chosen[$x]['id']; $package = $woocommerce->shipping->calculate_shipping_for_package($package); if (isset($package['rates']) && isset($package['rates'][$ship])) { $rate = $package['rates'][$ship]; $rates[$x] = $package['rates']; $shipping_total += $rate->cost; // calculate tax foreach (array_keys($shipping_taxes + $rate->taxes) as $key) { $shipping_taxes[$key] = (isset($rate->taxes[$key]) ? $rate->taxes[$key] : 0) + (isset($shipping_taxes[$key]) ? $shipping_taxes[$key] : 0); } } } $packages[$x] = $package; } $cart->shipping_taxes = $shipping_taxes; $cart->shipping_total = $shipping_total; $cart->shipping_tax_total = is_array($shipping_taxes) ? array_sum($shipping_taxes) : 0; // store the shipping rates wcms_session_set('wcms_package_rates', $rates); $this->calculate_taxes($cart, $packages); }
<?php global $woocommerce; $addresses = wcms_session_isset('cart_item_addresses') ? wcms_session_get('cart_item_addresses') : array(); $relations = wcms_session_isset('address_relationships') ? wcms_session_get('address_relationships') : array(); $placed = array(); if ($user->ID != 0) { $addresses = get_user_meta($user->ID, 'wc_other_addresses', true); if ($addresses) { foreach ($addresses as $x => $addr) { foreach ($contents as $key => $value) { if (isset($relations[$x]) && !empty($relations[$x])) { $qty = array_count_values($relations[$x]); if (in_array($key, $relations[$x])) { if (isset($placed[$key])) { $placed[$key] += $qty[$key]; } else { $placed[$key] = $qty[$key]; } } } } } } } else { $sigs = array(); if (isset($addresses) && !empty($addresses)) { $sigs = wcms_session_get('cart_address_sigs'); } foreach ($sigs as $sig => $addr_id) { if (isset($relations[$addr_id]) && !empty($relations[$addr_id])) {
function calculate_totals($cart) { global $woocommerce; $shipping_total = 0; $shipping_taxes = array(); $shipping_tax_total = 0; if (!wcms_session_isset('shipping_methods')) { return; } $packages = $woocommerce->cart->get_shipping_packages(); $available = $woocommerce->shipping->get_available_shipping_methods(); $chosen = wcms_session_get('shipping_methods'); foreach ($packages as $x => $package) { if (isset($chosen[$x])) { $ship = $chosen[$x]['id']; $package = $woocommerce->shipping->calculate_shipping_for_package($package); if (isset($package['rates'][$ship])) { $shipping_total += $package['rates'][$ship]->cost; } if (!empty($package['rates'][$ship]->taxes)) { foreach ($package['rates'][$ship]->taxes as $key => $value) { if (isset($shipping_taxes[$key])) { $shipping_taxes[$key] += $value; $shipping_tax_total += $value; } else { $shipping_taxes[$key] = $value; $shipping_tax_total += $value; } } } } } $cart->shipping_taxes = $shipping_taxes; $cart->shipping_total = $shipping_total; $cart->shipping_tax_total = $shipping_tax_total; }