function shipping_packages($packages) { global $woocommerce; if (defined('SHIPPING_PACKAGES_SET')) { return $packages; } $myPackages = array(); $settings = $this->settings; $methods = wcms_session_isset('shipping_methods') ? wcms_session_get('shipping_methods') : array(); $sess_cart_addresses = wcms_session_get('cart_item_addresses'); if (is_null($sess_cart_addresses) || empty($sess_cart_addresses)) { // multiple shipping is not set up // check if items have different origins if (wcms_count_real_cart_items() > 0) { foreach (wcms_get_real_cart_items() as $cart_item_key => $values) { $product_id = $values['product_id']; $product_cats = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids')); // look for direct product matches $matched = false; foreach ($settings as $idx => $setting) { if (in_array($product_id, $setting['products'])) { $matched = $setting; break; } } if (!$matched) { // look for category matches foreach ($settings as $idx => $setting) { foreach ($product_cats as $product_cat_id) { if (in_array($product_cat_id, $setting['categories'])) { $matched = $setting; break; } } } } if ($matched !== false) { // create or update package $existing = false; if (!empty($myPackages)) { foreach ($myPackages as $idx => $my_pkg) { if (isset($my_pkg['origin']) && $my_pkg['origin'] == $matched['zip'] && (isset($my_pkg['method']) && $my_pkg['method'] == $matched['method'])) { $existing = true; $values['package_idx'] = $idx; $myPackages[$idx]['contents'][$cart_item_key] = $values; $myPackages[$idx]['contents_cost'] += $values['line_total']; if (isset($methods[$idx])) { $myPackages[$idx]['selected_method'] = $methods[$idx]; } // modify the cart entry $woocommerce->cart->cart_contents[$cart_item_key] = $values; break; } } } if (!$existing) { $values['package_idx'] = count($myPackages); $pkg = array('contents' => array($cart_item_key => $values), 'contents_cost' => $values['line_total'], 'method' => $matched['method'], 'destination' => $packages[0]['destination']); if (isset($methods[$idx])) { $pkg['selected_method'] = $methods[$idx]; } $myPackages[] = $pkg; // modify the cart entry $woocommerce->cart->cart_contents[$cart_item_key] = $values; } } } } if (!empty($myPackages) && count($myPackages) > 1) { if (function_exists('wc_enqueue_js')) { wc_enqueue_js('_multi_shipping = true;'); } else { $woocommerce->add_inline_js('_multi_shipping = true;'); } $packages = $myPackages; } } else { // group items into ship-to addresses $addresses = wcms_session_get('cart_item_addresses'); $productsArray = array(); $address_fields = $woocommerce->countries->get_address_fields($woocommerce->countries->get_base_country(), 'shipping_'); if (wcms_count_real_cart_items() > 0) { foreach (wcms_get_real_cart_items() as $cart_item_key => $values) { $qty = $values['quantity']; for ($i = 1; $i <= $qty; $i++) { if (isset($addresses['shipping_first_name_' . $cart_item_key . '_' . $values['product_id'] . '_' . $i])) { $address = array(); foreach ($address_fields as $field_name => $field) { $addr_key = str_replace('shipping_', '', $field_name); $address[$addr_key] = isset($addresses[$field_name . '_' . $cart_item_key . '_' . $values['product_id'] . '_' . $i]) ? $addresses[$field_name . '_' . $cart_item_key . '_' . $values['product_id'] . '_' . $i] : ''; } } else { $address = array(); foreach ($address_fields as $field_name => $field) { $addr_key = str_replace('shipping_', '', $field_name); $address[$addr_key] = ''; } } $currentAddress = wcms_get_formatted_address($address); $key = md5($currentAddress); $_value = $values; $price = $_value['line_total'] / $qty; $tax = $_value['line_tax'] / $qty; $sub = $_value['line_subtotal'] / $qty; $subTax = $_value['line_subtotal_tax'] / $qty; $_value['quantity'] = 1; $_value['line_total'] = $price; $_value['line_tax'] = $tax; $_value['line_subtotal'] = $sub; $_value['line_subtotal_tax'] = $subTax; $meta = md5($woocommerce->cart->get_item_data($_value)); //$origin = $this->get_product_origin( $values['product_id'] ); $origin = false; $method = $this->get_product_shipping_method($values['product_id']); // if origins and/or shipping method are set, group using origins and shipping methods // if origins and/or shipping method are set, group using origins and shipping methods if (!$origin) { $origin = ''; } if (!$method) { $method = ''; } if (!empty($origin) || !empty($method)) { $key .= $origin . $method; } // no origin and method selected if (isset($productsArray[$key])) { // if the same product exists, add to the qty and cost $found = false; foreach ($productsArray[$key]['products'] as $idx => $prod) { if ($prod['id'] == $_value['product_id']) { if ($meta == $prod['meta']) { $found = true; $productsArray[$key]['products'][$idx]['value']['quantity'] += 1; $productsArray[$key]['products'][$idx]['value']['line_total'] += $_value['line_total']; $productsArray[$key]['products'][$idx]['value']['line_tax'] += $_value['line_tax']; $productsArray[$key]['products'][$idx]['value']['line_subtotal'] += $_value['line_subtotal']; $productsArray[$key]['products'][$idx]['value']['line_subtotal_tax'] += $_value['line_subtotal_tax']; break; } } } if (!$found) { // new product $productsArray[$key]['products'][] = array('id' => $_value['product_id'], 'meta' => $meta, 'value' => $_value); } } else { $productsArray[$key] = array('products' => array(array('id' => $_value['product_id'], 'meta' => $meta, 'value' => $_value)), 'country' => $address['country'], 'state' => $address['state'], 'postcode' => $address['postcode'], 'address' => $address); } if (!empty($origin)) { $productsArray[$key]['origin'] = $origin; } if (!empty($method)) { $productsArray[$key]['method'] = $method; } } } } if (!empty($productsArray)) { $myPackages = array(); foreach ($productsArray as $idx => $group) { $pkg = array('contents' => array(), 'contents_cost' => 0, 'destination' => $group['address'], 'full_address' => $group['address']); if (isset($group['origin'])) { $pkg['origin'] = $group['origin']; } if (isset($group['method'])) { $pkg['method'] = $group['method']; } if (isset($methods[$idx])) { $pkg['selected_method'] = $methods[$idx]; } foreach ($group['products'] as $item) { $data = (array) apply_filters('woocommerce_add_cart_item_data', array(), $item['value']['product_id'], $item['value']['variation_id']); // Composite Products support. Manually add the composite data in the cart_item_data array to match the existing cart_item_key if (isset($item['value']['composite_data'])) { $data['composite_data'] = $item['value']['composite_data']; } if (isset($item['value']['composite_children'])) { $data['composite_children'] = array(); } // gravity forms support if (isset($item['value']['_gravity_form_data'])) { $data['_gravity_form_data'] = $item['value']['_gravity_form_data']; } if (isset($item['value']['_gravity_form_lead'])) { $data['_gravity_form_lead'] = $item['value']['_gravity_form_lead']; } $cart_item_id = $woocommerce->cart->generate_cart_id($item['value']['product_id'], $item['value']['variation_id'], $item['value']['variation'], $data); $item['value']['package_idx'] = $idx; $pkg['contents'][$cart_item_id] = $item['value']; if ($item['value']['data']->needs_shipping()) { $pkg['contents_cost'] += $item['value']['line_total']; } } $myPackages[] = $pkg; } if (count($myPackages) > 1) { if (function_exists('wc_enqueue_js')) { wc_enqueue_js('_multi_shipping = true;'); } else { $woocommerce->add_inline_js('_multi_shipping = true;'); } } $packages = $myPackages; } } $packages = $this->normalize_packages_address($packages); wcms_session_set('wcms_packages', $packages); return $packages; }
<?php // set the address fields foreach ($addresses as $x => $addr) { if (empty($addr)) { continue; } $address_fields = $woocommerce->countries->get_address_fields($addr['shipping_country'], 'shipping_'); $address = array(); $formatted_address = false; foreach ($address_fields as $field_name => $field) { $addr_key = str_replace('shipping_', '', $field_name); $address[$addr_key] = isset($addr[$field_name]) ? $addr[$field_name] : ''; } if (!empty($address)) { $formatted_address = wcms_get_formatted_address($address); $json_address = json_encode($address); } if (!$formatted_address) { continue; } ?> <div style="display: none;"> <?php foreach ($shipFields as $key => $field) { $val = isset($addr[$key]) ? $addr[$key] : ''; $key .= '_' . $x; echo '<input type="hidden" name="' . $key . '" value="' . esc_attr($val) . '"/>'; //woocommerce_form_field( $key, $field, $val ); } do_action('woocommerce_after_checkout_shipping_form', $checkout);
</p> <?php } ?> </div> <div style="float:right; width: 49%;"> <h3><?php _e('Shipping address', 'woocommerce-pip'); ?> </h3> <p><?php echo wcms_get_formatted_address($package['full_address']); ?> </p> <?php if (get_post_meta($order_id, '_tracking_provider', true)) { ?> <p><strong><?php _e('Tracking provider:', 'woocommerce-pip'); ?> </strong> <?php echo get_post_meta($order_id, '_tracking_provider', true); ?> </p> <?php }