我希望有人能帮助我,我对此真的很陌生。 我想修改产品价格在我的 woocommerce 网站上的显示方式。具体来说,我想定位没有库存的商品,而不是显示价格,而是显示 最后以($Price)出票于(年) 我已将年份作为属性添加到我的产品中(每个产品只有一年)。它被称为年。
我一直在弄乱这个片段,但无法让它工作,主要是因为我不知道我在做什么。 (此代码是我在网络其他地方找到的内容的修改)
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;
}
有人可以帮我纠正这个问题吗?
我尝试了该代码,期望文本“Last Ticketed at $1000 in 2017”出现在缺货商品旁边。但是,年份字段不显示。
代码位于主题或子主题的functions.php中。
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;
}