Get a list of siblings term ids from current product category in WooCommerce

Cray :

I want to retrieve a list of term Ids based on the current category ID.

At the moment I'm using the following code:

$product_cat_items  = get_queried_object();
$product_cat_id     = $product_cat_items->term_id;
$product_cat_child  = get_term($product_cat_id, 'product_cat');
$product_cat_parent = $product_cat_child->parent;

$product_cat_related= get_terms('product_cat', array( 'parent' => $product_cat_parent, 'exclude' => $product_cat_id ));

It's working and I get an array of the terms. But the probem is, that I only need the IDs from the term object to get a list like this:

123,345,678

Is there any way to extract such a list from the $product_cat_related array?

This is the current output:

array(2) {
  [0]=>
  object(WP_Term)#26238 (10) {
    ["term_id"]=>
    int(177)
    ["name"]=>
    string(27) "Name"
    ["slug"]=>
    string(21) "name"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(177)
    ["taxonomy"]=>
    string(11) "product_cat"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    int(140)
    ["count"]=>
    int(8)
    ["filter"]=>
    string(3) "raw"
  }
  [1]=> ....
}
LoicTheAztec :

Since WordPress version 4.5.0, taxonomies should be passed via the "taxonomy" argument in the $args array (see get_terms() documentation).
Also get_queried_object() already gives a WP_Term Object when the queried Object is a taxonomy term.
Also you can use 'fields' => 'ids' as an argument in get_terms(), to get only an array of term Ids instead of an array of WP_term Objects (see WP_Term_Query available arguments).
To finish, you will use PHP implode() to get a string of coma separated terms Ids.

So your code will be instead:

$current_term = get_queried_object(); // Already a WP_Term Object

if ( $current_term->parent > 0 ) {
    $siblings_ids = get_terms( array(
        'taxonomy'  => 'product_cat',
        'parent'    => $current_term->parent,
        'exclude'   => $current_term->term_id,
        'fields'    => 'ids',
    ) );

    // Get a string of coma separated terms Ids
    $siblings_list_ids = implode(',', $siblings_ids);

    // Testing output
    echo $siblings_list_ids;
}

Tested and works.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get a product from specific product attribute term name in WooCommerce

How to get product category ids from order items in WooCommerce

Exclude specific product ids from a category price suffix change in WooCommerce

WooCommerce get parent category from current category page

Get "primary" category image from WooCommerce product

Get product categories ids from order items in WooCommerce

Get the Product tags for the current product only in WooCommerce

Get variations IDs from a variable product in Woocommerce 3

Get the parent product categories ids from a sub product category ID in Woocommerce

Get all Woocommerce products from current product category term Id in a WP_Query

List the subcategories of a given product category in Woocommerce

Add the term slug as a class to the product category div tag in Woocommerce

Set a product category term in a product on Woocommerce

List main product subcategories of a product category in WooCommerce

Append current category name to product title on woocommerce category archives

Get the subcategories of the current product category in Woocommerce archives

Get the product category terms from cart items in WooCommerce

How to get all products from current WooCommerce product category?

If product category has children remove permalink from parent term in WooCommerce

Get the category name from a WooCommerce product using the product id or the category id

Get only one product category term for a WooCommerce product

woocommerce - How do I get the most top level category of the current product category

woocommerce get product category id

Woocommerce - remove product from category

How to get sum group by category from product list

Woocommerce Product child category from current parent category

Getting the 'thumbnail ID' from last product category term in WooCommerce

Get term names array from all product for a specific product attribute in Woocommerce

Get current product category all products parent sku's and display them in array - Woocommerce

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive