skip to Main Content

I hope someone can help me, I’m REALLY new at this.
I want to modify how prices for products are displayed on my woocommerce site. Specifically I want to target items that are not in stock and instead of displaying the price, display
Last ticketed at ($Price) in (Year)
I have added the Year to my products as an attribute (there is only one year per product). It is called Year.

I have been messing with this snippet and cant get it to work, mainly because I have no idea what I am doing. (This code is a modification of things I have found elsewhere on the web)

add_filter( 'woocommerce_get_price_html', 'modify_price_out_of_stock_items', 9999, 2 );

function modify_price_out_of_stock_items( $price, $product ) {
global $product;
$year = $product->get_attribute('pa_Year');

if ( is_admin() ) return $price; // BAIL IF BACKEND
if ( ! $product->is_in_stock() ) {
$year = wc_get_product_terms( $product->get_id(), 'pa_Year', array( 'fields' => 'slugs' ) ) ;
$price = 'Last ticketed at ' . $price . ' in ' . $year[0];
}
return $price;
}

Can someone help me correct this?

I tried that code expecting the text ‘Last ticketed at $1000 in 2017’ to appear next to out of stock items. However the Year field does not display.

2

Answers


  1. Code goes in functions.php of theme or child theme.

      add_filter( 'woocommerce_get_price_html', 'modify_price_out_of_stock_items', 9999, 2 );
    
    function modify_price_out_of_stock_items( $price, $product ) {
        // Check if the product is in stock
        if ( ! $product->is_in_stock() ) {
            // Get the 'Year' attribute value
            $year = $product->get_attribute( 'Year' );
    
            // If the 'Year' attribute is set for the product
            if ( $year ) {
                // Format the price and year information
                $price = 'Last ticketed at ' . wc_price( $product->get_regular_price() ) . ' in ' . $year;
            } else {
                // If the 'Year' attribute is not set, display a generic message
                $price = 'Last ticketed at ' . wc_price( $product->get_regular_price() ) . ' in Year N/A';
            }
        }
        return $price;
    }
    
    Login or Signup to reply.
  2. The following will replace the formatted displayed price, for out of stock products, with a custom text including the price and the year:

    add_filter( 'woocommerce_get_price_html', 'custom_displayed_out_of_stock', 10, 2 );
    function modify_price_out_of_stock_items( $price_html, $product ) {
        if ( ! is_admin() && ! $product->is_in_stock() ) {
            return sprintf( __('Last ticketed at %s in %s', 'woocommerce'), 
                wc_price( wc_get_price_to_display( $product ) ), 
                $product->get_attribute('Year') 
            );
        }
        return $price_html;
    }
    

    Code goes in functions.php file of your active child theme (or in a plugin).
    It should work.

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