简体   繁体   中英

Conditionally display Woocommerce checkout field

On a Wordpress website,I am currently trying to setup a Woocommerce and Learnpress plugins. I Use Woocommerce Checkout Add-ons commercial plugin which allows to create some additional checkout fields.

I would like to display conditionally some checkout fields only when certain Learnpress courses are present in the checkout cart.

The code present in the checkout plugin is as follows:

<?php if ( $add_on_fields ) : ?>

<div id="wc_checkout_add_ons">
<?php
    foreach ( $add_on_fields as $key => $field ) :
        woocommerce_form_field( $key, $field, WC()->checkout()->get_value( 
        $key ) );
    endforeach;
?>
</div>

<?php endif; ?>

So started going this route:

<div id="wc_checkout_add_ons">
<?php 
    $course_category = get_the_terms( $post->ID, 'course_category' );
    echo $course_category; 
    if ($course_category == 'weekend-intensives') { 
        foreach ( $add_on_fields as $key => $field ) : 
            woocommerce_form_field( $key, $field, WC()->checkout()->get_value( $key ) ); 
        endforeach; 
    } else { 
        echo ('Proceed with checkout'); 
    } 
?> 
</div>

Right now I don't even get that initial echo for $course_category so I know that I am already wrong there...

I need to figure out what the code would be for getting the learnpress course category of the course in the checkout/cart.
I know there is a lot more to it than this and I am likely way off, but I am willing to work through it with some assistance.

Any help would be greatly appreciated.

You can use has_term() specific WordPress conditional function for custom taxonomies and custom taxonomies as "Product categories" or for your case "Course categories" that you can use easily.

1) You need to check cart items, to see if there is a cart item that remains to 'weekend-intensives' Course category and this has to be done at the beginning of the code. This way you can alter the existing condition if ( $add_on_fields ) :

2) You need to be sure that the Course categories taxonomy are applicable to WooCommerce product custom post type . If not you will not be able to check for any Course categories cart items.

So the correct way to do it is:

<?php
    ## ---- Custom code start here ---- ##
    $is_in_cart = false;

    // Checking cart items for specific "Course category"
    foreach( WC->cart->get_cart() as $cart_item ){
        // Here your Course category
        if( has_term( 'weekend-intensives', 'course_category', $cart_item['product_id'] ){
            $is_in_cart = true; // Found
            break; // Stop the loop
        }
    }
    // adding our condition in existing code if statement
    if ( $add_on_fields && $is_in_cart ) : 

    ## ---- Custom code Ends here ---- ##

    // if ( $add_on_fields ) :
?>

<div id="wc_checkout_add_ons">
<?php
    foreach ( $add_on_fields as $key => $field ) :
        woocommerce_form_field( $key, $field, WC()->checkout()->get_value( $key ) );
    endforeach;
?>
</div>

<?php endif; ?>

This is untested, but it should work (if Course categories are applicable to WC products)


UPDATE

Check if a product ID works with "weekend-intensives" course category:

  • Code goes in function.php file of your active child theme (or theme).
  • Set inside it the correct product ID to check and save.
  • Go in archives pages or product pages to see the result (once saved).

The code:

// Checking for "weekend-intensives" course category in shop and product pages
add_action( 'woocommerce_before_main_content', function(){
    // Only for admin user role
    if( ! current_user_can('edit_products')) return;

    // ==> BELOW set your product ID to check
    $product_id = 43;

    // Output the raw cart object data
    if( has_term( 'weekend-intensives', 'course_category', $product_id ){
        echo '<pre>YES! This product works with "weekend-intensives" course category</pre>';
    } else {
        echo '<pre>This product DOES NOT WORK with "weekend-intensives" course category</pre>';
    }
}, 987 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM