通过自定义前端表单设置 Woocommerce 产品标签和类别 [英] Setting Woocommerce Product Tags and Categories via Custom Frontend Form

查看:28
本文介绍了通过自定义前端表单设置 Woocommerce 产品标签和类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个表单,供用户使用 wp_insert_postupdate_post_meta 通过我网站的前端创建产品.

I am building a form for users to create a product via the frontend of my site using wp_insert_post and update_post_meta.

尝试设置产品类别和标签时出现问题.在这方面,Woocommerce 似乎没有使用标准的 Wordpress 分类法.有人对此有经验吗?Woocommerce 似乎在某些地方使用了 product_tags.是否有类似于 Wordpress 的功能来创建它们?

The problem arises when trying to set the product categories and tags. It seems Woocommerce doesn't use standard Wordpress taxonomies in this regard. Anyone have any experience with this? It seems Woocommerce uses product_tags in some places. Is there a function to create them similar to Wordpress?

下面是我正在做的事情的片段.谢谢!

Below is a snippet of what I am doing. Thanks!

$post = array(
 'ID' => '',
 'post_content' => $_POST['post_content'],
 'post_title' => $_POST['post_title'],
 'post_status' => 'draft',
 'post_type' => 'product',
 'post_author' => $user_id,
);

$newListing = wp_insert_post($post, $wp_error);

//SET META
update_post_meta($newListing, '_stock_status', 'instock', true);
update_post_meta($newListing, '_visibility', 'visible', true);
update_post_meta($newListing, '_price', $_POST['_regular_price'], true);

//SET CATEGORIES - **NOT WORKING**
wp_set_post_categories($newListing, $categories);

//SET THE TAGS **NOT WORKING**
wp_set_post_tags($newListing, $tags, true);

推荐答案

发现内置的 Wordpress 函数 wp_set_object_terms 可以很容易地处理这个问题.

Found out the built-in Wordpress function wp_set_object_terms will handle this quite easily.

以下是一些示例:

//SET THE PRODUCT CATEGORIES
wp_set_object_terms($productID, array('Cat Name 1', 'Cat Name 2'), 'product_cat');

//SET THE PRODUCT TAGS
wp_set_object_terms($productID, array('tag1','tag2','tag3'), 'product_tag');

这篇关于通过自定义前端表单设置 Woocommerce 产品标签和类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆