【问题标题】:Woocommerce checkout page required checkbox error is showing error even when checkbox is not shownWoocommerce 结帐页面需要复选框错误即使未显示复选框也显示错误
【发布时间】:2021-09-03 01:55:04
【问题描述】:

我需要 woocommerce 结帐页面上的第二个必填复选框(类似于现有的“条款”复选框)。我需要它仅在购物车中有特定产品时显示。到目前为止,我已经弄清楚了这段代码,除了一点之外,它工作得很好:

当产品不在购物车中且复选框未显示时,也会抛出“未选中复选框”的错误消息。

我可以从代码的结构中看出,最有可能的错误是抛出错误消息的代码不在检查商品是否在购物车中的 if 语句中,我尝试了各种方法重组这段代码,但我没有设法让它工作。有人可以帮忙吗?

代码如下:

/*Add custom text to checkout page if PDU subscription order is in the cart*/
add_action( 'woocommerce_review_order_before_submit', 'wordimpress_custom_checkout_field', 10 );

function wordimpress_custom_checkout_field( $checkout ) {

    //Check if Book in Cart (UPDATE WITH YOUR PRODUCT ID)
    $book_in_cart = wordimpress_is_conditional_product_in_cart( 161042 );

    //Book is in cart so show additional fields
    if ( $book_in_cart === true ) {
        woocommerce_form_field( 'checkout_checkbox', array( // CSS ID
               'type'          => 'checkbox',
               'class'         => array('form-row mycheckbox'), // CSS Class
               'label_class'   => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
               'input_class'   => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
               'required'      => true, // Mandatory or Optional
               'label'         => 'I have read and agree to the <a href="#" target="_blank" rel="noopener">subscription terms</a>', // Label and Link
            ));    
    }
}
        
add_action( 'woocommerce_checkout_process', 'bt_add_subscription_checkbox_warning' ); 
//Alert box if not checked
function bt_add_subscription_checkbox_warning() {
    if ( ! (int) isset( $_POST['checkout_checkbox'] ) ) {
    wc_add_notice( __( 'You must read and accept the subscription terms to start a monthly subscription.' ), 'error' );
    }
}

//Check if Conditional Product is In cart
function wordimpress_is_conditional_product_in_cart( $product_id ) {
    //Check to see if user has product in cart
    global $woocommerce;
    $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;
}

【问题讨论】:

    标签: php woocommerce


    【解决方案1】:

    好吧,经过一天的努力并尝试了几乎所有可能的组合后,我找到了答案。我将在下面发布完整的代码,以供将来遇到相同问题的任何人使用。

    add_action( 'woocommerce_review_order_before_submit', 'wordimpress_custom_checkout_field', 10 );
    
    function wordimpress_custom_checkout_field( $checkout ) {
        $book_in_cart = wordimpress_is_conditional_product_in_cart( 161042 ); //product ID
        //Book is in cart so show additional fields
        if ( $book_in_cart === true ) {
            woocommerce_form_field( 'checkout_checkbox', array( // CSS ID
                   'type'          => 'checkbox',
                   'class'         => array('form-row mycheckbox'), // CSS Class
                   'label_class'   => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
                   'input_class'   => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
                   'required'      => true, // Mandatory or Optional
                   'label'         => 'I have read and agree to the <a href="#" target="_blank" rel="noopener">subscription terms</a>', // Label and Link
                ));    
        }
    }
            
    add_action( 'woocommerce_checkout_process', 'bt_add_subscription_checkbox_warning' ); 
        //Alert box if not checked
        function bt_add_subscription_checkbox_warning() {
        $book_in_cart = wordimpress_is_conditional_product_in_cart( 161042 );
        if ( ( $book_in_cart === true ) && ! (int) isset( $_POST['checkout_checkbox'] ) ) {
        wc_add_notice( __( 'You must read and accept the subscription terms to start a monthly subscription.' ), 'error' );
        }
    }
    
    //Check if Conditional Product is In cart
    function wordimpress_is_conditional_product_in_cart( $product_id ) {
        global $woocommerce;
        $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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 2016-04-29
      • 1970-01-01
      相关资源
      最近更新 更多