Aiello
Aiello

Reputation: 31

Display Products Shipping Classes in WooCommerce order edit page

I'm using a function that allows me to calculate more than one freight in the cart based on the shipping class of each product. The function has no problem, it's perfect. But when I check the request in the area of administration of woocommerce, I need to identify in the method of submission, the name of the class corresponding to each item.

That is, I need to differentiate the items for each class, because I am using the shipping class to inform different suppliers about the orders made at the store.

But I have no idea how to make this adjustment.

Below is the function I use, working with packages based on the created classes. This function is responsible for calculating the freight in the shopping cart according to the shipping class. How do I display the shipping class name on the order page in the store admin?

    function custom_split_shipping_packages_shipping_class( $packages ) {
    // Reset all packages
    $packages              = array();
    $regular_package_items = array();
    $split_package_items   = array();
    $split_shipping_class = 'da-vinci'; // Shipping class slug
    foreach ( WC()->cart->get_cart() as $item_key => $item ) {
        if ( $item['data']->needs_shipping() ) {
            if ( $split_shipping_class == $item['data']->get_shipping_class() ) {
                $split_package_items[ $item_key ] = $item;
            } else {
                $regular_package_items[ $item_key ] = $item;
            }
        }
    }
    // Create shipping packages
    if ( $regular_package_items ) {
        $packages[] = array(
            'contents'        => $regular_package_items,
            'contents_cost'   => array_sum( wp_list_pluck( $regular_package_items, 'line_total' ) ),
            'applied_coupons' => WC()->cart->get_applied_coupons(),
            'user'            => array(
                 'ID' => get_current_user_id(),
            ),
            '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()
            )
        );
    }
    if ( $split_package_items ) {
        $packages[] = array(
            'contents'        => $split_package_items,
            'contents_cost'   => array_sum( wp_list_pluck( $split_package_items, 'line_total' ) ),
            'applied_coupons' => WC()->cart->get_applied_coupons(),
            'user'            => array(
                 'ID' => get_current_user_id(),
            ),
            '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()
            )
        );
    }
    return $packages;
}
add_filter( 'woocommerce_cart_shipping_packages', 'custom_split_shipping_packages_shipping_class' );

Upvotes: 1

Views: 2056

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253969

I can't get yet the way to to display the shipping classes in order shipping items. but I can display this shipping classes in Order edit "line_items" this way:

// Display the shipping classes names in Order edit line items
add_action( 'woocommerce_after_order_itemmeta', 'custom_admin_order_itemmeta', 15, 3 );
function custom_admin_order_itemmeta( $item_id, $item, $product ){
    if( ! is_admin() ) return; // only backend

    // Display the shipping class names if they exist
    if( $item->is_type( 'line_item' ) ){
        $label = __( 'Shipping class:', 'woocommerce' );
        $class_id = $product->get_shipping_class_id();
        if( $class_id > 0 ){
            $term = get_term_by( 'id', $class_id, 'product_shipping_class' );
            $class_name = esc_html($term->name);
        }
        if( isset($class_name) )
            echo '<div class="wc-order-item-ship-class"><strong>' . $label . '</strong> <span style="color:#ca4a1f;">' . $class_name . '</span></div>';
    }
}

Code goes in function.php file of your active child theme (or active theme) or in any plugin file.

Tested and works. You will get something like:

enter image description here

Note: On Woocommerce 3.2.x I get an error when checking out, related to your actual code.

Upvotes: 2

Related Questions