woocommerce offtopic – Group product by Instance ID

Question

My Goal is

  • if instance name ‘flexible_shipping_single:22’ to group them into One package.
  • if instance name ‘flexible_shipping_single:19’ to group them into another package.

and all the rest of product to group them by Product ID

I am using this hook “woocommerce_cart_shipping_packages”

my problem i am not able to get the available shipping method instance for each product in cart.
there is any possibility to get the instance ID ?

class pt_woo_cart_group {
 
    public function __construct() {
        add_filter('woocommerce_cart_shipping_packages', array($this, 'group_cart'));
    }


    /**
     * using filter woocommerce_cart_shipping_packages
     * 
     * @param array $packages
     * @return array
     */
    public function group_cart($packages) {
$rate_table = array();

$shipping_methods = WC()->shipping->get_shipping_methods();
foreach($shipping_methods as $shipping_method){
    //var_dump($shipping_method);
}

        
        // Reset the packages
        $packages_reset = array();
        //var_dump(WC()->cart);
        $cart_items = WC()->cart->get_cart();
        $n = 0;
    
        foreach ( $cart_items as $item ) {
            //var_dump($item);
            if ( $item['data']->needs_shipping() ) {
                // Put inside packages
                $packages_reset[ $n ] = array(
                    'contents' => array($item),
                    //'contents_cost' => array_sum( wp_list_pluck( $item, 'line_total' ) ),
                    'applied_coupons' => WC()->cart->applied_coupons,
                    'destination' => array(
                        'country' => WC()->customer->get_shipping_country(),
                        'state' => WC()->customer->get_shipping_state(),
                        'postcode' => WC()->customer->get_shipping_postcode(),
                        'city' => WC()->customer->get_shipping_city(),
                        'address' => WC()->customer->get_shipping_address(),
                        'address_2' => WC()->customer->get_shipping_address_2()
                    )
                );

                // Determine if 'ship_via' applies
                $key = $item['data']->get_shipping_class_id();
                $key . "<br>";
                $packages_reset[ $n ]['ship_via'] = '';
                $n++;
            }
        }
 
 
        return $packages_reset;
    }
 
}
 
new pt_woo_cart_group();

N.B: i don’t use shipping classes, i only use plugin to handling the shipping calculation fees

I tried “woocommerce_add_cart_item_data” maybe to pass the available shipping method for each product, but i am not able to get the available shipping method for each product by zone…

Kindly advice

0
Tayssir Ch 8 months 2021-12-19T12:01:44-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse