【问题标题】:Adding postfix to WooCommerce product variation gets overwritten向 WooCommerce 产品变体添加后缀会被覆盖
【发布时间】:2019-08-04 13:42:29
【问题描述】:

我开始期待 WooCommerce 中内置了一些“更新功能”,它只能让我暂时重命名变体 post_title。然后它又回到了钩子/WooCommerce 的决定?

我想以编程方式将“(取消)”之类的后缀添加到特定变体中。

$new_title = get_the_title( $variationid ) . ' (Cancelled)';
wp_update_post(array('ID' =>$variationid, 'post_title' => $new_title));

这只会“挂”一会儿......

我尝试禁用此挂钩,然后更改标题,但仍然被覆盖。

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;
    }
}

【问题讨论】:

    标签: php wordpress woocommerce custom-taxonomy variations


    【解决方案1】:

    您应该尝试使用专用的woocommerce_product_variation_title 过滤钩子,它允许临时更改产品变体标题(有条件地),这样:

    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 文件中。它应该可以工作。

    注意:$variation_title返回的产品标题包括产品属性,在上面的功能码中是禁用的……


    在订单编辑页面(也有体现)

    【讨论】:

    • 不错!我做了一些小改动来使用我的自定义帖子状态。虽然我有你在这里 Loic,但我如何在编辑顺序页面中反映这种变化?
    • @CarlF.Corneil 您可以添加它,因为您的问题中的编辑可能是……另一个答案作者刚刚在他的答案中添加了我的代码,这真的不是这样。
    • @CarlF.Corneil 您需要定位订单编辑页面...或者我的代码不会更改订单编辑页面中产品变体的订单项目标题?
    • 好的,感谢您到目前为止的帮助,我会调查一下。
    • 我想我是用你的一些其他答案 Loic 弄明白的。结合我已经编写的一些函数。 foreach ( $order->get_items() as $item_id => $item ){ $item->set_name($item_name . ' (Cancelled)'); $item->save();} 在 save() 部分有点挣扎...
    【解决方案2】:

    WooCommerce 中的代码部分及其过滤器。

            $should_include_attributes = apply_filters( 'woocommerce_product_variation_title_include_attributes', $should_include_attributes, $product );
            $separator                 = apply_filters( 'woocommerce_product_variation_title_attributes_separator', ' - ', $product );
            $title_base                = get_post_field( 'post_title', $product->get_parent_id() );
            $title_suffix              = $should_include_attributes ? wc_get_formatted_variation( $product, true, false ) : '';
    
            return apply_filters( 'woocommerce_product_variation_title', $title_suffix ? $title_base . $separator . $title_suffix : $title_base, $product, $title_base, $title_suffix );
    
    
    
    
    add_filter('woocommerce_product_variation_title', 'change_variation_title_temporary');
    
    function change_variation_title_temporary($variation_title, $product, $title_base, $title_suffix) {
    
        return $title_base .  ' (Cancelled)';
    
    } 
    

    【讨论】:

      猜你喜欢
      • 2015-07-12
      • 2019-07-29
      • 2020-09-13
      • 2021-10-20
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-19
      相关资源
      最近更新 更多