WC_Cart::get_shipping_packages PHP Méthode

get_shipping_packages() public méthode

This lets us calculate costs for carts that are shipped to multiple locations. Shipping methods are responsible for looping through these packages. By default we pass the cart itself as a package - plugins can change this. through the filter and break it up.
Since: 1.5.4
public get_shipping_packages ( ) : array
Résultat array of cart items
    public function get_shipping_packages()
    {
        // Packages array for storing 'carts'
        $packages = array();
        $packages[0]['contents'] = $this->get_cart();
        // Items in the package
        $packages[0]['contents_cost'] = 0;
        // Cost of items in the package, set below
        $packages[0]['applied_coupons'] = $this->applied_coupons;
        $packages[0]['user']['ID'] = get_current_user_id();
        $packages[0]['destination']['country'] = WC()->customer->get_shipping_country();
        $packages[0]['destination']['state'] = WC()->customer->get_shipping_state();
        $packages[0]['destination']['postcode'] = WC()->customer->get_shipping_postcode();
        $packages[0]['destination']['city'] = WC()->customer->get_shipping_city();
        $packages[0]['destination']['address'] = WC()->customer->get_shipping_address();
        $packages[0]['destination']['address_2'] = WC()->customer->get_shipping_address_2();
        foreach ($this->get_cart() as $item) {
            if ($item['data']->needs_shipping()) {
                if (isset($item['line_total'])) {
                    $packages[0]['contents_cost'] += $item['line_total'];
                }
            }
        }
        return apply_filters('woocommerce_cart_shipping_packages', $packages);
    }

Usage Example

 /**
  * Stores shipping info on the subscription
  *
  * @param WC_Subscription $subscription instance of a subscriptions object
  * @param WC_Cart $cart A cart with recurring items in it
  */
 public static function add_shipping($subscription, $cart)
 {
     // We need to make sure we only get recurring shipping packages
     WC_Subscriptions_Cart::set_calculation_type('recurring_total');
     foreach ($cart->get_shipping_packages() as $package_index => $base_package) {
         $package = WC_Subscriptions_Cart::get_calculated_shipping_for_package($base_package);
         $recurring_shipping_package_key = WC_Subscriptions_Cart::get_recurring_shipping_package_key($cart->recurring_cart_key, $package_index);
         $shipping_method_id = isset(WC()->checkout()->shipping_methods[$package_index]) ? WC()->checkout()->shipping_methods[$package_index] : '';
         if (isset(WC()->checkout()->shipping_methods[$recurring_shipping_package_key])) {
             $shipping_method_id = WC()->checkout()->shipping_methods[$recurring_shipping_package_key];
             $package_key = $recurring_shipping_package_key;
         } else {
             $package_key = $package_index;
         }
         if (isset($package['rates'][$shipping_method_id])) {
             $item_id = $subscription->add_shipping($package['rates'][$shipping_method_id]);
             if (!$item_id) {
                 throw new Exception(__('Error: Unable to create subscription. Please try again.', 'woocommerce-subscriptions'));
             }
             // Allows plugins to add order item meta to shipping
             do_action('woocommerce_add_shipping_order_item', $subscription->id, $item_id, $package_key);
             do_action('woocommerce_subscriptions_add_recurring_shipping_order_item', $subscription->id, $item_id, $package_key);
         }
     }
     WC_Subscriptions_Cart::set_calculation_type('none');
 }
All Usage Examples Of WC_Cart::get_shipping_packages
WC_Cart