Impof
Impof

Reputation: 23

Woocommerce checkout page depending on product category

I am building on a website using woocommerce.

For some products clients need to write the name of their child in the Child name field on the checkout page. (The site sells music lessons)

However for other products like giftcards I dont need this Child name field. I can't find any plugin that can show a different checkout page depending on the catagory of the product that the client is buying.

Anyone an idea for making this possible?

Thnx in advance!

Upvotes: 0

Views: 6162

Answers (2)

boar
boar

Reputation: 1

This worked better for me:

  
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
add_filter( 'woocommerce_default_address_fields' , 'optional_default_address_fields' );



 function optional_default_address_fields( $address_fields ) {
 $address_fields['postcode']['required'] = false;
 $address_fields['city']['required'] = false;
 $address_fields['state']['required'] = false;
 $address_fields['address_1']['required'] = false;
 $address_fields['country']['required'] = false;
 $address_fields['billing_company']['required'] = false;

 return $address_fields;
 }
 
function custom_override_checkout_fields( $fields ) {
    $categories = array('ajakirjad');

    $foundAjakiri = false;
    $foundOthers = false;
    
    
    foreach ( WC()->cart->get_cart() as $cart_item ){
        
        if(has_term( $categories, 'product_cat', $cart_item['product_id'] )) {
            $foundAjakiri = true;
        } else {
            $foundOthers = true;
        }
    }
    if($foundAjakiri == true && $foundOthers == false) {
//        echo '1';
    } elseif($foundAjakiri == false && $foundOthers == true) {
        
        $fields['billing']['billing_address_1']['required'] = false;
        $fields['billing']['billing_address_2']['required'] = false;
        $fields['billing']['billing_city']['required'] = false;
        $fields['billing']['billing_postcode']['required'] = false;
        $fields['billing']['billing_state']['required'] = false;
        $fields['billing']['billing_country']['required'] = false;
        $fields['billing']['billing_country']['class'][] = 'no-need';
        $fields['billing']['billing_company']['required'] = false;
        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_phone']);
        //unset($fields['order']['order_comments']);
    }
    
    //add_filter( 'woocommerce_enable_order_notes_field', '__return_false', 9999 );
    add_filter( 'woocommerce_checkout_fields' , 'remove_order_notes' );
    
    
    
    return $fields;
}

Upvotes: 0

Impof
Impof

Reputation: 23

I think I found a website with the answer:

https://wordimpress.com/create-conditional-checkout-fields-woocommerce/

I'm going to try this and place the outcome here.


*** okay couple of hours later! It did the job, the codes in the website I posted are used for a single product ID. If you want to check for category ID you can change this code:

 /**
 * Check if Conditional Product is In cart
 *
 * @param $product_id
 *
 * @return bool
 */
function wordimpress_is_conditional_product_in_cart( $product_id ) {
    //Check to see if user has product in cart
    global $woocommerce;

    //flag no book in cart
    $book_in_cart = false;

    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];

        if ( $_product->id === $product_id ) {
            //book is in cart!
            $book_in_cart = true;

        }
    }

    return $book_in_cart;

}

for:

/**
 * Check if Conditional Product is In cart
 *
 * @param $product_id
 *
 * @return bool
 */
function wordimpress_is_conditional_product_in_cart( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;

//flag no book in cart
$book_in_cart = false;

foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
        $terms = get_the_terms( $_product->id, 'product_cat' );

            foreach ($terms as $term) {
                $_categoryid = $term->term_id;
            }

    if ( $_categoryid === 14 ) {
        //book is in cart!
        $book_in_cart = true;
    }

}

return $book_in_cart;

}

If you need to check multiple categorie ID's or product ID's you can copy this excample:

/**
 * Check if Conditional Product is In cart
 *
 * @param $product_id
 *
 * @return bool
 */
function wordimpress_is_conditional_product_in_cart( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;

//flag no book in cart
$book_in_cart = false;

foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
        $terms = get_the_terms( $_product->id, 'product_cat' );

            foreach ($terms as $term) {
                $_categoryid = $term->term_id;
            }

    if (( $_categoryid === 14 ) || ( $_categoryid === 16 )) {
        //book is in cart!
        $book_in_cart = true;
    }

}

return $book_in_cart;

}

I Hope this post will hopefully save somebody a lot of time searching all the loose bits of information ;)

Upvotes: 1

Related Questions