Check for a product category in a hooked function for WooCommerce products

American Jewelry

I want to check the category of WooCommerce products in function.php.

That is my code:

function for_preorder()
{
///// DISPLAY something if category id = 50 
}
add_action('woocommerce_before_single_product','for_preorder'); 

How to check for a product category for a WooCommerce product?

LoicTheAztec

You can use has_term() WordPress conditional function to check for a category like:

add_action('woocommerce_before_single_product','for_preorder'); 
function for_preorder() {
    global $product;

    $categories = array( 50 ); // Here define your categories term Ids, slugs or names

    if ( has_term( $categories, 'product_cat', $product->get_id() ) ) {
        echo '<p>' . __("DISPLAY something here") . '</p>';
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

WooCommerce specific coupon discount based on product category

분류에서Dev

In stock products in category

분류에서Dev

combine products with same category in array

분류에서Dev

Woocommerce sub category loop

분류에서Dev

Get WooCommerce products tags for array of products

분류에서Dev

Display product under its category

분류에서Dev

Listing a User's Products by Category in Eloquent

분류에서Dev

Magento - Get all products in the current category

분류에서Dev

WooCommerce main product image

분류에서Dev

Backbone filter products in each category ( Filter parameter will be category name )

분류에서Dev

woocommerce category wrong class name

분류에서Dev

ACF "acf / save_post"후크의 WooCommerce WC_Product set_category_ids () 메서드 문제

분류에서Dev

how to get category name of current product (on product detail page) in magento

분류에서Dev

Wordpress Plugin - WooCommerce - Hide a product if

분류에서Dev

Display products in 2 rows on mobile - Woocommerce

분류에서Dev

Woocommerce delete products which are "Out of stock"

분류에서Dev

Prestashop 1.6 get feature value of products from specific category

분류에서Dev

Shopfy accessing all products from a collection on the product page

분류에서Dev

How to disable product image thumbnails for woocommerce gallery?

분류에서Dev

AttributeError at / 'Product'object has no attribute 'get_category_display'

분류에서Dev

What is the best way to design Seller, Category and Product tables?

분류에서Dev

Cartesian product generic function in Python

분류에서Dev

Differentiating a product with an unknown function - sympy

분류에서Dev

Mongoose: Get the data from 2 collections, like get products that have same category type like fruits

분류에서Dev

Display a product custom field only in WooCommerce Admin single orders

분류에서Dev

woocommerce 2.1.8 product_type이 null입니다.

분류에서Dev

Show the price of a product in WooCommerce after a button is clicked by the customer

분류에서Dev

Missing required parameters for [Route: getFirstProduct] [URI: {language}/products/{product1}]

분류에서Dev

Selenium webdriver- how to select a product when all products have same class (amazon.in)

Related 관련 기사

  1. 1

    WooCommerce specific coupon discount based on product category

  2. 2

    In stock products in category

  3. 3

    combine products with same category in array

  4. 4

    Woocommerce sub category loop

  5. 5

    Get WooCommerce products tags for array of products

  6. 6

    Display product under its category

  7. 7

    Listing a User's Products by Category in Eloquent

  8. 8

    Magento - Get all products in the current category

  9. 9

    WooCommerce main product image

  10. 10

    Backbone filter products in each category ( Filter parameter will be category name )

  11. 11

    woocommerce category wrong class name

  12. 12

    ACF "acf / save_post"후크의 WooCommerce WC_Product set_category_ids () 메서드 문제

  13. 13

    how to get category name of current product (on product detail page) in magento

  14. 14

    Wordpress Plugin - WooCommerce - Hide a product if

  15. 15

    Display products in 2 rows on mobile - Woocommerce

  16. 16

    Woocommerce delete products which are "Out of stock"

  17. 17

    Prestashop 1.6 get feature value of products from specific category

  18. 18

    Shopfy accessing all products from a collection on the product page

  19. 19

    How to disable product image thumbnails for woocommerce gallery?

  20. 20

    AttributeError at / 'Product'object has no attribute 'get_category_display'

  21. 21

    What is the best way to design Seller, Category and Product tables?

  22. 22

    Cartesian product generic function in Python

  23. 23

    Differentiating a product with an unknown function - sympy

  24. 24

    Mongoose: Get the data from 2 collections, like get products that have same category type like fruits

  25. 25

    Display a product custom field only in WooCommerce Admin single orders

  26. 26

    woocommerce 2.1.8 product_type이 null입니다.

  27. 27

    Show the price of a product in WooCommerce after a button is clicked by the customer

  28. 28

    Missing required parameters for [Route: getFirstProduct] [URI: {language}/products/{product1}]

  29. 29

    Selenium webdriver- how to select a product when all products have same class (amazon.in)

뜨겁다태그

보관