php - 更新 Woocommerce 3 中的产品可见性

标签 php wordpress methods woocommerce product

我在 woocommerce/wordpress 中使用了几个插件来实现它,这样我的每个产品变体都会作为单独的产品显示在我的产品列表中,但是每种颜色中只有一种设置为显示(所以如果我有一个产品有 3 种尺寸和 3 种颜色,共有 9 种产品,但由于每种颜色只有一种可见,因此列表中只显示 3 种产品。)。我写了一个脚本,一旦当前可见的产品缺货,它就会将可见性设置转移到下一个相同颜色的产品。

我遇到的问题是,即使一切看起来都正确,产品也不会显示在产品列表中,直到我进入管理端,编辑产品,将可见性设置更改为隐藏,然后再返回显示(所以它让我点击保存按钮,虽然页面加载时实际上没有任何变化),然后点击保存并显示它应该的样子。

所以我一定是在数据库中遗漏了一些被查询的东西,但我不知道是什么,posts 和 post_meta 表在管理员端点击保存之前和之后看起来是一样的。唯一更改的字段是 post_meta 表中的 _edit_lock 字段。

这是我为传输可见性设置而编写的脚本的一部分,我最初有这个问题,发现这是因为产品仍然被标记为缺货,所以我在最后添加了那行,它似乎工作正常,但现在是其他原因造成的:

    ...
    // $child_id is the product that the visibility settings are being transferred to, it should at this point be hidden
    // $visibility is the settings of the product that was visibile


    // swap visibility settings of the products
    $child_visibility = get_post_meta($child_id,"_visibility",true);
    update_post_meta($product->get_id(),"_visibility",$child_visibility);
    update_post_meta($child_id,"_visibility",$visibility);


    // copy color taxonomies over in case they were not entered 
    // this saved time so that the admin only had to enter taxonomy information on the visible products
    $terms = get_the_terms( $product->get_id(), 'product_color');
    $termArray = array();
    foreach($terms as $term) {
        $termArray[] = $term->name;
    }
    wp_set_object_terms( $child_id, $termArray, 'product_color', false );

    // i dont remember what this was for
    delete_transient( 'jck_wssv_term_counts' );

    // make sure new item is not marked out of stock
    wp_remove_object_terms( $child_id, 'outofstock', 'product_visibility' );
    wp_remove_object_terms( $child_id, 'exclude-from-catalog', 'product_visibility' );

最佳答案

从 Woocommerce 3 开始,产品可见性现在由术语 'exclude-from-catalog''exclude- 的 'product_visibility' 自定义分类法处理来自搜索'…参见this threadthis one也是。

所以你应该改用WC_Product CRUD setter methods set_catalog_visibility()这样:

// Get an instance of the product variation from a defined ID
$child_product = wc_get_product(child_id);
// Change the product visibility
$child_product->set_catalog_visibility('visible');
// Save and sync the product visibility
$child_product->save();

这也将更新 transient 缓存数据,并达到目的...

关于php - 更新 Woocommerce 3 中的产品可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50633551/

相关文章:

javascript - 当谷歌地图标记点移动时用经纬度更新数据库

使用 GET 变量重写 WordPress URL

java - 为什么我不能在主类中创建除 main 方法之外的另一个方法?

android - 检查 editText 数组是否为空

php - 有什么方法可以在不提供完整 url(即本地路径)的情况下链接到 CSS 中的背景图片 url?

php - 如何使用正则 exp 从段落中选择电话号码

javascript - jquery:SyntaxError: 未终止的字符串文字

JavaScript onClick 仅在第二次点击后起作用

php - 在 WP 上添加自定义 'order by date' 按钮

ios - Obj-C block 作为参数