- This topic has 19 replies, 2 voices, and was last updated 5 years, 8 months ago by David.
-
AuthorPosts
-
July 9, 2018 at 2:33 am #619038Steve
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
July 9, 2018 at 3:53 am #619070DavidStaffCustomer SupportHi 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 ๐
July 9, 2018 at 4:22 am #619095SteveHi 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 contentJuly 9, 2018 at 7:27 am #619232DavidStaffCustomer SupportSomething 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.July 9, 2018 at 8:36 am #619391SteveI’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.
July 9, 2018 at 9:09 am #619427DavidStaffCustomer SupportHmm 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.
July 9, 2018 at 9:16 am #619435SteveI 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
July 9, 2018 at 9:23 am #619449DavidStaffCustomer SupportThat code is a PHP snippet, so if you have Code Snippets plugin installed (?) just add it in there and save and activated ๐
July 9, 2018 at 9:34 am #619462SteveHi 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
July 9, 2018 at 10:03 am #619487DavidStaffCustomer SupportAah 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>'; }
July 10, 2018 at 2:23 am #619977SteveAdded 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
July 10, 2018 at 3:37 am #620036DavidStaffCustomer SupportHi 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.
July 10, 2018 at 3:46 am #620040SteveI 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.July 10, 2018 at 3:56 am #620049DavidStaffCustomer SupportHi 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?
July 10, 2018 at 4:11 am #620055SteveHave 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.
-
AuthorPosts
- You must be logged in to reply to this topic.