WooCommerce 中特定产品变体的自定义后缀 [英] Custom suffix for specific product variation in WooCommerce

查看:31
本文介绍了WooCommerce 中特定产品变体的自定义后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为可变商品的价格添加后缀.它只需要在选择该特定项目时显示.

I am trying to add a suffix to the price of a variable item. It only has to show when that specific item is selected.

我尝试了 stackoverflow 中的一些不同代码,但所有这些都将后缀添加到所有变量中,而不仅仅是我需要的那个.

I tried some different codes from stackoverflow, but all of the add the suffix to all variables instead of just the one I need.

目前我正在使用以下代码,但它给出了一个严重错误.

Currently I am using following code, but it gives a critical error.

致命错误:未捕获的错误:无法使用 WC_Product_Variation 类型的对象作为数组

Fatal error: Uncaught Error: Cannot use object of type WC_Product_Variation as array

add_filter('woocommerce_available_variation', 'variation_price_custom_suffix', 10, 3 );
function variation_price_custom_suffix( $variation_data, $product, $variation ) {
// For a specific product variation ID
if( $variation['variation_id'] == 9256 ) {

    $variation_data['price_html'] .= ' ' . __("Text after price");
}

return $variation_data;
}

谁能帮帮我?

推荐答案

要在 WooCommerce 中为特定产品变体添加自定义后缀,您可以使用:

To add a custom suffix for a specific product variation in WooCommerce, you can use:

function variation_price_custom_suffix( $variation_data, $product, $variation ) {   
    // For a specific product variation ID
    if ( $variation_data['variation_id'] == 9256 ) {
        $variation_data['price_html'] .= ' ' . __( 'Text after price', 'woocommerce');
    }

    return $variation_data;
}
add_filter( 'woocommerce_available_variation', 'variation_price_custom_suffix', 10, 3 );

这篇关于WooCommerce 中特定产品变体的自定义后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆