【问题标题】:Get woocommerce category by name and assign to product按名称获取 woocommerce 类别并分配给产品
【发布时间】:2017-05-13 19:36:11
【问题描述】:

您好,我已经搜索了一段时间,但我无法让它工作,我无法按名称获取类别,也无法以编程方式将其分配给产品

$product = new WC_Product(4);
write_log($product->get_title());

// Gets term object from Tree in the database. 
$category = get_term_by( 'name', 'bolsas', 'product_cat' );
echo "Cat: ".$category->name;

wp_set_object_terms($product->id, $cat->term_id, 'product_cat');

我在functions.php文件上运行它并重新加载页面,我还有一个登录控制台进行调试的功能,但它让我无处可去。

请帮忙!

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    试试这个

    $tag = '5'; // Wrong. This will add the tag with the *name* '5'.
    $tag = 5; // Wrong. This will also add the tag with the name '5'.
    $tag = array( '5' ); // Wrong. Again, this will be interpreted as a term name rather than an id.
    
    $tag = array( 5 ); // Correct. This will add the tag with the id 5.
    wp_set_post_terms( $post_id, $tag, $taxonomy );
    

    【讨论】:

      【解决方案2】:

      这很有帮助,因为在尚未加载的主题 functions.php woocommerce 分类中,它必须与 init 挂钩。

      https://wordpress.stackexchange.com/questions/27111/get-term-by-not-working-when-in-functions-php

      【讨论】:

        猜你喜欢
        • 2021-01-12
        • 2022-07-08
        • 1970-01-01
        • 1970-01-01
        • 2018-12-10
        • 2016-01-09
        • 2014-06-27
        • 1970-01-01
        • 2018-07-09
        相关资源
        最近更新 更多