【问题标题】:Setting Woocommerce Product Tags and Categories via Custom Frontend Form通过自定义前端表单设置 Woocommerce 产品标签和类别
【发布时间】:2014-06-02 08:58:37
【问题描述】:

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

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

下面是我正在做的事情。谢谢!

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

【问题讨论】:

    标签: php wordpress tags woocommerce categories


    【解决方案1】:

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

    以下是一些示例:

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

    【讨论】:

    • 有没有办法显示出现在帖子编辑屏幕中的标签框,以将其带到我们的自定义插件中,那真的很酷。
    猜你喜欢
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 2017-11-13
    • 2016-12-26
    • 1970-01-01
    • 2014-09-13
    相关资源
    最近更新 更多