如何在 WooCommerce 中定位一系列产品 ID?
How to target an array of Product IDs in WooCommerce?
当价格字段留空时,我正在使用下面的代码将产品价格更改为文本(联系定价):
add_filter( 'woocommerce_get_price_html', 'change_special_product_price', 10, 2 );
function change_special_product_price( $price_html, $product ) {
if ( $product->id == 6917 ) {
$price_html = '<span class="woocommerce-Price-amount amount">Contact for Pricing</span>';
}
return $price_html;
}
add_filter('woocommerce_empty_price_html', 'custom_call_for_empty_price_html');
function custom_call_for_empty_price_html() {
return 'TBC';
}
function dequeue_woocommerce_styles_scripts() {
if ( function_exists( 'is_woocommerce' ) ) {
if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
}
}
}
它适用于一种产品,但我无法让它适用于多种产品。
如何定位一组产品 ID?
对于产品 ID 数组,使用 in_array()
条件函数,如下所示:
function change_special_product_price( $price_html, $product ) {
$product_ids = array(6917, 6918, 6921);
if ( in_array($product->get_id(), $product_ids ) ) {
$price_html = '<span class="woocommerce-Price-amount amount">' . __("Contact for Pricing") . '</span>';
}
return $price_html;
}
当价格字段留空时,我正在使用下面的代码将产品价格更改为文本(联系定价):
add_filter( 'woocommerce_get_price_html', 'change_special_product_price', 10, 2 );
function change_special_product_price( $price_html, $product ) {
if ( $product->id == 6917 ) {
$price_html = '<span class="woocommerce-Price-amount amount">Contact for Pricing</span>';
}
return $price_html;
}
add_filter('woocommerce_empty_price_html', 'custom_call_for_empty_price_html');
function custom_call_for_empty_price_html() {
return 'TBC';
}
function dequeue_woocommerce_styles_scripts() {
if ( function_exists( 'is_woocommerce' ) ) {
if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
}
}
}
它适用于一种产品,但我无法让它适用于多种产品。
如何定位一组产品 ID?
对于产品 ID 数组,使用 in_array()
条件函数,如下所示:
function change_special_product_price( $price_html, $product ) {
$product_ids = array(6917, 6918, 6921);
if ( in_array($product->get_id(), $product_ids ) ) {
$price_html = '<span class="woocommerce-Price-amount amount">' . __("Contact for Pricing") . '</span>';
}
return $price_html;
}