Woocommerce 变体未过滤掉无效组合

Woocommerce variations not filtering out invalid combinations

将 Woocommerce 更新到 2.6.12 后,我的可变产品在前端运行。例如,我有一个具有尺寸和颜色属性的可变产品。并非所有尺寸都提供所有颜色。在更新之前,Woocommerce 将使用 AJAX 过滤掉下一个下拉列表中不兼容的选项,以防止选择不存在的产品。现在它显示所有选项,无论它们是否产生有效产品。选择无效产品会导致弹出警告框:

Sorry, no products matching your selection. Please choose a different combination.

如何让 Woocommerce 再次过滤掉无效的属性选择?

我在查看 Woocommerce 的 github 回购已关闭问题时找到了修复程序。 This user had the same issue. The fix can be found here。我下载了 zip 文件中包含的修复程序,并用它来替换 /wp-content/plugins/woocommerce/assets/js/frontend/ 中找到的文件。刷新页面后,无效的选择被过滤掉,一切恢复正常。

这可能是由于 woocommerce_ajax_variation_threshold。如果您的产品包含的变体多于指定的阈值,woocommerce 将等到用户选择了所有变体选项,然后再确定所选的选项组合是否有效(有货、存在等)。

只需将阈值设置为更高的值即可。如果您的产品有 30 种可能的组合(无论它们是否实际存在),请将阈值设置为高于 30,例如 1111。

您可以在 functions.php 文件中使用下面的代码段。

/* Increase Woocommerce Variation Threshold */
function wc_ajax_variation_threshold_modify( $threshold, $product ){
  $threshold = '1111';
  return  $threshold;
}
add_filter( 'woocommerce_ajax_variation_threshold', 'wc_ajax_variation_threshold_modify', 10, 2 );