Php 根据产品ID/WP在WooCommerce产品中的价格后添加文本

Php 根据产品ID/WP在WooCommerce产品中的价格后添加文本,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我在我的子主题的functions.php文件中有以下代码,它非常有效 function themeprefix_custom_price_message( $price ) { global $post; $product_id = $post->ID; $my_product_array = array( 270,373,378,506,1306,1311,1312,1444,1445,1447,1449,1930,1932,1933,1934,1935,1963,4146,41

我在我的子主题的functions.php文件中有以下代码,它非常有效

function themeprefix_custom_price_message( $price ) { 

global $post;

$product_id = $post->ID;
$my_product_array = array( 270,373,378,506,1306,1311,1312,1444,1445,1447,1449,1930,1932,1933,1934,1935,1963,4146,4152,4153,4154 );//add in product IDs
if ( in_array( $product_id, $my_product_array )) {
    $textafter = ' Each/Min 12'; //add your text
    return $price . '<span class="price-description">' . $textafter . '</span>';
 } 
 else 
 { 
    return $price; 
 } 
}
add_filter( 'woocommerce_get_price_html', 'themeprefix_custom_price_message' );
函数themeprefix\u custom\u price\u消息($price){
全球$员额;
$product\U id=$post->id;
$my_product_array=array(27037337850611303111131214441445144714491930193219331934193519634146415415415414);//加载项产品ID
if(在数组中($product\U id,$my\U product\U数组)){
$textfeater='Each/min12';//添加文本
返回$price.'.$textafter.';
} 
其他的
{ 
返回$price;
} 
}
添加过滤器(“woocommerce\u get\u price\u html”、“themeprefix\u custom\u price\u message”);

但是,现在我需要添加另一组产品,并在价格之后启用不同的文本,即每个/Min 8。我尝试过对上面的内容进行各种更改,每次都关闭了网站。我如何调整以上内容以在价格之后添加另一组(然后可能是另一组)具有不同文本的产品ID?提前谢谢

可以保留现有阵列的原样。通过从
if
中删除
else
来简化代码,然后在返回默认的纯价格文本之前开始声明和检查新产品数组

function themeprefix_custom_price_message( $price ) { 

    global $post;

    $product_id = $post->ID;

    $my_product_array = array( 270,373,378,506,1306,1311,1312,1444,1445,1447,1449,1930,1932,1933,1934,1935,1963,4146,4152,4153,4154 );//add in product IDs

    if ( in_array( $product_id, $my_product_array )) {
        $textafter = ' Each/Min 12'; //add your text
        return $price . '<span class="price-description">' . $textafter . '</span>';
    } 

    $my_product_array2 = array( 9990,9991,9992 );

    if ( in_array( $product_id, $my_product_array2 )) {
        $textafter = ' Each/Min 8'; //add your text
        return $price . '<span class="price-description">' . $textafter . '</span>';
    }

    $my_product_array3 = array( 9993,9994,9995 );

    if ( in_array( $product_id, $my_product_array3 )) {
        $textafter = ' Each/Min 4'; //add your text
        return $price . '<span class="price-description">' . $textafter . '</span>';
    }

    return $price;  
}
add_filter( 'woocommerce_get_price_html', 'themeprefix_custom_price_message' );
函数themeprefix\u custom\u price\u消息($price){
全球$员额;
$product\U id=$post->id;
$my_product_array=array(27037337850611303111131214441445144714491930193219331934193519634146415415415414);//加载项产品ID
if(在数组中($product\U id,$my\U product\U数组)){
$textfeater='Each/min12';//添加文本
返回$price.'.$textafter.';
} 
$my_product_array2=阵列(999099992);
if(在数组中($product\U id,$my\U product\U array2)){
$textfeater='Each/min8';//添加文本
返回$price.'.$textafter.';
}
$my_product_array3=阵列(99939499995);
if(在数组中($product\U id,$my\U product\U array3)){
$textfeater='Each/Min 4';//添加文本
返回$price.'.$textafter.';
}
返回$price;
}
添加过滤器(“woocommerce\u get\u price\u html”、“themeprefix\u custom\u price\u message”);

您可以保留现有阵列的原样。通过从
if
中删除
else
来简化代码,然后在返回默认的纯价格文本之前开始声明和检查新产品数组

function themeprefix_custom_price_message( $price ) { 

    global $post;

    $product_id = $post->ID;

    $my_product_array = array( 270,373,378,506,1306,1311,1312,1444,1445,1447,1449,1930,1932,1933,1934,1935,1963,4146,4152,4153,4154 );//add in product IDs

    if ( in_array( $product_id, $my_product_array )) {
        $textafter = ' Each/Min 12'; //add your text
        return $price . '<span class="price-description">' . $textafter . '</span>';
    } 

    $my_product_array2 = array( 9990,9991,9992 );

    if ( in_array( $product_id, $my_product_array2 )) {
        $textafter = ' Each/Min 8'; //add your text
        return $price . '<span class="price-description">' . $textafter . '</span>';
    }

    $my_product_array3 = array( 9993,9994,9995 );

    if ( in_array( $product_id, $my_product_array3 )) {
        $textafter = ' Each/Min 4'; //add your text
        return $price . '<span class="price-description">' . $textafter . '</span>';
    }

    return $price;  
}
add_filter( 'woocommerce_get_price_html', 'themeprefix_custom_price_message' );
函数themeprefix\u custom\u price\u消息($price){
全球$员额;
$product\U id=$post->id;
$my_product_array=array(27037337850611303111131214441445144714491930193219331934193519634146415415415414);//加载项产品ID
if(在数组中($product\U id,$my\U product\U数组)){
$textfeater='Each/min12';//添加文本
返回$price.'.$textafter.';
} 
$my_product_array2=阵列(999099992);
if(在数组中($product\U id,$my\U product\U array2)){
$textfeater='Each/min8';//添加文本
返回$price.'.$textafter.';
}
$my_product_array3=阵列(99939499995);
if(在数组中($product\U id,$my\U product\U array3)){
$textfeater='Each/Min 4';//添加文本
返回$price.'.$textafter.';
}
返回$price;
}
添加过滤器(“woocommerce\u get\u price\u html”、“themeprefix\u custom\u price\u message”);

谢谢@davmos,太棒了!我真诚地感谢你的帮助!非常欢迎你,帕特里夏,很高兴听到它起作用:)谢谢你@davmos,这太好了!我真诚地感谢你的帮助!不客气,帕特里夏,很高兴听到它起作用:)