我开始期待 WooCommerce 中内置了一些“更新功能”,它只允许我重命名变体 post_title 一段时间。然后它又回到钩子(Hook)/WooCommerce 决定的状态?
我想以编程方式向特定变体添加诸如“(Cancelled)”之类的后缀。
$new_title = get_the_title( $variationid ) . ' (Cancelled)';
wp_update_post(array('ID' =>$variationid, 'post_title' => $new_title));
这只会“挂起”一小会儿......
我尝试禁用这个钩子(Hook),然后更改标题,但它仍然被覆盖。
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
有什么方法可以让 WooCommerce 停止覆盖变体的标题?
我的解决方案基于@LoicTheAztec 的回答,使用基于我的自定义帖子状态“已取消”的逻辑。
add_filter( 'woocommerce_product_variation_title', 'filter_product_variation_title_callback', 10, 4 );
function filter_product_variation_title_callback( $variation_title, $product, $title_base, $title_suffix ) {
$id = $product->get_id();
$status = get_post_status($id);
if ($status == 'cancelled'){
return $variation_title . ' (' . __("Cancelled", "woocommerce") . ')';
} else {
return $variation_title;
}
}
最佳答案
您应该尝试使用专用的 woocommerce_product_variation_title
过滤器 Hook ,它允许暂时更改产品变体标题(有条件地),这样:
add_filter( 'woocommerce_product_variation_title', 'filter_product_variation_title_callback', 10, 4 );
function filter_product_variation_title_callback( $variation_title, $product, $title_base, $title_suffix ) {
// Conditional custom field (example)
if( $product->get_meta('_is_cancelled') )
$title_base .= ' (' . __("cancelled", "woocommerce") . ')';
return $title_base
}
代码进入事件子主题(或事件主题)的 function.php 文件。它应该有效。
Note: The
$variation_title
returns the product title including product attributes, that is disabled in the function code above…
在订单编辑页面上(也反射(reflect)了):
关于php - 向 WooCommerce 产品变体添加后缀会被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55149361/