Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

The product object: reading

A product in code is an object of the WC_Product class. It has a method for every property: the name, the price, the stock. Where exactly the data lives in the database is the object's business — you only call the method.

Where to write the code

Create the includes/products.php file and include it in acme-shop.php with the line require __DIR__ . '/includes/products.php';. Everything about products goes there.

Getting a product

// wp-content/plugins/acme-shop/includes/products.php
add_action('woocommerce_before_shop_loop', function () {
    $product = wc_get_product(12);
 
    if ($product) {
        echo '<p>Product of the day: ' . esc_html($product->get_name()) . '</p>';
    }
});

wc_get_product() takes an ID and returns a product object, or false if there is no such product. Replace 12 with the ID of your mug and open /shop/: the line Product of the day: Mug appears above the catalog.

Properties

MethodWhat it returns
get_id()The ID
get_name()The name
get_sku()The SKU
get_regular_price()The regular price
get_sale_price()The sale price, an empty string when there is none
get_price()The active price
get_price_html()The price as HTML, the old one struck through
get_short_description()The short description
get_stock_quantity()The stock, null without stock management
get_permalink()The product page address
get_image_id()The image ID

Prices are returned as strings, for example '9.90': cast them with (float) before calculating.

Checks

Methods starting with is_ answer yes or no:

// wp-content/plugins/acme-shop/includes/products.php
add_action('woocommerce_before_shop_loop', function () {
    $product = wc_get_product(12);
 
    if ($product && $product->is_in_stock() && $product->is_on_sale()) {
        printf(
            '<p><a href="%s">%s</a> on sale: %s</p>',
            esc_url($product->get_permalink()),
            esc_html($product->get_name()),
            $product->get_price_html()
        );
    }
});

The line with the link and the price appears only if the product is in stock and on sale. A sale is set in the product editor, the Sale price field.

The current product

On a product page and inside a catalog tile WooCommerce itself puts the current product into the global $product variable:

// wp-content/plugins/acme-shop/includes/products.php
add_action('woocommerce_single_product_summary', function () {
    global $product;
 
    if ($product instanceof WC_Product && $product->get_sku() !== '') {
        echo '<p>SKU: ' . esc_html($product->get_sku()) . '</p>';
    }
}, 6);

Open the page of any product with a SKU filled in: the line SKU: MUG-001 appears under the name. There is no need to call wc_get_product() here.

The image

echo wp_get_attachment_image($product->get_image_id(), 'woocommerce_thumbnail');
echo $product->get_image('woocommerce_single');

The first line prints the image tag by its ID; the second does the same through a product method and substitutes a placeholder if there is no image.

Common mistake

The result of wc_get_product() is used without a check: wc_get_product($id)->get_name(). The product may have been deleted — the function returns false and the page crashes with a call to a member function on false error. Always check the result before calling methods.

Thanks for reading!

Donate to the site — we work hard for you.