php - 向 WooCommerce 产品变体添加后缀会被覆盖

标签 php wordpress woocommerce custom-taxonomy product-variations

我开始期待 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)了):

enter image description here

关于php - 向 WooCommerce 产品变体添加后缀会被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55149361/

相关文章:

php - 根据类别名称 woocommerce 检查购物车中的产品?

php - 在 Centos 7 上为 Laravel 5.6 安装 PHP 7.2

php - 使用 wordpress 多站点意外卸载了 AWS EC2 上的 mysql。需要一点指导

php - 使用 JQuery 获取 WooCommerce 购物篮的内容

wordpress - 德语特殊字符在 WordPress 架构中不起作用

php - Wordpress 主题错误 "Fatal error: Cannot re-assign auto-global variable _POST"

php - 使用变量和分页计算行数?

php - 设置和使用 php-resque 的正确方法是什么?

php - PDO 回滚不起作用

css - 从 wordpress 插件自定义生成的 css