在 WooCommerce 中,我使用以下代码在显示的产品价格周围添加一些文本(“租金:”和“/天”),例如:
function cp_change_product_price_display( $price ) {
echo 'Rent: ' . $price . '/day';
}
add_filter( 'woocommerce_get_price_html', 'cp_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cp_change_product_price_display' );
我还使用插件“YITH WooCommerce Subscription”,但我的代码有问题。现在,在订阅包中,我显示了以下价格之一:
- 租金:20 美元/7 天/天
- 租金:80 美元/月/天。
如何从我的代码中排除订阅产品或订阅类别以避免此问题?
最佳答案
您可以添加条件来检查当前产品是否已订阅。示例。
function cp_change_product_price_display( $price, $instance ) {
if ( 'yes' === $instance->get_meta( '_ywsbs_subscription' ) ) {
return $price;
}
return 'Rent: ' . $price . '/day';
}
add_filter( 'woocommerce_get_price_html', 'cp_change_product_price_display', 10, 2 );
购物车价格:
function cp_change_product_price_display_in_cart( $price, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
if ( 'yes' === $product->get_meta( '_ywsbs_subscription' ) ) {
return $price;
}
return 'Rent: ' . $price . '/day';
}
add_filter( 'woocommerce_cart_item_price', 'cp_change_product_price_display_in_cart', 10, 3 );
关于php - 在 WooCommerce 中的价格前后添加文本(不包括订阅产品),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56032657/