在WooCommerce结帐页面上将运输类别添加到身体类别 [英] Add shipping class to body class on WooCommerce checkout page

查看:60
本文介绍了在WooCommerce结帐页面上将运输类别添加到身体类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望已在结帐页面上将运输类别添加为主体类别.

I wish to have added the shipping classes as a body class on the checkout page.

最好添加所有活动的运输类别,否则,如果购物车中存在某个运输类别,则添加一个特定的类别.

It would be best if all active shipping classes is added, but otherwise add a specific class if a certain shipping class is present in the cart.

有人可以帮助我吗..我能找到的所有其他指南仅涉及取消费率/运输选项,因此,我真的希望在这里寻求帮助.

Can anyone help me with this.. all other guides I can find only relates to remove rates/shipping options, so really hope for some help here.

推荐答案

以下内容将添加产品(购物车)发货类别作为其他主体类别(es)结帐页面上的标签:

The following will add the product(s) (cart item(s)) shipping class(es) as additional body class(es) tag(s) on checkout page:

add_filter( 'body_class', 'add_shipping_classes_to_body_class' );
function add_shipping_classes_to_body_class( $classes ) {
    // Only on checkout page
    if( is_checkout() && ! is_wc_endpoint_url() ) {
        // Loop through cart items
        foreach ( WC()->cart->get_cart() as $item ) {
            // If a shipping class is set for the product, we add it to body classes
            if ( $shipping_class = $item['data']->get_shipping_class() )
                $classes[] = $shipping_class;
        }
    }
    return $classes;
}

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

这篇关于在WooCommerce结帐页面上将运输类别添加到身体类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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