Group Of Oceninfo
Group Of Oceninfo

Reputation: 358

WooCommerce Show checkout fields based on product category

I have added below code to show currency switcher dropdown on the WooCommerce checkout page which is working fine, but I don’t want to show currency switcher field if anyone has added product from "Games" category and use only default store currency

Code 1

add_action('woocommerce_before_checkout_billing_form', 'wps_add_select_checkout_field');
function wps_add_select_checkout_field( $checkout ) {
    echo '<label for="payment_option" class="payment_option">'.__('Preferred currency').'</label>';
    echo '<div class="own">', do_shortcode('[woocs]'), '</div>';
    return $checkout;
}

//* Process the checkout
add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
function wps_select_checkout_field_process() {
    global $woocommerce;
    // Check if set, if its not set add an error.
    if ($_POST['payopt'] == "blank")
     wc_add_notice( '<strong>Please select a currency</strong>', 'error' );
}

Based on this answer thread: Checking cart items for a product category in Woocommerce I have tried below code and I think something is missing, If I am using below code it's not showing currency switcher at all even if product is from "Games" or other categories.

Code 2

add_action('woocommerce_before_cart', 'check_product_category_in_cart');
function check_product_category_in_cart() {
    // Here set your product categories in the array (can be IDs, slugs or names)
    $categories = array('games');
    $found      = false; // Initializing

    // Loop through cart items      
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // If product categories is found
        if ( !has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $found = true; // Set to true
            break; // Stop the loop
        }
    }

    // If any defined product category is found, run below code
    if ( $found ) {
        add_action('woocommerce_before_checkout_billing_form', 'wps_add_select_checkout_field');
        function wps_add_select_checkout_field( $checkout ) {
            echo '<label for="payment_option" class="payment_option">'.__('Preferred currency').'</label>';
            echo '<div class="own">', do_shortcode('[woocs]'), '</div>';
            return $checkout;
        }
        //* Process the checkout
        add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
        function wps_select_checkout_field_process() {
            global $woocommerce;
            // Check if set, if its not set add an error.
            if ($_POST['payopt'] == "blank")
             wc_add_notice( '<strong>Please select a currency</strong>', 'error' );
        }
    }
}

Do you have any other suggestion for the same? Where I can add currency switcher on checkout page based on cart product category. Code 1 is working fine on the checkout page but I don't want to run that code if product category is games.

Upvotes: 2

Views: 2055

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

You are not using the correct hook as woocommerce_before_cart action hook is only triggered in cart page, but not in checkout and it can't work this way. Instead try to use the following:

// Utility function that checks if at least a cart items remains to a product category
function has_product_category_in_cart( $product_category ) {
    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // If any product category is found in cart items
        if ( has_term( $product_category, 'product_cat', $cart_item['product_id'] ) ) {
            return true;
        }
    }
    return false;
}

// Add a custom select field in checkout
add_action('woocommerce_before_checkout_billing_form', 'add_custom_checkout_select_field');
function add_custom_checkout_select_field( $checkout ) {
    // Here set in the function your product category term ID, slugs, names or array
    if ( ! has_product_category_in_cart( 'games' ) && shortcode_exists( 'woocs' ) ) {
        echo '<label for="payment_option" class="payment_option">'.__('Preferred currency').'</label>';
        echo '<div class="own">' . do_shortcode('[woocs]') . '</div>';
    }
}

// Custom Checkout fields validation
add_action('woocommerce_checkout_process', 'custom_checkout_select_field_validation');
function custom_checkout_select_field_validation() {
    if ( isset($_POST['payopt']) && empty($_POST['payopt']) )
        wc_add_notice( '<strong>Please select a currency</strong>', 'error' );
}

Code goes in function.php file of your active child theme (active theme). Untested but it should works.

Upvotes: 2

Related Questions