Add a custom text under featured products price in Woocommerce product widget

I am trying to add a line of text under the price of my featured products on my homepage. I have tried editing the content-widget-product.php so it looks like this –

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

global $product;

if ( ! is_a( $product, 'WC_Product' ) ) {
    return;
}

?>
<li>
    <?php do_action( 'woocommerce_widget_product_item_start', $args ); ?>

    <a href="<?php echo esc_url( $product->get_permalink() ); ?>">
        <?php echo wp_kses_post( $product->get_image() ); ?>
        <span class="product-title"><?php echo esc_html( $product->get_name() ); ?></span>
    </a>

    <?php if ( ! empty( $show_rating ) ) : ?>
        <?php echo wp_kses_post( wc_get_rating_html( $product->get_average_rating() ) ); ?>
    <?php endif; ?>

    <?php echo wp_kses_post( $product->get_price_html() ); ?>
    <p class="deliveryline">DELIVERY THROUGHOUT GREATER CAPE TOWN AREA</p>

    <?php do_action( 'woocommerce_widget_product_item_end', $args ); ?>
</li>

I added the “Delivery throughout line”… but it doesn’t do anything though. Can someone please tell me why it’s not working. Thanks!

Answer

Try the following code that will add your custom text to the widget Products items for featured products only under the price in the home page:

add_action( 'woocommerce_widget_product_item_end', 'home_widget_features_products', 10, 1 );
function home_widget_features_products( $args ){
    global $product;

    // Featured product on home page (when using the loop)
    if( $product->is_featured() && is_front_page() )
        echo '<p class="deliveryline">' . __("DELIVERY THROUGHOUT GREATER CAPE TOWN AREA", "woocommerce") . '</p>';
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

enter image description here


Addition: If you use a shortcode in your home page like for example:

[products limit="3" columns="3" visibility="featured"]

You will use the following code (that works for products in the loop):

add_action( 'woocommerce_after_shop_loop_item_title', 'home_loop_features_products', 20 );
function home_loop_features_products(){
    global $product;

    // Featured product on home page (when using the loop)
    if( $product->is_featured() && is_front_page() )
        echo '<p class="deliveryline">' . __("DELIVERY THROUGHOUT GREATER CAPE TOWN AREA", "woocommerce") . '</p>';
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

enter image description here


To display this custom text everywhere for all products (in all Woocommerce archive pages as shop and in all Woocommerce loops as related products, upsells, cross-sells…)

You will use the same code without the if statement:

add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_products_loop', 20 );
function woocommerce_products_loop(){
    global $product;

    echo '<p class="deliveryline">' . __("DELIVERY THROUGHOUT GREATER CAPE TOWN AREA", "woocommerce") . '</p>';
}

Leave a Reply

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