Php 加上“;销售「;如果销售价格因商业变化而填写,则为徽章
在WooCommerce中,对于一个简单的产品,我可以通过以下方式添加“销售!”徽章:Php 加上“;销售「;如果销售价格因商业变化而填写,则为徽章,php,wordpress,woocommerce,product,price,Php,Wordpress,Woocommerce,Product,Price,在WooCommerce中,对于一个简单的产品,我可以通过以下方式添加“销售!”徽章: add_action( 'woocommerce_before_shop_loop_item_title', function() { global $product; $akc = get_post_meta(get_the_ID(), '_sale_price', true); if ( $akc > 0 ) { echo '<span class="
add_action( 'woocommerce_before_shop_loop_item_title', function() {
global $product;
$akc = get_post_meta(get_the_ID(), '_sale_price', true);
if ( $akc > 0 ) {
echo '<span class="onsale soldout">Sale!</span>';
}
});
add_action( 'woocommerce_before_shop_loop_item_title', 'custom_shop_loop_on_sale_badge' );
function custom_shop_loop_on_sale_badge(){
global $product;
if($product->is_on_sale())
echo '<span class="onsale soldout">Sale!</span>';
}
但在此之后,“销售!”不再分配(仅适用于可变产品)要使您的代码适用于所有产品类型,您应该使用
WC\u Product
方法。通过这种方式,所有产品类型只有一个功能:
add_action( 'woocommerce_before_shop_loop_item_title', function() {
global $product;
$akc = get_post_meta(get_the_ID(), '_sale_price', true);
if ( $akc > 0 ) {
echo '<span class="onsale soldout">Sale!</span>';
}
});
add_action( 'woocommerce_before_shop_loop_item_title', 'custom_shop_loop_on_sale_badge' );
function custom_shop_loop_on_sale_badge(){
global $product;
if($product->is_on_sale())
echo '<span class="onsale soldout">Sale!</span>';
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中
经过测试,效果良好。非常感谢您的回答!但我被重新定义为产品的价格。让我在更新问题中向您展示。@user我已经更新了关于您更新问题的代码。哦,不,浏览器发出了“ERR\u CONNECTION\u RESET”,因为
函数自定义变量\u获取价格($price,$variation){
可能是什么?@user271244我真的不知道……对不起
// Prices calculation common function
function get_rrp_price( $price, $product_id ) {
$rooms2 = get_post_meta( $product_id, 'rooms2', true );
if ( '' === $rooms2 )
$rooms2 = 1;
if ( $rooms2 == 1 )
$price *= 10;
elseif ( $rooms2 == 2 )
$price *= 100;
return $price;
}
// Simple products prices
add_filter( 'woocommerce_product_get_price', 'custom_simple_product_price', 20, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'custom_simple_product_price', 20, 2 );
function custom_simple_product_price( $price, $product ) {
$price = get_rrp_price( $price, $product->get_id() );
update_post_meta( $product->get_id(), 'rrp_price', $price );
return $price;
}
// Variable products: Selected variation prices
add_filter( 'woocommerce_variation_prices_price', 'custom_variations_prices', 10, 3 );
add_filter( 'woocommerce_variation_prices_regular_price', 'custom_variations_prices', 10, 3 );
add_filter( 'woocommerce_variation_prices_sale_price', 'custom_variations_prices', 10, 3 );
function custom_variations_prices( $price, $variation, $product ) {
if ( $variation->is_type('variation') ) {
$product_id = $product->get_id();
$price = get_rrp_price( $price, $product_id );
}
return $price;
}
// Variable products: Min/Max variations prices
add_filter( 'woocommerce_product_variation_get_regular_price', 'custom_variations_get_prices' , 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_variations_get_prices' , 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'custom_variations_get_prices', 10, 2 );
function custom_variations_get_prices( $price, $variation ) {
if ( $variation->is_type('variation') ) {
$parent_product_id = $variation->get_parent_id();
$rrp_price = get_rrp_price( $price, $product_id );
// Adding/Updating "rrp regular price" custom field
$regular_price = $variation->get_regular_price();
if( $regular_price == $price )
update_post_meta( $product_id, $variation->get_name() . ' - regular' , $rrp_price );
$price = $rrp_price;
}
return $price;
}