Site logo

Archived topic

WooCommerce Archive Pages - Show SKU

19 replies · Started by Steve on July 9, 2018

Viewing posts 1–15 of 20

Hi Tom

I need to show the SKU below the Product Title but above the price on the individual products on the Archive, Search Results and Shop Page.
I am using Code Sippets and have managed to find the following code which puts the SKU in the individual product lines in the Cart.

add_filter( 'woocommerce_cart_item_name', 'add_sku_in_cart', 20, 3);

function add_sku_in_cart( $title, $values, $cart_item_key ) {
	$sku = $values['data']->get_sku();
	return $sku ? $title . sprintf(" (SKU: %s)", $sku) : $title;
}

On the current Site for this Client which is using the Old WooCommerce Canvas with a Child Theme I have used:
Part No. <?php echo '<div class="sku">' . $product->sku . '</div>';
In the "content-product.php" Template to produce the result.

Results can be found at stedall.co.uk - select a product category from "Products" on the menu.

This is messy as I need to update the Template as Woo move the version on with major upgrades.

I would like to incorporate the above code line from content-product.php in a Code Snippet which would avoid the problem.

Can you help. Even better for the future it would be great if this function could be incorporated directly into GP in the wooCommerce module, with the ability to add your own title.

Best Regards

Steve

Hi Steve,

you can use the woocommerce_single_product_summary hook. And assign a priority higher than the price (priority: 10) and lower than the title (priority: 5), something like so:

add_action( 'woocommerce_single_product_summary', 'db_after_single_product_title', 6 );
function db_after_single_product_title() { 
    // your content here
}

GP 1.7 (Beta) Elements module will allow you to simply add a Custom Hook and output your content without using snippets :)

Hi David

Thanks for your prompt reply.
Being a designer and not very good with code I don't fully understand your reply.
Is the code you included to be used in Code Snippet?
Do I add the
Part No. <?php echo '<div class="sku">' . $product->sku . '</div>';
inlace of the // your content

Something like this:

add_action( 'woocommerce_single_product_summary', 'db_show_sku', 6 );
function db_show_sku(){
	global $product;
	echo '<span class="custom_sku">Part no. ' . $product->get_sku() . '</span>';
}

You can then target the span with the .custom_sku class as well, name that what you like.

I've put this in Code Snippet but it doesn't work. (My preferred option if it will work)
How do I target the span with the .custom_sk class.
I don't want to change template code.

Sorry for being a pain.

Hmm does the code not do anything? Just tested it and it works. Can you add the code and let me know so i can take a look.

the custom_sk is a CSS class, if we got the code to work we could use that to style the SKU field.

I think think the problem is I'm not sure where to put.
I need step by step instructions and the code/css.

The site is on a .dev password protected. If that doesn't work I will send you the logon and details. Would prefer by private email or your secure suggestion.

Thanks again for your patience

Steve

That code is a PHP snippet, so if you have Code Snippets plugin installed (?) just add it in there and save and activated ;)

Hi David

I can see what the problem is.
It's on the single product page, thats great.
But I also need it in the shop and results page, below name but above price.

Thanks

Steve

Aah ok, well the shop loop is identical aside of we are going to use a different hook like so:

add_action( 'woocommerce_shop_loop_item_title', 'db_show_sku', 10 );
function db_show_sku(){
	global $product;
	echo '<span class="custom_sku">Part no. ' . $product->get_sku() . '</span>';
}

Added the new code to the existing snippet but didn't like it (error "custom_sku" defined twice.
Tried adding it as a separate code snippet and now dreaded White Screen.
Cant get in to delete it.
Have looked at the server files and can see Code Snippet Plugin but can't find individual code snippet files to delete.
Any ideas.

Looked in log folder against time and date and there is a ups.php file
<?php exit('Access denied'); __halt_compiler(); ?>

Update
Have now decided to wind back the site to a backup.
Site now backup and running.

Deactivated the Individual code snippet and activated the new one.
This produce a Part No.on the Search Results Page but not on the Archive/Category page.
Can we add that in.

Thanks

Hi Steve, my bad and apologies, the custom class shouldn't be the problem but the name of the function db_show_sku for the second function change this to something else on the first and second line.

If code snippets breaks a site then you can add this to your wp-config file just before the line /* That's all, stop editing! Happy blogging. */ :

define('CODE_SNIPPETS_SAFE_MODE', true);

You can remove that once the problem is solved.

I think I understand.

The search results are now showing the Part No.
How do I get the Archive/Category pages to show the Part No.

Hi Steve - in simple terms don't have two functions using the same name.

They should display on the Archives as it uses the same shop loop. Try clearing the cache?

Have changed one of the function names and cleared the Catch and it works!!
It might be worth adding the end result to your help documents because I'm sure other will want to use it.

One final piece to the puzzle.
The following code outputs the SKU to the cart.

add_filter( 'woocommerce_cart_item_name', 'add_sku_in_cart', 20, 3);

function add_sku_in_cart( $title, $values, $cart_item_key ) {
	$sku = $values['data']->get_sku();
	return $sku ? $title . sprintf(" (SKU: %s)", $sku) : $title;
}

How can I change the Title to read Part No.

Thanks.

This archived topic is closed to new replies.