根据购物车商品数量取消设置 WooCommerce 结帐字段

Unset WooCommerce checkout fields based on cart items quantities

根据三个不同产品(数组)的数量,我需要取消设置不同的结账字段。

问题是,无论我如何尝试使用下面的代码以及尝试什么 - 我在网站上都收到“严重错误”。

我需要检查:

这是代码:

add_filter('woocommerce_checkout_fields', 'remove_cheeckout_fields_based_on_product_qty' );
function remove_cheeckout_fields_based_on_product_qty( $fields ) {

    $targeted_ids = array( 123, 456, 789 ); // how do I add qty to these?

    $found = false;

    foreach ( WC()->cart->get_cart() as $item ) {
    
        if (array_intersect( $targeted_ids, array( $item['product_id'], $item['variation_id'] ) ) ) {

    $found = true;

    break;

        }
    }
    
    if ( $found ) {
    
    unset($fields['shipping']['shipping_country']);
    unset($fields['shipping']['shipping_state']);
    unset($fields['billing']['billing_country']);
    unset($fields['billing']['billing_state']);

    } else {

    unset($fields['shipping']['shipping_company']);
    unset($fields['shipping']['shipping_country']);
    unset($fields['shipping']['shipping_address_1']);
    unset($fields['shipping']['shipping_address_2']);
    unset($fields['shipping']['shipping_city']);
    unset($fields['shipping']['shipping_state']);
    unset($fields['shipping']['shipping_postcode']);
    unset($fields['billing']['billing_address_1']);
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_city']);
    unset($fields['billing']['billing_postcode']);
    unset($fields['billing']['billing_country']);
    unset($fields['billing']['billing_state']);
    unset($fields['billing']['billing_company']);
    return $fields;
}

有什么建议吗?

您遇到的“严重错误”是因为您的 else 语句没有正确应用。

要获取购物车商品数量,您可以使用 WC()->cart->get_cart_item_quantities()

操作如下:

  • 如果数组中三个产品中的任何一个的数量为 8 个或更多,则删除国家和州(账单和运费)。

  • 如果数组中三个产品中的任何一个的数量小于 8,删除除名称、phone 和电子邮件(账单和送货)之外的所有字段。

所以你得到:

function filter_woocommerce_checkout_fields( $fields ) {
    // The targeted product ids, multiple product IDs can be entered, separated by a comma
    $targeted_ids = array( 123, 456, 789 );
    
    // Required minimum quantity
    $minimum_quantity = 8;
    
    // Flag, default = false
    $flag = false;
    
    // Loop trough cart items quantities
    foreach( WC()->cart->get_cart_item_quantities() as $product_id => $cart_item_quantity ) {
        // Product ID in targeted IDs?
        if ( in_array ( $product_id, $targeted_ids ) ) {
            // Greater than or equal to
            if ( $cart_item_quantity >= $minimum_quantity ) {
                $flag = true;
            }

            // Break loop
            break;
        }
    }
    
    // True
    if ( $flag ) {      
        unset( $fields['shipping']['shipping_country'] );
        unset( $fields['shipping']['shipping_state'] );
        unset( $fields['billing']['billing_country'] );
        unset( $fields['billing']['billing_state'] );
    } else {        
        // Unset
        unset( $fields['shipping']['shipping_company'] );
        unset( $fields['shipping']['shipping_country'] );
        unset( $fields['shipping']['shipping_address_1'] );
        unset( $fields['shipping']['shipping_address_2'] );
        unset( $fields['shipping']['shipping_city'] );
        unset( $fields['shipping']['shipping_state'] );
        unset( $fields['shipping']['shipping_postcode'] );
        unset( $fields['billing']['billing_address_1'] );
        unset( $fields['billing']['billing_address_2'] );
        unset( $fields['billing']['billing_city'] );
        unset( $fields['billing']['billing_postcode'] );
        unset( $fields['billing']['billing_country'] );
        unset( $fields['billing']['billing_state'] );
        unset( $fields['billing']['billing_company'] );
    }    

    return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'filter_woocommerce_checkout_fields', 10, 1 );