Digital experiences for all disciplines
New Landing › How can we help? › Atelier › SKU Display on Loops
New Landing › How can we help? › Atelier › SKU Display on Loops
- This topic has 12 replies, 3 voices, and was last updated 9 years by Kyle – SUPPORT.
-
Posted in: Atelier
-
June 16, 2015 at 11:33 pm #185208
How can we get the product standard/list/grid loops to show the SKU in place of the categories for each product?
Attachments:
You must be logged in to view attached files.June 17, 2015 at 8:22 am #185301Hi
Open up atelier/woocommerce/content-product.php and replace the 2 instances of:
echo $product->get_categories( ', ', '<span class="posted_in">', '</span>' );
with
echo $product->get_sku( ', ', '<span class="posted_in">', '</span>' );
– Kyle
June 17, 2015 at 8:34 am #185306Fantastic Kyle.
Thanks for the help. I copied atelier/woocommerce/content-product.php over to my atelier child theme and made the change there.
Also, of note (although I’m not sure if it changed anything) I used two different echo’s. The two instances of
echo $product->get_categories( ', ', '<span class="posted_in">', '</span>' );
where actually a little different from each other.One was surrounded by H5 tags, and the other just span. So I used
echo $product->get_sku( ', ', '<h5 class="posted_in">', '</h5>' );
on line 296 and
echo $product->get_sku( ', ', '<span class="posted_in">', '</span>' );
on line 325.Again. Thank you.
June 17, 2015 at 8:41 am #185309Ok no problem ๐
June 28, 2015 at 4:29 pm #189404Hi Again.
Since you were so helpful in getting the SKU to display on the category product loops, I was curious if you could help me get one more thing displayed.
I’m matching my products to a category hero image that displays all the products arranged in an exploded view, with a reference letter/number next to the product image on that view. So for each product, I have custom meta fields on the products to set this reference letter/number per category for each product (so long as it is in that category, and matches one of the references on the image).
So the first question is, how do we insert a custom field into the loop?
And the second is, how do we dynamically call for the correct custom field based on the category slug?I’m using Advanced Custom Fields for the explosion reference fields because I can have a sidebar box displaying a group of all the custom explosion reference fields.
The code below works to display the text on the loop for the Airbox Woo Category (category slug = airbox):
if(get_field('eref-airbox')) { echo '<div><span>REF: ' . get_field('eref-airbox') . '</span></div>'; }
However I need to test for many different fields, all of which have names matching the structure eref-{category-slug}. I don’t want to put hundreds of if statements in my content-product loop. So I’d like to inject the slug for the current category being viewed into the statement. I tried:
$category = get_the_category(); if(get_field('eref-' . $category[0]->slug . '')) { echo '<div><span>REF: ' . get_field('eref-' . $category[0]->slug . '') . '</span></div>'; }
but it is not working. It passes PHP validation, but no longer loads the custom fields.
June 28, 2015 at 7:48 pm #189435Solved my above question on how to load the custom meta. Final code on content-product.php, placed just below the code to load my SKU’s.
$categories = get_the_terms($post_id, 'product_cat'); if(get_field('eref-' . $categories[0]->slug . '')) { echo '<div><span>REF: ' . get_field('eref-' . $categories[0]->slug . '') . '</span></div>'; }
PS
I’m using Advanced Custom Fields, so that is whereget_field
comes from.June 29, 2015 at 8:09 am #189524Great! Thanks for sharing ๐
June 30, 2015 at 7:19 am #190000Damn.
I spoke too soon.
The two categories I was testing did not contain any products which are located in multiple categories. If a product is located in multiple categories (with a REF letter set for multiple categories) then the PHP is loading the REF from the first category in the array. I added
echo '<div>eref-' . $categories[0]->slug . '</div>';
into the loop to confirm that is what was happening. See the image below:June 30, 2015 at 9:25 am #190035<?php echo single_term_title(); ?>
When placed outside or inside of the product archive loop correctly returns the proper title of the active Woocommerce category. So now I just need a way to break that result down to the slug, and I’d be in business.June 30, 2015 at 9:38 am #190038June 30, 2015 at 11:29 pm #190384Hi,
Take a look at
https://codex.wordpress.org/Function_Reference/get_term_bySince it’s an advanced customization we can take more time than we took at the moment. Hope you understand.
-Rui
July 1, 2015 at 2:07 am #190395Hello Rui,
I totally understand and appreciate the pointers when you guys are able to do so. I dug into it some more this afternoon after taking a break from the computer screen. Bingo. Found the solution.
The true solution:
$catslug = get_query_var( 'product_cat' ); if(get_field('eref-' . $catslug . '')) { echo '<div><span>REF: ' . get_field('eref-' . $catslug . '') . '</span></div>'; }
I hope it can help someone else in the future too!
July 1, 2015 at 7:20 am #190444Great, thanks for sharing
– Kyle
-
Posted in: Atelier
You must be logged in and have valid license to reply to this topic.