How to use Shortcodes?

is_product():
The is_product() function is a conditional function in WooCommerce that checks whether the current page is a single product page. It returns true if the current page is a product page, and false if it is not.

! is_product():
The exclamation mark (!) in front of is_product() is the logical “not” (negation) operator. So, ! is_product() will return true if the current page is not a product page.

return ”;:
If the ! is_product() condition is met (i.e. if the current page is not a product page), then the function will return an empty value ” and stop further execution of the code inside it. This means that the code after the return will not be executed.

Conditional tags in WooCommerce

If you want the shortcode function to be able to run in the loop product archive, use is_shop

function display_product_stock_with_text() {
    global $product; // Access the global WooCommerce product object

    if ( ! is_shop() ) {
        return ''; 
    }

    // Get the product stock status
    $stock_quantity = $product->get_stock_quantity();
    $stock_status = $product->get_stock_status();

    // Customize the text based on stock status
    if ( $stock_status == 'instock' ) {
        return "<p>Only $stock_quantity items left in stock! Hurry!</p>";
    } elseif ( $stock_status == 'outofstock' ) {
        return "<p>Sorry, this product is currently out of stock.</p>";
    } else {
        return "<p>Stock status unavailable.</p>";
    }
}

// Register the shortcode [product_stock_status]
add_shortcode( 'product_stock_status', 'display_product_stock_with_text' );

techhipbettruvabetnorabahisbahis forumutaraftarium24edusedueduedueduedueduedueduedus