WordPress

WooCommerce: wc_get_product Function

27 October 2023

Ever find yourself caught in handling product information on your e-commerce platform? Let’s say you’re running a trendy online boutique and just got a batch of hot-off-the-runway items you are eager to put on your website. Well, if you’re using WooCommerce for your online store, the wc_get_product function comes to the rescue.

Imagine this function as a well-organized warehouse manager who swiftly and efficiently fetches you the product details, as long as you provide either the post object or the post ID of the product. But remember, our diligent worker, wc_get_product, can only start its job once the ‘gateway’ actions: ‘woocommerce_init’, ‘woocommerce_after_register_taxonomy’, and ‘woocommerce_after_register_post_type’ are completed. Simply said, only when you’re done registering your taxonomies and post types correctly, then wc_get_product function can get into action, ensuring the accuracy of your new products’ information. This practical function saves you time and keeps your product info neat, all in one stroke.

Syntax

function wc_get_product( $the_product = false, $deprecated = array() )
Parameters
  • $the_product: (mixed) Post object or post ID of the product.
  • $deprecated: (array) Previously used to pass arguments to the factory, e.g., to force a type.
Return Value
  • (WC_Product|null|false) Returns a WC_Product object if successful, otherwise returns null or false.

How it works

<?php
/**
* Main function for returning products, uses the WC_Product_Factory class.
*
* This function should only be called after 'init' action is finished, as there might be taxonomies that are getting
* registered during the init action.
*
* @since 2.2.0
*
* @param mixed $the_product Post object or post ID of the product.
* @param array $deprecated Previously used to pass arguments to the factory, e.g. to force a type.
* @return WC_Product|null|false
*/
function wc_get_product( $the_product = false, $deprecated = array() ) {
if ( ! did_action( 'woocommerce_init' ) || ! did_action( 'woocommerce_after_register_taxonomy' ) || ! did_action( 'woocommerce_after_register_post_type' ) ) {
/* translators: 1: wc_get_product 2: woocommerce_init 3: woocommerce_after_register_taxonomy 4: woocommerce_after_register_post_type */
wc_doing_it_wrong( __FUNCTION__, sprintf( __( '%1$s should not be called before the %2$s, %3$s and %4$s actions have finished.', 'woocommerce' ), 'wc_get_product', 'woocommerce_init', 'woocommerce_after_register_taxonomy', 'woocommerce_after_register_post_type' ), '3.9' );
return false;
}
if ( ! empty( $deprecated ) ) {
wc_deprecated_argument( 'args', '3.0', 'Passing args to wc_get_product is deprecated. If you need to force a type, construct the product class directly.' );
}
return WC()->product_factory->get_product( $the_product, $deprecated );
}

The wc_get_product function first checks if specific actions related to WooCommerce have been performed i.e., 'woocommerce_init''woocommerce_after_register_taxonomy', and 'woocommerce_after_register_post_type'. If any of these actions haven’t been completed, an error message is triggered and false is returned.

If there are any deprecated arguments present in $deprecated, a deprecated argument warning is also triggered.

Finally, the function calls the get_product() method on WC()->product_factory, passing in $the_product and $deprecated as arguments, and returns its result. This method constructs and returns an instance of the WC_Product class for further use.

Usage

1. How to Get Product Attributes in WooCommerce

To get the attributes of a product in WooCommerce, you can use the wc_get_product function. Here’s an example of how to do it inside a plugin:

<?php
/**
* Plugin Name: My WooCommerce Plugin
*/
// Hook into the 'woocommerce_after_register_taxonomy' action
add_action("woocommerce_after_register_taxonomy", "my_custom_function");
function my_custom_function()
{
// Get the product ID
$product_id = 42; // Replace with the actual ID of your product
// Get the product object using wc_get_product function
$product = wc_get_product($product_id);
// Check if the product exists
if ($product) {
// Get the attributes of the product
$attributes = $product->get_attributes();
// Handle the attributes data
foreach ($attributes as $attribute) {
echo esc_html($attribute["name"]);
foreach ($attribute["options"] as $option) {
echo esc_html($option);
}
}
}
}

In this example:

  • We hook into the woocommerce_after_register_taxonomy action, which is fired after WooCommerce has finished registering taxonomies.
  • We define a custom function my_custom_function, which will be executed when this action is triggered.
  • Inside this function, we specify a product ID (replace 42 with your actual product ID).
  • We call the wc_get_product function, passing in the product ID to retrieve the corresponding WooCommerce WC_Product object.
  • Next, we check if the $product object exists.
  • If it does, we use $product->get_attributes() to retrieve an array of attributes for the product.
  • We iterate over each attribute and display its name and options using simple HTML markup.

After activating and navigating to a page in your WooCommerce store, you should see the attributes of the specified product displayed on the page.

2. How to Get Product Description in WooCommerce

To retrieve the description of a product in WooCommerce, you can utilize the wc_get_product function. Here’s an example of how you can achieve this inside a theme:

<?php
/**
* Theme Name: My WooCommerce Theme
*/
// Hook into the 'woocommerce_after_register_post_type' action
add_action("woocommerce_after_register_post_type", "my_custom_function");
function my_custom_function()
{
// Get the product ID
$product_id = get_the_ID();
// Get the product object using wc_get_product function
$product = wc_get_product($product_id);
// Check if the product exists
if ($product) {
// Get the product description
$description = $product->get_description();
// Display the product description
echo wp_kses_post($description);
}
}

In this example:

  • We hook into the woocommerce_after_register_post_type action, which is triggered after WooCommerce has finished registering post types.
  • We define a custom function my_custom_function, which will be executed when this action is fired.
  • Inside this function, we use get_the_ID() to retrieve the ID of the current post (assuming you are on a single product page).
  • We call the wc_get_product function, passing in the product ID to get the corresponding WooCommerce WC_Product object.
  • Next, we check if the $product object exists.
  • If it does, we use $product->get_description() to retrieve the product description.
  • Finally, we display the description using the wp_kses_post function to ensure safe output of HTML content.

After activating your custom theme and visiting a single product page in your WooCommerce store, you should see the product description displayed within the specified HTML element with the class product-description.

Leave a Reply

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

Array

Some of Our Clients

Join clients who enjoy 96% satisfaction

Schedule a Free Strategy Call with a WordPress Expert