61

I'm currently working on a WooCommerce theme and attempting to add a sidebar to the product detail page.

I've been able to get the sidebar added (specifically, this one: http://woocommerce.wp-a2z.org/oik_file/templatescontent-widget-product-php/)

Now, I'm trying to figure out how to add a class of "active" to the currently selected product and can't seem to figure it out?

In other words, how do I do something along the lines of if the current product id is equal to the product id in the sidebar add class="active" to the li?

I've made numerous searches and haven't been able to come up with anything useful, so I'm turning here.

Any help greatly appreciated!!

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
shparkison
  • 943
  • 1
  • 8
  • 20

7 Answers7

140

2017 Update - since WooCommerce 3:

global $product;
$id = $product->get_id();

Woocommerce doesn't like you accessing those variables directly. This will get rid of any warnings from woocommerce if your wp_debug is true.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Samyer
  • 1,695
  • 1
  • 8
  • 9
48

If the query hasn't been modified by a plugin for some reason, you should be able to get a single product page's "id" via

global $post;
$id = $post->ID

OR

global $product;
$id = $product->id;

EDIT: As of WooCommerce 3.0 this needs to be

global $product;
$id = $product->get_id();
helgatheviking
  • 25,596
  • 11
  • 95
  • 152
  • 1
    I am not able to retrieve the current product id using any of the above two methods. I am using custom plugin to access the current plugin displayed. Please could you help me... – therealprashant Oct 16 '15 at 07:00
  • 1
    Inside the loop this works. If you are trying to access the variable on an early hook (such as `init`) then it won't work. I can't help you further here. You need to ask your own specific and detailed question. – helgatheviking Oct 16 '15 at 13:54
  • Hey thanks for the comment .Sadly I got no question asking power anymore on SO. Could you direct me to some other stream where you could help me. Thanks a lot – therealprashant Oct 19 '15 at 06:35
  • $id = $post->ID - with a capital ID – Juergen Schulze Apr 22 '17 at 09:11
  • 6
    See @Samyer's 2017 Update. Using `$product->id` will trigger this message: __Product properties should not be accessed directly__ – Christian Lescuyer May 05 '17 at 08:56
  • @helgatheviking: Have you ever seen $post->ID not being the same as $product->id ? – Aleksandar Jun 19 '18 at 12:37
  • @alev I haven't seen that. I think it would be incredibly rare since in the DB the product ID and the post ID are the same. – helgatheviking Jun 20 '18 at 14:10
  • @helgatheviking: Thanks! – Aleksandar Jun 21 '18 at 14:34
  • Trying to get property 'id' of non-object and Uncaught Error: Call to a member function get_id() on array – Dev Mar 06 '23 at 08:08
  • @Dev `$object->id` is referenced above. Call to `get_id()` on array means that your `$object` is not an object but rather an array. Check the types of your variables. – helgatheviking Apr 10 '23 at 17:55
23

Since WooCommerce 2.2 you are able to simply use the wc_get_product Method. As an argument you can pass the ID or simply leave it empty if you're already in the loop.

wc_get_product()->get_id();

OR with 2 lines

$product = wc_get_product();
$id = $product->get_id();
Ruvee
  • 8,611
  • 4
  • 18
  • 44
Orlandster
  • 4,706
  • 2
  • 30
  • 45
10

Retrieve the ID of the current item in the WordPress Loop.

echo get_the_ID(); 

hence works for the product id too. #tested #woo-commerce

R T
  • 4,369
  • 3
  • 38
  • 49
7

The correct method is:

global $product;

$id = $product->get_id();
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Keerthi S
  • 177
  • 1
  • 5
1

Save the current product id before entering your loop:

$current_product = $product->id;

Then in your loop for your sidebar, use $product->id again to compare:

 <li><a <? if ($product->id == $current_product) { echo "class='on'"; }?> href="<?=get_permalink();?>"><?=the_title();?></a></li>
Etienne Dupuis
  • 13,548
  • 6
  • 47
  • 58
0

your can query woocommerce programatically you can even add a product to your shopping cart. I'm sure you can figure out how to interact with woocommerce cart once you read the code. how to interact with woocommerce cart programatically

====================================

<?php

add_action('wp_loaded', 'add_product_to_cart');
function add_product_to_cart()
{
    global $wpdb;

    if (!is_admin()) {


        $product_id = wc_get_product_id_by_sku('L3-670115');

        $found = false;

        if (is_user_logged_in()) {
            if (sizeof(WC()->cart->get_cart()) > 0) {
                foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
                    $_product = $values['data'];
                    if ($_product->get_id() == $product_id)
                        WC()->cart->remove_cart_item($cart_item_key);
                }
            }
        } else {
            if (sizeof(WC()->cart->get_cart()) > 0) {
                foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
                    $_product = $values['data'];
                    if ($_product->id == $product_id)
                        $found = true;
                }
                // if product not found, add it
                if (!$found)
                    WC()->cart->add_to_cart($product_id);
            } else {
                // if no products in cart, add it
                WC()->cart->add_to_cart($product_id);
            }
        }
    }
}
Hugo R
  • 2,613
  • 1
  • 14
  • 6