在woocommerce中,我使用的是“ Add text before product price if it's higher than a specific amount in Woocommerce” 应答代码,该代码会在所有价格之前添加文本。
如何使此代码仅适用于特定产品类别?
感谢您的帮助。
答案 0 :(得分:1)
以下代码适用于特定的产品类别。该代码使用针对产品类别的自定义条件函数来处理父产品类别和产品变体:
// Custom conditional function that handle parent product categories too
function has_product_categories( $product_id, $categories ) {
// Initializing
$parent_term_ids = $categories_ids = array();
$taxonomy = 'product_cat';
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
if( is_numeric( $category ) ) {
$categories_ids[] = (int) $category;
} elseif ( term_exists( sanitize_title( $category ), $taxonomy ) ) {
$categories_ids[] = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
}
}
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}
add_filter( 'woocommerce_get_price_html', 'prepend_text_to_product_price', 20, 2 );
function prepend_text_to_product_price( $price_html, $product ) {
// HERE set your product category in the array
$product_category = array('clothing');
// Only on frontend and excluding min/max prices for variable products
if( is_admin() || $product->is_type('variable') )
return $price_html;
// Get the variable product ID for product variations (as variations dont handle product categories)
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
// Only for a specific product category
if( ! has_product_categories( $product_id, $product_category ) )
return $price_html;
// Get product price
$price = (float) $product->get_price(); // Regular price
if( $price > 15 )
$price_html = '<span>'.__('(GRATIS)', 'woocommerce' ).'</span> '.$price_html;
return $price_html;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。
您还可以使用has_term()
条件函数(不处理父产品类别):
add_filter( 'woocommerce_get_price_html', 'prepend_text_to_product_price', 20, 2 );
function prepend_text_to_product_price( $price_html, $product ) {
// HERE set your product category in the array
$product_category = array('clothing');
// Only on frontend and excluding min/max prices for variable products
if( is_admin() || $product->is_type('variable') )
return $price_html;
// Get the variable product ID for product variations (as variations dont handle product categories)
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
// Only for a specific product category
if( ! has_product_categories( $product_id, $product_category ) )
return $price_html;
// Get product price
$price = (float) $product->get_price(); // Regular price
if( $price > 15 )
$price_html = '<span>'.__('(GRATIS)', 'woocommerce' ).'</span> '.$price_html;
return $price_html;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。
相关主题:Add text before product price if it's higher than a specific amount in Woocommerce
答案 1 :(得分:0)
这应该起作用: 在该问题的现有答案中添加了has_term作为类别。
if ( has_term( 'putcategorynamehere', `'product_cat'` ) ) {
add_filter( 'woocommerce_get_price_html', 'prepend_text_to_product_price', 20, 2 );
function prepend_text_to_product_price( $price_html, $product ) {
// Only on frontend and excluding min/max prices on variable products
if( is_admin() || $product->is_type('variable') )
return $price_html;
// Get product price
$price = (float) $product->get_price(); // Regular price
if( $price > 15 )
$price_html = '<span>'.__('(GRATIS)', 'woocommerce' ).'</span> '.$price_html;
return $price_html;
}
}