将产品类别保存到woocommerce帐户选项卡中的用户配置文件
我目前正在一个woocommerce网站上工作,我需要将我的产品类别保存到用户的个人资料中,我遇到了如何保存的问题。我已经成功地提取了类别列表,并将其显示为一个表单,其中类别显示在复选框列表中。当我保存表单时,数据库会更新,但我必须刷新页面,表单才能显示更新。此外,它不允许我删除所有选定的项目,一旦一个保存到配置文件将产品类别保存到woocommerce帐户选项卡中的用户配置文件,woocommerce,ultimate-member,Woocommerce,Ultimate Member,我目前正在一个woocommerce网站上工作,我需要将我的产品类别保存到用户的个人资料中,我遇到了如何保存的问题。我已经成功地提取了类别列表,并将其显示为一个表单,其中类别显示在复选框列表中。当我保存表单时,数据库会更新,但我必须刷新页面,表单才能显示更新。此外,它不允许我删除所有选定的项目,一旦一个保存到配置文件 如何使表单在保存表单后立即显示更新的内容 如何允许用户取消选中所有类别 我对wordpress/woocommerce开发相当陌生,所以我不确定我缺少了什么。任何帮助都将不胜感激
//将选项卡添加到我的帐户页面
功能foobar\添加\类别\链接\我的\帐户($items){
$items['account-categories']='account-categories';
退回$items;
}
添加过滤器('woocommerce\u account\u menu\u items'、'foobar\u add\u categories\u link\u my\u account');
//帐户类别选项卡内容
函数foobar\u帐户\u类别\u内容(){
$user\u id=get\u current\u user\u id();
$data=获取作者元('user\u categories',$user\u id);
$args=数组('hide_empty'=>0);
$categories=get_术语('product_cat',$args);
$nocat_msg=\uuuuu('您当前未选择任何类别');
如果(!空($\u POST['user\u categories'])){
更新_user_meta($user_id,'user_categories',$_POST['user_categories');
}
if(空($数据)){
回音“”.$nocat_msg.”;
}
echo“您的团队”;
如果($类别){
foreach($categories作为$category){
if(在数组中($category->term\u id,(数组)$data)){
$selected='checked=“checked”“;
}否则{
$selected='';
}
echo'- 。$category->name.
;
}
}
回显“
保存更改”;
}
添加_操作('woocommerce_account_account-categories_endpoint'、'foobar_account_categories_content');
这是帮助我展示产品类别的第一篇文章-
//Add tab to my account page
function foobar_add_categories_link_my_account( $items ) {
$items['account-categories'] = 'Account Categories';
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'foobar_add_categories_link_my_account');
//Account Categories tab content
function foobar_account_category_content() {
$user_id = get_current_user_id();
$data = get_the_author_meta( 'user_categories', $user_id );
$args = array( 'hide_empty' =>0 );
$categories = get_terms( 'product_cat', $args );
$nocat_msg = __('You currently have no categories selected');
if ( !empty( $_POST['user_categories'] ) ){
update_user_meta( $user_id, 'user_categories', $_POST['user_categories'] );
}
if ( empty($data) ) {
echo '<p>'. $nocat_msg .'</p>';
}
echo '<form class="woocommerce-EditAccountCategories edit-account" action="" method="post"><label for="user_categories">Your Teams</label> <ul class="no-bullets account-category-list">';
if ($categories){
foreach ( $categories as $category ){
if(in_array($category->term_id,(array)$data)) {
$selected = 'checked="checked""';
} else {
$selected = '';
}
echo '<li class="user-category"><input name="user_categories[]" value="'.$category->term_id.'" '.$selected.' type="checkbox"/>'.$category->name.'</li>';
}
}
echo '</ul> <button type="submit" class="woocommerce-Button button" name="save_account_categories" value="Save changes">Save changes</button><input type="hidden" name="action" value="save_account_categories"></form>';
}
add_action( 'woocommerce_account_account-categories_endpoint', 'foobar_account_category_content' );