从WooCommerce中的自定义字段更新add_to_cart上的用户元

时间:2022-10-25 11:01:33

I am displaying a checkbox on single product pages that belong to a specific category. The user has to check this checkbox before adding the product to the cart. I want to update a custom user meta field with the value of this checkbox once it's been check and the user goes to add the product to the cart.

我在属于特定类别的单个产品页面上显示一个复选框。在将产品添加到购物车之前,用户必须选中此复选框。我想在检查后使用此复选框的值更新自定义用户元字段,然后用户将产品添加到购物车。

Here is a shortened example:

这是一个缩短的例子:

//function to update user meta on add_to_cart if checkbox is checked
function checked_meta_add_to_cart(){
    //if checkbox is checked
    if(isset($_POST['myCheckbox'])){
        //get user id
        $user_id = get_current_user_id();
        //update custom meta field for current user
        update_user_meta( $user_id, 'myMetaField', $_POST['myCheckbox']);

    }
}
//function to display checkbox
function custom_single_checkbox(){
    if(is_user_logged_in() && is_product()){
        global $post;
        //get the current user id
        $user_id = get_current_user_id();
        //get the custom meta field to test if it's been filled already
        $metaField = get_user_meta( $user_id, 'myMetaField', true );
        //get array of the product's categories
        $terms = wp_get_post_terms( $post->ID, 'product_cat' );
        //form an array of category slugs
        foreach ( $terms as $term ) $categories[] = $term->slug;

        // if the product has the specified category and our custom user meta field hasn't been filled
        if ( in_array( 'my-category-slug', $categories ) && $metaField != 'yes') {
            //show our checkbox, note value is "yes" like we checked for above
            echo '<input type="checkbox" id="myCheckbox" name="myCheckbox" value="yes"></input>';
            //add action to update user meta once add to cart is clicked 
            add_action('woocommerce_add_to_cart', 'checked_meta_add_to_cart');

        }
    }
}
add_action('woocommerce_after_add_to_cart_form', 'custom_single_checkbox', 25);

Displaying the checkbox works fine, but the user meta field is not updated after the add_to_cart process. At fist I thought it was because I was using woocommerce_after_add_to_cart_form to display the checkbox. However, after trying other hooks inside the add_to_cart_form, it still doesnt update the meta field.

显示该复选框工作正常,但在add_to_cart进程后不会更新用户元字段。我认为这是因为我使用woocommerce_after_add_to_cart_form来显示复选框。但是,在add_to_cart_form中尝试其他挂钩后,它仍然不会更新元字段。

1 个解决方案

#1


2  

Here is the correct way to make it work, simplifying a bit your code and changing the hook:

以下是使其正常工作的正确方法,简化了一些代码并更改了钩子:

//function to display checkbox
add_action('woocommerce_after_add_to_cart_button', 'custom_single_checkbox', 25);
function custom_single_checkbox(){
    if(is_user_logged_in() && is_product()){
        global $product;

        //get the custom meta field to test if it's been filled already
        $metaField = get_user_meta( get_current_user_id(), 'myMetaField', true );

        // if the product has the specified category and our custom user meta field hasn't been filled
        if( has_term( 'my-category-slug', 'product_cat', get_the_id() ) && $metaField != 'yes' ) {
            //show our checkbox, note value is "yes" like we checked for above
            echo '<input type="checkbox" id="myCheckbox" name="myCheckbox" value="yes"></input>';
        }
    }
}

// Update user metadata on add to cart
add_filter( 'woocommerce_add_cart_item_data', 'checked_on_add_to_cart', 10, 2 );
function checked_on_add_to_cart( $cart_item_data, $product_id ){
    if( isset($_POST['myCheckbox']) && is_user_logged_in() )
        update_user_meta( get_current_user_id(), 'myMetaField', esc_attr($_POST['myCheckbox']) );

    return $cart_item_data;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

The WordPress conditional function has_term() accepts term ids, term slugs or term names (or an array of values)

WordPress条件函数has_term()接受术语id,术语slug或术语名称(或值数组)

#1


2  

Here is the correct way to make it work, simplifying a bit your code and changing the hook:

以下是使其正常工作的正确方法,简化了一些代码并更改了钩子:

//function to display checkbox
add_action('woocommerce_after_add_to_cart_button', 'custom_single_checkbox', 25);
function custom_single_checkbox(){
    if(is_user_logged_in() && is_product()){
        global $product;

        //get the custom meta field to test if it's been filled already
        $metaField = get_user_meta( get_current_user_id(), 'myMetaField', true );

        // if the product has the specified category and our custom user meta field hasn't been filled
        if( has_term( 'my-category-slug', 'product_cat', get_the_id() ) && $metaField != 'yes' ) {
            //show our checkbox, note value is "yes" like we checked for above
            echo '<input type="checkbox" id="myCheckbox" name="myCheckbox" value="yes"></input>';
        }
    }
}

// Update user metadata on add to cart
add_filter( 'woocommerce_add_cart_item_data', 'checked_on_add_to_cart', 10, 2 );
function checked_on_add_to_cart( $cart_item_data, $product_id ){
    if( isset($_POST['myCheckbox']) && is_user_logged_in() )
        update_user_meta( get_current_user_id(), 'myMetaField', esc_attr($_POST['myCheckbox']) );

    return $cart_item_data;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

The WordPress conditional function has_term() accepts term ids, term slugs or term names (or an array of values)

WordPress条件函数has_term()接受术语id,术语slug或术语名称(或值数组)