wordpress 根据购物车中的产品ID在WooCommerce结帐时显示/隐藏复选框

r55awzrz  于 2022-11-02  发布在  WordPress
关注(0)|答案(1)|浏览(274)

我正在使用下面的代码片段来显示所有产品的自定义复选框,但如果ID为32的产品在购物车中,我想隐藏/不显示它。我如何修改此代码来实现这一点?提前感谢。

add_action( 'woocommerce_review_order_before_submit', 'bbloomer_add_checkout_privacy_policy', 9 );

function bbloomer_add_checkout_privacy_policy() {

woocommerce_form_field( 'privacy_policy', array(
   'type'          => 'checkbox',
   'class'         => array('form-row privacy'),
   '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,
   'label'         => 'I\'ve read and accept the <a href="/privacy-policy">Privacy Policy</a>',
)); 

}

// Show notice if customer does not tick

add_action( 'woocommerce_checkout_process', 'bbloomer_not_approved_privacy' );

function bbloomer_not_approved_privacy() {
    if ( ! (int) isset( $_POST['privacy_policy'] ) ) {
        wc_add_notice( __( 'Please acknowledge the Privacy Policy' ), 'error' );
    }
}
3ks5zfa0

3ks5zfa01#

在实际显示复选框字段之前,您必须先浏览购物车,查看产品ID是否不存在。
对于验证也是如此,因为它是必填字段,否则即使不存在,它也将是必填字段。
因此,您可以获得:

// Function to check if a certain product ID is in cart
function is_product_in_cart() {
    // Check if product in cart
    // Multiple product IDs can be entered, separated by a comma
    $targeted_ids = array( 32, 1234, 5678 );

    // Flag no product in cart
    $flag = false;

    // WC Cart NOT null
    if ( ! is_null( WC()->cart ) ) {
        // Loop through cart items
        foreach( WC()->cart->get_cart() as $cart_item ) {
            // Check cart item for defined product Ids
            if ( in_array( $cart_item['product_id'], $targeted_ids ) ) {
                // Product is in cart
                $flag = true;

                // Break loop
                break;
            }
        }
    }

    return $flag;
}

// Add field 
function action_woocommerce_review_order_before_submit() {    
    // NOT true
    if ( ! is_product_in_cart() ) {
        // Add checkbox
        woocommerce_form_field( 'privacy_policy', array(
            'type'          => 'checkbox',
            'class'         => array( 'form-row privacy' ),
            '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,
            'label'         => 'I\'ve read and accept the <a href="/privacy-policy">Privacy Policy</a>',
        ));
    }
}
add_action( 'woocommerce_review_order_before_submit', 'action_woocommerce_review_order_before_submit', 9 );

// Validate
function action_woocommerce_checkout_process() {
    // NOT true
    if ( ! is_product_in_cart() ) {
        // NOT isset
        if ( ! isset( $_POST['privacy_policy'] ) ) {
            wc_add_notice( __( 'Please acknowledge the Privacy Policy', 'woocommerce' ), 'error' );
        }
    }
}
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );

// Save field
function action_woocommerce_checkout_create_order( $order, $data ) {
    // Isset    
    if ( isset( $_POST['privacy_policy'] ) ) {
        $order->update_meta_data( 'privacy_policy', sanitize_text_field( $_POST['privacy_policy'] ) );
    } 
}
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );

相关问题