简体   繁体   中英

Custom terms and conditions checkbox for a specific product category in WooCommerce

On a Woocommerce site, I have different terms and conditions that need to be served, depending on the product category in the cart. This part is working no problem.

However, when the product-specific terms are triggered I need them to be a required field. This was working, except it is also blocking checkout when the indicated category is not in the cart.

This is my code:

add_action('woocommerce_review_order_before_submit', 'add_pipletz_terms', 9);
function add_pipletz_terms() {

    // Show Terms 1
    $special_cat = 'insurance'; // HERE set your special category name, slug or ID
    $bool = false;

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $item = $cart_item['data'];
        if ( has_term( 'insurance', 'product_cat', $item->id ) )
            $bool = true;
    }

    if ( $bool ) {
        ?>
        <p class="form-row terms wc-terms-and-conditions">
        <label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
        <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox pipletz-terms" name="pipletz-terms" <?php checked( apply_filters( 'woocommerce_terms_is_checked_default', isset( $_POST['terms-1'] ) ), true ); ?> id="pipletz-terms"> <span><a href="https://businessbloomer.com/woocommerce-display-product-specific-tc-checkout/" target="_blank">I&rsquo;ve read and accept the Pipletz product terms &amp; conditions</a></span> <span class="required">*</span>
        </label>
        </p>
        <?php
    }   
}

// If customer does not agree to terms
add_action('woocommerce_checkout_process', 'not_approved_pipletz_terms');
function not_approved_pipletz_terms() {
    if ( empty( $_POST['pipletz-terms'] ) ) {
        wc_add_notice( __( 'Please agree to the Pipletz product terms & conditions' ), 'error' );          }
}

Any help on how to to make the terms required only when the category is present would be appreciated.

Updated - First, this code is a bit outdated for woocommerce version 3+ … So I have added the WC 3+ compatibility code.

To avoid this issue you need also to check in your second function for the special product category in cart items (so you need to add the foreach loop too)…

Also in the foreach loop, once the product is found, you can break the loop… To finish, in your code, isset( $_POST['terms-1'] ) should be instead isset( $_POST['pipletz-terms'] ) to work…

So your compatible WC3+ code should be:

add_action('woocommerce_review_order_before_submit', 'add_pipletz_terms', 9 );
function add_pipletz_terms() {

    $special_cat = 'insurance'; // HERE set your special category name, slug or ID
    $bool = false;

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // compatibility with WC +3
        $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
        if ( has_term( $special_cat, 'product_cat', $product_id ) ){ 
            $bool = true;
            break; // added this too
        }
    }

    if ( $bool ) {
        $link = 'https://businessbloomer.com/woocommerce-display-product-specific-tc-checkout/';
        ?>
        <p class="form-row terms wc-terms-and-conditions">
            <label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
                <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox pipletz-terms" name="pipletz-terms" <?php checked( apply_filters( 'woocommerce_terms_is_checked_default', isset( $_POST['pipletz-terms'] ) ), true ); ?> id="pipletz-terms">
                <span>
                    <a href="<?php echo $link; ?>" target="_blank">I&rsquo;ve read and accept the Pipletz product terms &amp; conditions</a>
                </span> <span class="required">*</span>
            </label>
        </p>
        <?php
    }
}

// If customer does not agree to terms
add_action('woocommerce_checkout_process', 'not_approved_pipletz_terms');
function not_approved_pipletz_terms() {

    $special_cat = 'insurance'; // HERE set your special category name, slug or ID
    $bool = false;

    // Checking again if the category is in one cart item
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // compatibility with WC +3
        $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
        if ( has_term( $special_cat, 'product_cat', $product_id ) ){
            $bool = true;
            break; // added this too
        }
    }

    if ( empty( $_POST['pipletz-terms'] ) && $bool )
        wc_add_notice( __( 'Please agree to the Pipletz product terms & conditions' ), 'error' );
}

I have successfully tested this code on WC3+ without issues, so it works now for all cases…

do you think this approach can be used in case we need to serve T&C based on VENDOR (instead of product or product cat) ? Explaination: In case of a Marketplace, vendors should be able to display their own T&C. So, on the checkout page, should appear one or multiple boxes that the customer has to check (mutliple if there are multiple vendors in the order). Does that make sense ? I've checked lot of topic but this one is the only one that actualy treat this subject. Bye.

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