我想打一个折扣类别。但是在某些类别上,我得到父类别ID,而不是子ID。
我试图这样获得产品cat的term_id:
$terms = wp_get_post_terms( $product->get_id(), 'product_cat' );
$term_id = $terms[0]->term_id;
这个版本对我来说不是一个很好的解决方案:
$category = get_queried_object();
$term_id = $category->term_id;
到目前为止,我一直坚持下去:
function category_percentage($product)
{
$terms = wp_get_post_terms( $product->get_id(), 'product_cat' );
$term_id = $terms[0]->term_id; // I belive here is the problem
$discount_category = get_term_meta($term_id, 'discount_category', true);
return $discount_category;
}
add_filter( 'woocommerce_product_get_price', 'custom_sale_price_for_category', 10, 2 );
function custom_sale_price_for_category( $price, $product )
{
if(category_percentage($product)<=0) return $price;
$price *= ( 1 - category_percentage($product)/100 );
return $price;
}
在category_percentage($ product)函数中,我想获取产品的当前类别,但是由于某些原因,在某些类别中,我得到了父类别,是的,我也为产品检查了父类别。但是我对所有产品都有这样的感觉,当我获得当前的cat id时仍然得到不同的结果。