在 Woocommerce 结帐中为特定选定的运输选项添加正文类 [英] Add a body class for specific selected shipping option in Woocommerce checkout

查看:33
本文介绍了在 Woocommerce 结帐中为特定选定的运输选项添加正文类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果访问者在 Woocommerce 结帐页面上处于特定的运输选项内,我正在尝试向页面正文添加一个类.我已经完成了以下操作,但没有添加课程?请问有人可以帮忙吗?

I'm trying to add a class to the body of the page if a visitor is within a specific shipping option when on the Woocommerce checkout page. I've done the below but it's not adding the class? Can anyone help please?

add_filter( 'body_class', 'bbloomer_wc_product_cats_css_body_class' );

function bbloomer_wc_product_cats_css_body_class( $classes, $available_gateways ){

    global $woocommerce;
    $chosen_titles = array();
    $available_methods = $woocommerce->shipping->get_packages();
    $chosen_rates = ( isset( $woocommerce->session ) ) ? $woocommerce->session->get( 'chosen_shipping_methods' ) : array();

    foreach ($available_methods as $method)
           foreach ($chosen_rates as $chosen) {
                if( isset( $method['rates'][$chosen] ) ) $chosen_titles[] = $method['rates'][ $chosen ]->label;
            }
    if( in_array( 'Delivery price on request', $chosen_titles ) ) {
    $custom_terms = get_the_terms(0, 'product_cat');
    if ($custom_terms) {
      foreach ($custom_terms as $custom_term) {
        $classes[] = 'delivery-not-available';
      }
    }       
    }
  return $classes;
}

推荐答案

由于这是客户端的实时事件,因此只能使用 Javascript/jQuery 来完成

As this is a live event on client side, it can only be done with Javascript/jQuery

由于您没有为按要求交货价格"运输方式提供运输方式 ID,php 代码会在将其传递给 jQuery 代码之前找到它.当按要求交货价格"将是所选方法时,类交货不可用"将附加到结帐页面上的正文现有类中.

As you don't provide the shipping method ID for 'Delivery price on request' shipping method, the php code will find it before passing it to the jQuery code. When 'Delivery price on request' will be the chosen method, the class 'delivery-not-available' will be appended in body existing classes on checkout page.

代码:

add_filter( 'wp_footer','custom_product_title_script' );
function custom_product_title_script(){
    if( ! is_checkout() ) return; // only on checkout

    $method_id = "''";
    // Find the Shipping Method ID for the Shipping Method label name 'Delivery price on request'
    foreach( WC()->session->get('shipping_for_package_0')['rates'] as $rate_key => $rate ){
        if( 'Delivery price on request' == $rate->label ){
            $method_id = $rate_key;
            break;
        }
    }
    ?>
        <script type="text/javascript">
            (function($){
                // variables initialization
                var a = 'input[name^="shipping_method[0]"]',
                    b = a+':checked',
                    c = 'delivery-not-available',
                    d = '<?php echo $method_id; ?>';

                if( $(b).val() == d )
                    $('body').addClass(c);
                else
                    $('body').removeClass(c);

                $( 'form.checkout' ).on( 'change', a, function() {
                    if( $(b).val() == d )
                        $('body').addClass(c);
                    else
                        $('body').removeClass(c);
                });
            })(jQuery);
        </script>
    <?php
}

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

经过测试并有效.

这篇关于在 Woocommerce 结帐中为特定选定的运输选项添加正文类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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