【发布时间】:2018-11-11 00:18:38
【问题描述】:
我正在使用 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' );
【问题讨论】:
标签: php wordpress methods woocommerce product