从 WooCommerce 中的自定义字段更新 add_to_cart 上的用户元数据
Update user meta on add_to_cart from a custom field in WooCommerce
我在属于特定类别的单个产品页面上显示一个复选框。用户必须在将产品添加到购物车之前选中此复选框。一旦选中并且用户将产品添加到购物车,我想使用此复选框的值更新自定义用户元字段。
这里是一个简短的例子:
//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 中尝试了其他钩子之后,它仍然没有更新元字段。
这里是让它工作的正确方法,稍微简化了你的代码并更改了钩子:
//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 文件。测试和工作。
The WordPress conditional function has_term()
accepts term ids, term slugs or term names (or an array of values)
我在属于特定类别的单个产品页面上显示一个复选框。用户必须在将产品添加到购物车之前选中此复选框。一旦选中并且用户将产品添加到购物车,我想使用此复选框的值更新自定义用户元字段。
这里是一个简短的例子:
//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 中尝试了其他钩子之后,它仍然没有更新元字段。
这里是让它工作的正确方法,稍微简化了你的代码并更改了钩子:
//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 文件。测试和工作。
The WordPress conditional function
has_term()
accepts term ids, term slugs or term names (or an array of values)