• Resolved lucytech

    (@lucytech)


    How do I display the SKU under the price?

    I’ve tried this code and it isn’t working

    add_action ( 'woocommerce_after_shop_loop_item_title', 'custom_after_title' ); function custom_after_title() { global $product; if ( $product->get_sku() ) { echo $product->get_sku(); } }

    • This topic was modified 1 year, 5 months ago by lucytech.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Saif

    (@babylon1999)

    Hello @lucytech,

    Thank you for reaching out!

    How do I display the SKU under the price?

    You’re using the wrong hook, try this:

    /*  Add SKU under price  */
    
    function add_sku_after_title() {
        global $product;
    
        $sku = $product->get_sku();
    
        if (!empty($sku)) {
            echo '<p class="product-custom-sku">SKU: ' . $sku . '</p>';
        }
    }
    add_action('woocommerce_before_add_to_cart_form', 'add_sku_after_title', 6);


    Link to image: https://d.pr/i/4qJ6Zj

    Hope it helps! :?)

    Thread Starter lucytech

    (@lucytech)

    Thank you .

    That adds it to the single product page (I can’t view your image, security error).

    I need to add it to the product-category listing page as in the link in the first thread. How do I show it on that page?

    Thread Starter lucytech

    (@lucytech)

    I think it the code is being called from this page. How do I hook in the SKU above the price?

    <?php
    /**
     * The template for displaying product content within loops
     *
     * This template can be overridden by copying it to yourtheme/woocommerce/content-product.php.
     *
     * HOWEVER, on occasion WooCommerce will need to update template files and you
     * (the theme developer) will need to copy the new files to your theme to
     * maintain compatibility. We try to do this as little as possible, but it does
     * happen. When this occurs the version of the template file will be bumped and
     * the readme will list any important changes.
     *
     * @see     https://docs.woocommerce.com/document/template-structure/
     * @package WooCommerce\Templates
     * @version 3.6.0
     */
    
    defined( 'ABSPATH' ) || exit;
    
    global $product;
    
    // Ensure visibility.
    if ( empty( $product ) || ! $product->is_visible() ) {
    	return;
    }
    ?>
    
    <li <?php wc_product_class( '', $product ); ?>>
    	<?php
    	/**
    	 * Hook: woocommerce_before_shop_loop_item.
    	 *
    	 * @hooked woocommerce_template_loop_product_link_open - 10
    	 */
    	do_action( 'woocommerce_before_shop_loop_item' );
    
    	/**
    	 * Hook: woocommerce_before_shop_loop_item_title.
    	 *
    	 * @hooked woocommerce_show_product_loop_sale_flash - 10
    	 * @hooked woocommerce_template_loop_product_thumbnail - 10
    	 */
    	do_action( 'woocommerce_before_shop_loop_item_title' );
    
    	/**
    	 * Hook: woocommerce_shop_loop_item_title.
    	 *
    	 * @hooked woocommerce_template_loop_product_title - 10
    	 */
    	do_action( 'woocommerce_shop_loop_item_title' );
    
    	/**
    	 * Hook: woocommerce_after_shop_loop_item_title.
    	 *
    	 * @hooked woocommerce_template_loop_rating - 5
    	 * @hooked woocommerce_template_loop_price - 10
    	 */
    	do_action( 'woocommerce_after_shop_loop_item_title' );
    
    	/**
    	 * Hook: woocommerce_after_shop_loop_item.
    	 *
    	 * @hooked woocommerce_template_loop_product_link_close - 5
    	 * @hooked woocommerce_template_loop_add_to_cart - 10
    	 */
    	do_action( 'woocommerce_after_shop_loop_item' );
    	?>
    </li>
    
    • This reply was modified 1 year, 5 months ago by lucytech.
    Saif

    (@babylon1999)

    I need to add it to the product-category listing page as in the link in the first thread. How do I show it on that page?

    Sorry, I thought you meant the single product page. Your site URL is broken for some reason: https://d.pr/i/5liQBX

    In that case, your snippet should work fine. Here’s a screenshot from my test site: https://d.pr/i/YrzqWX

    It could be a theme-related problem. Have you tried switching to the default Storefront theme temporarily?

    Look forward to hearing back from you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘add sku to product category results page’ is closed to new replies.