-2

I am trying to get product category on check out page for showing a special message for shipping for specific category. For this I have created action filter on woocommerce_available_shipping_methods in theme functions.php

This is my code for getting category here -

global $post;
$args = array( 'taxonomy' => 'product_cat',);
$terms = wp_get_post_terms($post->ID,'product_cat', $args);
print_r($terms);

But it's not giving category on checkout page. Only product ID is one thing I am getting here.

Any help please.

Thanks is advance

closed as off-topic by Pieter Goosen, birgire, gmazzap, Johannes Pille, fuxia Aug 16 '14 at 0:51

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – Pieter Goosen, birgire, gmazzap, Johannes Pille, fuxia
If this question can be reworded to fit the rules in the help center, please edit the question.

2

The problem is that your code, especially the

global $post;

and then

$post->ID

part is getting the $post_id of the page you are on from the $post global variable, not of the product(s) in your cart, which would be what you are looking for.

Aside from that, the $args parameter of wp_get_post_terms has no argument taxonomy, besides you already defined the taxonomy as second parameter of the function call.


Disclaimer:

  • I answered this because the problem on hand has nothing to do with the third-party plugin, questions about those are generally off topic, but with understanding how WordPress works.
  • So the woocommerce specific part below does not justify asking off topic questions, but is meant to help someone out anyway.

Getting the ids of the items in your cart can be done somewhat like this:

global $woocommerce;
$cart = $woocommerce->cart->get_cart();
$cart_items_ids = array();
foreach ( $cart as $item_key => $item_value ) {
    $cart_items_ids[] = $item_value[ 'data' ]->id;
}
  • 1
    +1 for not doing any automatic close. I agree part of this is still on-topic. – helgatheviking Aug 14 '14 at 11:18
  • In a way things have gotten a bit more complicated lately; or at least more restrictive. @helgatheviking – Nicolai Aug 14 '14 at 12:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.