将后缀添加到WooCommerce产品变体会被覆盖

问题描述 投票:4回答:2

我开始期待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
2个回答
3
投票

您应该尝试使用专用的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返回产品标题,包括产品属性,在上面的功能代码中禁用了...


在订单编辑页面上(也反映出来):

enter image description here


0
投票

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)';

} 
© www.soinside.com 2019 - 2024. All rights reserved.