Support » Plugin: WooCommerce » Display sku in single product page – with variations

  • Resolved wordpressuser18

    (@wordpressuser18)


    I am using this code to make our site display SKUs on a product page and it works great:

    add_action( ‘woocommerce_single_product_summary’, ‘dev_designs_show_sku’, 5 );
    function dev_designs_show_sku(){
    global $product;
    echo ‘SKU: ‘ . $product->get_sku();
    }

    Our only issue is that we have products with variations and when a customer selects a variant from the drop down, the displayed SKU remains the parent SKU – it doesn’t display the selected variant SKU.

    Does anyone have any advice on this?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Rashed Ripon

    (@rur165)

    Hello @wordpressuser18 ,

    I understand your request and it will take a bit of a trick to make it work as expected. You can understand that the SKU information needs to be updated as soon as the variation selection is made. This will require the use of Javascript/jQuery along with a PHP code that you have used.

    We can rather re-use one of the default WooCommerce code that comes from the product meta.php file.

    So, this is how you can use your code now –

    add_action( 'woocommerce_single_product_summary', 'dev_designs_show_sku', 5 );
    
    function dev_designs_show_sku(){
    	global $product;
    	?>
    
    	<div class="product_meta custom">
    
    		<?php if ( wc_product_sku_enabled() && ( $product->get_sku() || $product->is_type( 'variable' ) ) ) : ?>
    
    			<span class="sku_wrapper"><?php esc_html_e( 'SKU:', 'woocommerce' ); ?> <span class="sku"><?php echo ( $sku = $product->get_sku() ) ? $sku : esc_html__( 'N/A', 'woocommerce' ); ?></span></span>
    		<?php endif; ?>
    
    	</div>
     <?php
    }

    You can now target this custom product meta class using .product_meta.custom and change the style via CSS.

    I hope this information helps.

    Thank you 🙂

    Plugin Support abwaita

    (@abwaita)

    Hi @rur165,

    We’ve not heard back from you in a while, so I’m marking this thread as resolved. Hopefully, the above info was useful and you were able to find a solution to your problem!

    If you have further questions, please feel free to open a new topic.

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.