启用Woocommerce中特定产品的“运送到其他地址"复选框 [英] Enable 'Ship to a different address' check box for specific products in Woocommerce

查看:27
本文介绍了启用Woocommerce中特定产品的“运送到其他地址"复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使特定产品必须强制运送到其他地址",而对普通产品则禁用该功能?我尝试使用此代码,但对我而言不起作用.

How to make 'Ship to a different address' mandatory for specific products and disable it for ordinary products? I have tried to use this code, but it did not work for me.

add_filter( 'woocommerce_available_payment_gateways', 'conditionally_disable_cod_payment_method', 10, 1);
function conditionally_disable_cod_payment_method( $gateways ){

    $products_ids = array(793, 796);

    foreach ( WC()->cart->get_cart() as $cart_item ){

        $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
        if (in_array( $cart_item['product_id'], $products_ids ) ){

            add_filter( 'woocommerce_ship_to_different_address_checked', '__return_true' );

        }
    }
    return $gateways;
}

推荐答案

请尝试以下操作:

add_filter( 'woocommerce_ship_to_different_address_checked', 'filter_cart_needs_shipping_address');
function filter_cart_needs_shipping_address( $checked ) {

    $products_ids = array(793, 796);
    $found = $others_found = false;

    foreach ( WC()->cart->get_cart() as $cart_item ){
        if (in_array( $cart_item['data']->get_id(), $products_ids ) ){
            $found = true;
        } else {
            $others_found = true;
        }
    }

    if( $found && ! $others_found )
        $checked = true;

    return $checked;
} 

这篇关于启用Woocommerce中特定产品的“运送到其他地址"复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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