【问题标题】:Add a body class for specific selected shipping option in Woocommerce checkout在 Woocommerce 结帐中为特定选定的运输选项添加正文类别
【发布时间】:2018-07-29 13:30:02
【问题描述】:

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

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;
}

【问题讨论】:

  • 不确定其余的,因为我不熟悉 WC 的那一面,但 $custom_terms = get_the_terms(0, 'product_cat'); 这条线在理论上对我来说似乎是错误的,因为它试图从一个帖子中获取 product_cat 条款ID 为 0 - 因为 WP 从 1 开始编号,所以它永远不会存在。您希望从哪里获得 product_cat 条款?

标签: php jquery css wordpress woocommerce


【解决方案1】:

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

由于您没有为“Delivery price on request”运输方式提供运输方式 ID,因此 php 代码会在将其传递给 jQuery 代码之前找到它。当“请求的交货价格”将是所选方法时,将在Checkout页面上的身体现有类中附加“交付 - 不可用”。

代码:

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 文件中。

经过测试并且有效。

【讨论】:

    猜你喜欢
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多