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
Code goes in functions.php of theme or child theme.
The following will replace the formatted displayed price, for out of stock products, with a custom text including the price and the year:
Code goes in functions.php file of your active child theme (or in a plugin).
It should work.