如果 WooCommerce product/archive 页面中的价格高于 50 美元,则显示免费送货徽章
Show free delivery badge if price is above 50$ in WooCommerce product/archive page
我想在每个价格超过 50 美元的产品上展示一个 "free delivery" 徽章。
它应该在产品页面和循环中可见。
问题是,价格可能不止一个。如果您考虑变化和销售(甚至是销售变体的变化)。
所以我需要检查产品的类型,并且必须搜索最便宜的价格来计算。
目前我正在使用以下代码。
有时它工作正常。但是对于没有有效库存管理的产品,它会在产品页面上产生超时并且对存档不起作用(不显示任何消息)。
它还会产生一些关于不直接使用 ID 的通知。
我觉得那个代码不安全...有没有更好的方法来实现它?
我尝试了很多方法,但我不确定我是否考虑过关于价格、销量、库存或产品类型的每一种可能性?!
<?php add_action( 'wgm_after_tax_display_single', 'wgm_after_tax_display_single_free_delivery', 10, 1 );
function wgm_after_tax_display_single_free_delivery( ) {
if (is_product()):
global $post, $product;
if ( ! $product->is_in_stock() ) return;
$sale_price = get_post_meta( $product->id, '_price', true);
$regular_price = get_post_meta( $product->id, '_regular_price', true);
if (empty($regular_price)){ //then this is a variable product
$available_variations = $product->get_available_variations();
$variation_id=$available_variations[0]['variation_id'];
$variation= new WC_Product_Variation( $variation_id );
$regular_price = $variation ->regular_price;
$sale_price = $variation ->sale_price;
}
if ( $sale_price >= 50 && !empty( $regular_price ) ) :
echo 'free delivery!';
else :
echo 'NO free delivery!';
endif;
endif;
} ?>
由于您使用的是自定义挂钩,因此很难对其进行真实测试(与您的方式相同)……现在重新访问的代码应该比您的代码工作得更好(解决错误通知):
add_action( 'wgm_after_tax_display_single', 'wgm_after_tax_display_single_free_delivery', 10, 1 );
function wgm_after_tax_display_single_free_delivery() {
// On single product pages and archives pages
if ( is_product() || is_shop() || is_product_category() || is_product_tag() ):
global $post, $product;
if ( ! $product->is_in_stock() ) return;
// variable products (get min prices)
if ( $product->is_type('variable') ) {
$sale_price = $product->get_variation_sale_price('min');
$regular_price = $product->get_variation_regular_price('min');
}
// Other products types
else {
$sale_price = $product->get_sale_price();
$regular_price = $product->get_regular_price();
}
$price = $sale_price > 0 ? $sale_price : $regular_price;
if ( $price >= 50 ) {
echo __('free delivery!');
} else {
echo __('NO free delivery!');
}
endif;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该会更好用。
我想在每个价格超过 50 美元的产品上展示一个 "free delivery" 徽章。 它应该在产品页面和循环中可见。
问题是,价格可能不止一个。如果您考虑变化和销售(甚至是销售变体的变化)。 所以我需要检查产品的类型,并且必须搜索最便宜的价格来计算。
目前我正在使用以下代码。 有时它工作正常。但是对于没有有效库存管理的产品,它会在产品页面上产生超时并且对存档不起作用(不显示任何消息)。 它还会产生一些关于不直接使用 ID 的通知。
我觉得那个代码不安全...有没有更好的方法来实现它? 我尝试了很多方法,但我不确定我是否考虑过关于价格、销量、库存或产品类型的每一种可能性?!
<?php add_action( 'wgm_after_tax_display_single', 'wgm_after_tax_display_single_free_delivery', 10, 1 );
function wgm_after_tax_display_single_free_delivery( ) {
if (is_product()):
global $post, $product;
if ( ! $product->is_in_stock() ) return;
$sale_price = get_post_meta( $product->id, '_price', true);
$regular_price = get_post_meta( $product->id, '_regular_price', true);
if (empty($regular_price)){ //then this is a variable product
$available_variations = $product->get_available_variations();
$variation_id=$available_variations[0]['variation_id'];
$variation= new WC_Product_Variation( $variation_id );
$regular_price = $variation ->regular_price;
$sale_price = $variation ->sale_price;
}
if ( $sale_price >= 50 && !empty( $regular_price ) ) :
echo 'free delivery!';
else :
echo 'NO free delivery!';
endif;
endif;
} ?>
由于您使用的是自定义挂钩,因此很难对其进行真实测试(与您的方式相同)……现在重新访问的代码应该比您的代码工作得更好(解决错误通知):
add_action( 'wgm_after_tax_display_single', 'wgm_after_tax_display_single_free_delivery', 10, 1 );
function wgm_after_tax_display_single_free_delivery() {
// On single product pages and archives pages
if ( is_product() || is_shop() || is_product_category() || is_product_tag() ):
global $post, $product;
if ( ! $product->is_in_stock() ) return;
// variable products (get min prices)
if ( $product->is_type('variable') ) {
$sale_price = $product->get_variation_sale_price('min');
$regular_price = $product->get_variation_regular_price('min');
}
// Other products types
else {
$sale_price = $product->get_sale_price();
$regular_price = $product->get_regular_price();
}
$price = $sale_price > 0 ? $sale_price : $regular_price;
if ( $price >= 50 ) {
echo __('free delivery!');
} else {
echo __('NO free delivery!');
}
endif;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该会更好用。