Z
Z
zeaovede2021-12-29 17:11:01
WordPress
zeaovede, 2021-12-29 17:11:01

How to disable woocommerce checkout fields?

How to disable checkout fields for certain woocommerce products on checkout page ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2021-12-29
@zeaovede

How will you select certain products?
In a separate category or tag? Product type?

add_filter( 'woocommerce_checkout_fields', 'conditionally_remove_checkout_fields', 25, 1 );
function conditionally_remove_checkout_fields( $fields ) {

    // Определяем категорию для которой отключем поля
    $categories = array('house');

    $found = false;

    // Проверяем товары: находятся ли они в выше указаной категории?
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $found = true;
            break;
        }
    }

    //  Если товар принадлежит выше указаной категории, тогда отключаем поля
    if ( $found ) {

        // hide the billing fields
        unset($fields['shipping']['shipping_first_name']);
        unset($fields['shipping']['shipping_last_name']);
        unset($fields['shipping']['shipping_company']);
        unset($fields['shipping']['shipping_address_1']);
        unset($fields['shipping']['shipping_address_2']);
        unset($fields['shipping']['shipping_city']);
        unset($fields['shipping']['shipping_postcode']);
        unset($fields['shipping']['shipping_country']);
        unset($fields['shipping']['shipping_state']);
        unset($fields['shipping']['shipping_phone']);

    }
    return $fields;
}

You can take it as a basis and customize it for yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question