Get product details from $product object WooCommerce

Get product details from $product object WooCommerce
940 Views
0
(0)

How do I obtain the WooCommerce product ID? How can I get the product’s SKU? Or how do you find out the regular price, sale price, stock, shipping class, tax class, images, dimensions, attributes, categories, and other product information?

To perform various store operations, such as determining which products were present in order, applying advanced product filters, editing prices and descriptions, and so on, you must obtain the product ID and other details.

When we have a $product object

Hooks (which take action and apply filters) make use of extra arguments given to the method. It’s fantastic if they let you utilize the “$product” object. You may also specify the “global $product” inside your function.

This is how you acquire all of the product information in both cases:

<?php 
  /* For Get Product ID */
  $product->get_id();

  /* For Get Product General Information */
  $product->get_type();
  $product->get_name();
  $product->get_slug();
  $product->get_date_created();
  $product->get_date_modified();
  $product->get_status();
  $product->get_featured();
  $product->get_catalog_visibility();
  $product->get_description();
  $product->get_short_description();
  $product->get_sku();
  $product->get_menu_order();
  $product->get_virtual();
  get_permalink( $product->get_id() );

  /* For Get Product Prices */
  $product->get_price();
  $product->get_regular_price();
  $product->get_sale_price();
  $product->get_date_on_sale_from();
  $product->get_date_on_sale_to();
  $product->get_total_sales();

  /* For Get Product Taxs, Shipping Info & Stock */
  $product->get_tax_status();
  $product->get_tax_class();
  $product->get_manage_stock();
  $product->get_stock_quantity();
  $product->get_stock_status();
  $product->get_backorders();
  $product->get_sold_individually();
  $product->get_purchase_note();
  $product->get_shipping_class_id();

  /* For Get Product Dimensions */
  $product->get_weight();
  $product->get_length();
  $product->get_width();
  $product->get_height();
  $product->get_dimensions();

  /* For Get Linked Products */
  $product->get_upsell_ids();
  $product->get_cross_sell_ids();
  $product->get_parent_id();

  /* For Get Product Variations and Attributes */
  $product->get_children(); // get product variations
  $product->get_attributes();
  $product->get_default_attributes();
  $product->get_attribute( 'attributeid' ); //get specific attribute value

  /* For Get Product Taxonomies */
  $product->get_categories();
  $product->get_category_ids();
  $product->get_tag_ids();

  /* For Get Product Downloads */
  $product->get_downloads();
  $product->get_download_expiry();
  $product->get_downloadable();
  $product->get_download_limit();

  /* For Get Product Images */
  $product->get_image_id();
  $product->get_image();
  $product->get_gallery_image_ids();

  /* For Get Product Reviews */
  $product->get_reviews_allowed();
  $product->get_rating_counts();
  $product->get_average_rating();
  $product->get_review_count();
?>

Get $product object by $product_id

If you have the product ID, you can access the product object with the following WooCommerce function:

<?php
  $product = wc_get_product( $product_id );
?>

Get $order object by $order_id

<?php
  $order = wc_get_order( $order_id );
  $items = $order->get_items();

  foreach ( $items as $item ) {
    
      $product = wc_get_product( $item['product_id'] );
      
      /* Now you can get all information from $product object. */
      $product->get_name();

  }
?>
  • The code starts by getting the order with an ID of “123”.
  •  Then it loops through all the items in that order.
  •  The loop starts by calling wc_get_order() which returns a WCOrder object.
  •  This is then passed to get_items(), which returns an array of WCItem objects, each representing an item in the order.
  •  The code then iterates over these items and gets their product information from $product->get_name().
  •  The code will iterate through all the products in the order and print out their names.

Get products from $cart object

How do I get the product information from within the cart? In this case, you will need to loop through all of the items in the cart and then apply the above rules.

<?php
  $cart = WC()->cart->get_cart();

  foreach( $cart as $cart_item ){

      $product = wc_get_product( $cart_item['product_id'] );

      /* Now you can get all information from $product object. */
      $product->get_name();

  }
?>

Conclusion:

If you enjoy coding and are familiar with hooks and filters, you can use the coding approach to obtain product information as needed.

How useful was this blog?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this blog.

  • CodexCoach

    - Web Development Expert

    CodexCoach is a skilled tech educator known for easy-to-follow tutorials in coding, digital design, and software development. With practical tips and interactive examples, CodexCoach helps people learn new tech skills and advance their careers.

Leave a comment

Your email address will not be published. Required fields are marked *