Woocommerce - Display product category above product title - wordpress

I am trying to display specific categories above each of my product titles on the main shop page, the product category page, the single product page and the cart.
Using Shopfront as an example in the image below, I want to display "Nikon" above the titles of these flashes. The products are in multiple categories (Nikon, flashes, camera equipment etc.) but I only want to display the brand (Nikon) above the title. There are multiple brands within the shop (Canon, Nikon, Sony etc.) so the correct brand needs to be shown above the title depending on the maker of the product.
How can I programmatically add the "brand" category above the titles? I was thinking I could call for the categories list associated with each product within the product loop, but somehow whitelist or manually specify within the code which categories are allowed to be output on the front-end.
I have Googled pretty deep on this but can't find anything that gets me heading in the right direction.
Thanks in advance.

This code should solve your problem.You can add this code in functions.php.I tested and confirmed its working for me.
function category_single_product(){
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
$single_cat = array_shift( $product_cats ); ?>
<h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2>
<?php }
}
add_action( 'woocommerce_before_shop_loop_item_title', 'category_single_product', 25 );

Related

Woocommerce product category filter dosn't update ACF

There is custom filed created to display second place for Seo text just under products on product category archive pages.
When entering category from menu bar - the different description in ACF correctly changes according to chosen category.
The problem is - WHEN - we are in any of these product categories and want to use product category filter from the side bar. Then nothing is changing. Category changes but description from ACF stays from previous category. That should work like this, we ar in A category, but want to filter products with B category filter, after applying that filter there should be updated page with b category products and updated ACF Seo description. How can we resolve that?
My code:
add_action( 'woocommerce_after_main_content', 'my_extra_description' );
function my_extra_description() {
global $wp_query;
apply_filters( 'acf/update_value', $value, $post_id, $field, $original );
# get the query object
$category = $wp_query->get_queried_object();
#get the cat ID
$category_ID = $category->term_id;
#get the field
$contenu_categorie = get_field( 'contenu_categorie', 'category_'.$category_ID );
#if we have data, show it
if( $contenu_categorie ){
echo $contenu_categorie;
}
}
Plase help :)

add an icon after product price on woocommerce

Hello as the title says.
i'm after a solution for wordpresss woocommerce with products that needs to have a added icon with a van.
same as the following link
Add an icon after product price on Woocommerce archive pages
However i've managed to made categories with fa fast-shipping and gotten the icon.
I've added the code
add_filter( 'woocommerce_get_price_html', 'prepend_append_icon_to_price', 10, 2 );
function prepend_append_icon_to_price( $price, $product ) {
if( has_term( 'fast-shipping', 'product_cat', $product->get_id() ) && ! is_product() ){
$price .= '<span style="float:right"><i class="fas fa-shipping-fast"></i></span> ';
}
return $price;
}
but it still doesn't want to pop up on my product which i dont understand.
Display Picture
I am not sure about this. Try going to the product code(where your product page with the product is shown) which will be located in themes mostly and then go below the pic and add your icon. I hope this helps :) Let me know if you find it difficult to figure it out and ill help you

Woocommerce variable product on custom single product template

I've developed custom single product template for Woocommerce for products which works correctly and as intended. The problem is that now the customer wants some different variations of products that are being sold and I can't work out how to add this functionality.
Currently on the product page I have used functions such as the_post_thumbail() and get_post_meta() to display various information about the product such as price and excerpt entered in the product post type. I then use do_action('woocommerce_simple_add_to_cart') along with a few other buttons.
This all works fine but the problem is now with the variation feature. I found the following code which echos out the product variations ID and Variation Type but I'm not sure how to implement this into a working system. Any help or guidance would be appreciated.
global $product, $post;
$variations = $product->get_available_variations();
foreach ($variations as $key => $value) {
echo 'variation ID'.$value['variation_id'];
foreach ($value['attributes'] as $attr_key => $attr_value) {
echo $attr_key.': '.$attr_value;
}
}
I found the answer so I'm just posting this here in case anyone else has the same question. It was as simple as changing do_action('woocommerce_simple_add_to_cart') to do_action('woocommerce_variable_add_to_cart'). This displays the product variations in a list box and has the add to cart button.

Get a sidebar widget that show products of the same categories in Woocommerce

I’m trying to set a sidebar in the single product page that show all products of the same categories as the product displayed.
That's how I proceed:
1) First I’ve created a sidebar called “Products_of_same_Category” to put in there a widget to show what I needed, then in function.php of my child theme I added the following snippet to execute php code in text widget:
// Enable PHP in widgets
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
2) Then when I saw that the snippet runs ok I added that code to test it:
<?php
$prod=get_the_term_list( $post->ID, 'product_cat');
echo $prod;
?>
And all worked fine, it gave me the exact name of the current category of the product displayed in the single product page.
3) So I've tried another test, deleting the previous code, to view if a shortcode translated in php should works in a widget too (writing at this time the exact category name requested, in this case "towels" - in the code below I substitute it with THE-CATEGORY-I-LIKE):
<?php echo do_shortcode('[product_category category=“THE-CATEGORY-I-LIKE” per_page="20" columns="1" orderby="title" order="desc"]'); ?>`
And all was again well done!
4) Finally I mixed all in this code to show the list of products of same categories but something goes wrong:
<?php $prod=get_the_term_list( $post->ID, 'product_cat', '', '', '' );
echo do_shortcode('[product_category category="'.$prod.'" per_page="20" columns="1" orderby="title" order="desc"]'); ?>
In last case the code doesn't display anything. I don't understand where I made mistakes, the syntax is wrong or the solving approach is illogical?
I really appreciate any help about this.
The problem is how you get the category slug. get_the_term_list will give you a formatted linked list of the categories, so it will display category names, not category slugs, which are different things. "Towels" would be your category name, but the category slug would be "towels". And product_category shortcode expect a slug, not a name.
The correct approach to get the category product slug is the following:
$terms = get_the_terms($post, 'product_cat');
if($terms && ! is_wp_error($terms)) {
foreach($terms as $term) {
echo do_shortcode('[product_category category="'.$term->slug.'" per_page="20" columns="1" orderby="title" order="desc"]');
}
}
This will display the products of all the categories associated to your product. See get_the_terms doc for reference.
In order to remove from the results the current product shown, you can make use of the woocommerce_shortcode_products_query filter. It isn't documented, but you can find it by looking at the product_category shortcode code located in includes/class-wc-shortcodes.php. In the product_category() method you will find the following line:
$return = self::product_loop( $query_args, $atts, 'product_cat' );
Where $query_args is a WP_Query parameters array. In the same class you will find the method product_loop() called here and see the following:
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $query_args, $atts ) );
So the query arguments are filtered - you will be able to work with that to add the desirated behavour. What you want to do is to use the post__not_in parameter to the query like this:
function remove_current_product_from_wc_shortcode($args, $atts) {
if(is_product()) { // check if we're on a single product page
$args['post__not_in'] = array(get_queried_object_id());
}
return $args;
}
add_filter('woocommerce_shortcode_products_query', 'remove_current_product_from_wc_shortcode');
This code should go in your theme functions.php - please not this is untested, so if it doesn't work look at the get_queried_object_id() return if it contain the current product ID.

Include the current product category name at the top of the single product page in woocommerce?

Just wanted to ask if anyone here can tell me how to include the current product category name at the top of the single product page in Woocommerce.
I'm going to want to place it above the breadcrumb trail at the top of my product page. I've included a link to an example of what I'm looking for here: http://www.espguitars.com/guitars/signature/kirk.html -
Notice at the top right-hand corner of the page you'll see the current category name, in this case, the category name is Guitars. I realize that the site example I've given is not a wordpress site, but I feel certain there's a way to do this with woocommerce.
If you check the meta.php file in the "templates/single-product" folder of the WooCommerce plugin, you'll see how the WooCommerce plugin displays the product categories of the current product as product meta information.
<?php
...
global $post, $product;
?>
<div class="product_meta">
...
<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
...
</div>
Please copy the code to your theme and modify it accordingly.

Resources