在 WooCommerce 订单编辑页面中显示产品运输类别 [英] Display Products Shipping Classes in WooCommerce order edit page

查看:30
本文介绍了在 WooCommerce 订单编辑页面中显示产品运输类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的函数允许我根据每种产品的运输类别计算购物车中的多个运费.功能没有问题,很完美.但是我在woocommerce的管理领域查看请求的时候,需要在提交的方法中标识每个item对应的类名.

也就是说,我需要区分每个类别的商品,因为我使用运输类别来通知不同的供应商在商店下的订单.

但我不知道如何进行这种调整.

下面是我使用的函数,使用基于创建的类的包.该函数负责根据运输等级计算购物车中的运费.如何在商店管理的订单页面上显示配送类别名称?

 function custom_split_shipping_packages_shipping_class( $packages ) {//重置所有包$packages = array();$regular_package_items = array();$split_package_items = array();$split_shipping_class = '达芬奇';//运输类 slugforeach ( WC()->cart->get_cart() as $item_key => $item ) {如果 ( $item['data']->needs_shipping() ) {if ( $split_shipping_class == $item['data']->get_shipping_class() ) {$split_package_items[ $item_key ] = $item;} 别的 {$regular_package_items[ $item_key ] = $item;}}}//创建运输包如果( $regular_package_items ){$packages[] = 数组('内容' =>$regular_package_items,'contents_cost' =>array_sum( wp_list_pluck( $regular_package_items, 'line_total' ) ),'applied_coupons' =>WC()->购物车->get_applied_coupons(),'用户' =>大批('ID' =>get_current_user_id(),),'目的地' =>大批('国家' =>WC()->客户->get_shipping_country(),'状态' =>WC()->客户->get_shipping_state(),'邮政编码' =>WC()->客户->get_shipping_postcode(),'城市' =>WC()->客户->get_shipping_city(),'地址' =>WC()->客户->get_shipping_address(),'address_2' =>WC()->客户->get_shipping_address_2()));}如果( $split_package_items ){$packages[] = 数组('内容' =>$split_package_items,'contents_cost' =>array_sum( wp_list_pluck( $split_package_items, 'line_total' ) ),'applied_coupons' =>WC()->购物车->get_applied_coupons(),'用户' =>大批('ID' =>get_current_user_id(),),'目的地' =>大批('国家' =>WC()->客户->get_shipping_country(),'状态' =>WC()->客户->get_shipping_state(),'邮政编码' =>WC()->客户->get_shipping_postcode(),'城市' =>WC()->客户->get_shipping_city(),'地址' =>WC()->客户->get_shipping_address(),'address_2' =>WC()->客户->get_shipping_address_2()));}返回 $packages;}add_filter('woocommerce_cart_shipping_packages', 'custom_split_shipping_packages_shipping_class');

解决方案

我还没有办法在订单发货项目中显示发货类别.但我可以在订单编辑line_items"中显示此运输类别;这样:

//在订单编辑行项目中显示运输类别名称add_action('woocommerce_after_order_itemmeta', 'custom_admin_order_itemmeta', 15, 3);函数 custom_admin_order_itemmeta( $item_id, $item, $product ){如果(!is_admin())返回;//只有后端//如果存在,则显示运输类别名称if($item->is_type('line_item')){$label = __( 'Shipping class:', 'woocommerce' );$class_id = $product->get_shipping_class_id();如果( $class_id > 0 ){$term = get_term_by('id', $class_id, 'product_shipping_class');$class_name = esc_html($term->name);}如果( isset($class_name) )echo '

'.$标签.'</strong><span style="color:#ca4a1f;">'.$class_name .'</span></div>';}}

代码位于活动子主题(或活动主题)的 function.php 文件或任何插件文件中.

经过测试并有效.你会得到类似的东西:

<块引用>

注意:在 Woocommerce 3.2.x 上,我在结帐时遇到错误,与您的实际代码有关.

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' );

解决方案

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:

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

这篇关于在 WooCommerce 订单编辑页面中显示产品运输类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆