skip to Main Content

I have the following code in my child theme’s functions.php file, and it works great.

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

However, now I need to add another group of products and enable different text after the price, i.e., Each/Min 8. I have tried various changes to the above and have brought the site down each time. How do I adjust the above to add another (and then possibly another) group of product IDs with different text after the price? Thanks in advance!

2

Answers


  1. You could leave the existing array as it is. Simplify the code by removing the else from the if and then start declaring & checking new product arrays before returning the default price-only text…

    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' );
    
    Login or Signup to reply.
  2. This answer is for the question which was asked here: Add custom text after the price in WooCommerce

    However, it can also be used in the above question.

    You can try something like that if you want dynamic text after every product.

    /* add custom field in single product */
    
    add_action('woocommerce_product_options_general_product_data', function() {
        woocommerce_wp_text_input([
                'id' => '_custom_text_after_price',
                'label' => __('Custom Text after Price', 'themedomain'),
        ]);
    });
    

    You can see the above changes in your product.

    enter image description here

    Now add the below code to save meta in your product

    /* save meta field */
    add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
    function woocommerce_product_custom_fields_save($post_id)
    {
        $woocommerce_custom_product_text_field = $_POST['_custom_text_after_price'];
        if (!empty($woocommerce_custom_product_text_field))
            update_post_meta($post_id, '_custom_text_after_price', esc_attr($woocommerce_custom_product_text_field));
    }
    

    And now to display custom code on the front end you need to use 2 hooks.

    /* show custom text on front end */
    function cw_change_product_price_display( $price ) {
        global $post, $product;
        if(get_post_meta( $post->ID, '_custom_text_after_price', true )){
            $text = get_post_meta( $post->ID, '_custom_text_after_price', true );
        }
        $price .= ' '.$text;
        return $price;
    }
    add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
    add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
    

    In the front end, it will look like this.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search