Skip to content
Advertisement

How to show SKU before woocommerce product title on archive page

I’m trying to show the SKU before title of product in woocommerce. Here is a code what I’m trying bellow code show the SKU after price on Woocommerce archive page. But is there any hook available to show it before the product title on archive page ?

I use woocommerce_get_price_html this hook to show it after price I also try the_title but it override the menu also.

function rupom_custom_price_sku( $price ) { 
    global $woocommerce , $product;
    $sku = $product->get_sku();
    if (is_shop() || is_product_category() || is_product_tag()){
        return $price . '<br /><span class="price-sku" style="color:red">Item # ' . $sku . '</span>';
    }

    else { 
        return $price; 
    } 
}
add_filter( 'woocommerce_get_price_html', 'rupom_custom_price_sku' );

Advertisement

Answer

woocommerce_before_shop_loop_item_title is the hook you need to use

add_action( 'woocommerce_before_shop_loop_item_title', 'custom_before_title' );
function custom_before_title() {

    global $product;

    if ( $product->get_sku() ) {
        echo $product->get_sku();
    }

}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement