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

时间:2018-08-13 20:23:33

标签: php wordpress woocommerce cart product

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

这里是一个简短的示例:

//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);

显示该复选框可以正常工作,但是在add_to_cart进程之后,用户元字段未更新。一开始我以为是因为我使用woocommerce_after_add_to_cart_form来显示复选框。但是,在尝试在add_to_cart_form内的其他挂钩之后,它仍然不会更新meta字段。

1 个答案:

答案 0 :(得分:2)

这是使其正常工作的正确方法,从而简化了代码并更改了钩子:

//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;
}

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

  

WordPress条件函数has_term()接受术语ID,术语段或术语名称​​(或值的数组)