Parent product category page in Woocommerce - php

Is it possible in Woocommerce, if the product category page shows its subcategories, to do any actions? I need to add a class to the body tag on such pages. Is it real? Example:
function my_custom_body_class_woo_cat($classes) {
if ( is_product_category() && is_subcategory_show() ) {
$classes[] = 'my-class';
return $classes;
}
}
add_filter('body_class','my_custom_body_class_woo_cat');`
I tried to look for solutions, but I didn't find one

I checked woocommerce conditional tags and is_subcategory_show() is not a valid woocommerce function.
So in order for this to work we need to do our own checks before assigning the extra class or classes to the body.
Here is a code snippet that does what you asked( if i understood your question correctly ):
add_filter('body_class', function ($classList) {
// Not a product category, return the classList intact
if (!is_product_category()) {
return $classList;
}
$desiredDisplayType = 'subcategories';
$term = get_queried_object();
// Not a parent category, return classList intact
if ($term->parent !== 0) {
return $classList;
}
// Get the woocommerce category archive display option (the one you set up in the customizer)
$categoryArchiveDisplayOption = get_option('woocommerce_category_archive_display');
// Get the display type for the current category
$currentCategoryDisplayOption = get_term_meta($term->term_id, 'display_type', true);
// The current category displatype isn't the desired one, return classList intact
if ($currentCategoryDisplayOption !== $desiredDisplayType) {
return $classList;
}
// Neither of the option are the desired one, return classList intact
if (
$currentCategoryDisplayOption !== $desiredDisplayType
&& $categoryArchiveDisplayOption !== $desiredDisplayType
) {
return $classList;
}
// All checks passed, add my class to body
return array_merge($classList, ['my-class']);
});
Woocommerce has a general option in the customizer where you set up if you want category archives to display subcategories.
Then when you edit a specific category you can assign a display type specific to that category, so this should take precedence over the general option.

Related

Woocommerce category description using shortcode

I want to replace the Woocommerce category description on pages using a shortcode.
With the shortcode I want to dynamically display custom featured image and description for current category.
Here is some code I found from searching here and Google. This code seemed to work but also affected single product pages.
Can someone tell me what is wrong with this please ?
add_filter('woocommerce_short_description', function ($description) {
if (! is_product_category()) { return; }
return do_shortcode('[porto_block id="510079"]');
});
Use the following conditions to make sure it only works on category pages.
add_filter('woocommerce_short_description', function ($description) {
if ( is_product_category() && !is_single() && !is_product()) {
return do_shortcode('[porto_block id="510079"]');
}else{
return $description;
}
});

Extend WooCommerce Product Filters plugin shortocodes

I am using Woocommerce with WOOF plugin (woocommerce filter). In particular, this plugin can display a filter that will only search in a specific product category using for example [woof taxonomies=product_cat:23] shortcode and display the results using the [woof_products taxonomies=product_cat:23] shortcode, where 23 is the category id of goods.
However, it is not always possible to specify a category in the shortcode itself, and I would like to implement functionality that allows you to use a shortcode like [woof taxonomies=product_cat:auto], which will automatically determine the current category using a specific function, for example, this (the function is tested and works):
function show_product_category_id() {
$cat = get_queried_object();
$catID = $cat->term_id;
if (empty($catID)) {
//
if (strpos($_GET['really_curr_tax'], 'product_cat')) {
$catID=str_replace('-product_cat', '', $_GET['really_curr_tax']);
}
else {}
}
else {}
echo $catID;
}
I can, of course, create a shortcode for this function, and add it to the theme's functions.php:
add_shortcode( 'show_product_category_id', 'show_product_category_id' );
and it will work. But I can't use a construction like:
[woof taxonomies=product_cat:[show_product_category_id]]
since nested shortcodes in Wordpress won't work. Therefore, apparently, I need to add to woocommerce the ability to specify not only product_cat:35, but also product_cat:auto.
How can i realize it? Or, is there also a way to use nested shortcodes in wordpress?
Of course that you can't nest shortcodes one in another, but what you can do is embed a shortcode in a another shortcode as follows:
function woof_current_product_category_id() {
$term = get_queried_object();
$term_id = 0; // Initializing
if ( isset($_GET['really_curr_tax']) && false !== strpos( $_GET['really_curr_tax'], 'product_cat' ) ) {
$term_id = (int) str_replace('-product_cat', '', $_GET['really_curr_tax']);
} elseif ( is_a($term, 'WP_Term') ) {
$term_id = (int) $term->term_id;
}
return do_shortcode("[woof taxonomies=product_cat:$term_id]"); // Always use return for a shortcode
}
add_shortcode( 'show_product_category_id', 'woof_current_product_category_id' );
Code goes in functions.php file of the active child theme (or active theme).
So the usage will simply be: [show_product_category_id]

Wordpress the_title filter is duplicating title

I'm running this code:
add_filter( 'the_title', 'ekins_titlefilter', 1, 2);
function ekins_titlefilter( $content, $id = null) {
if(($id !== null) && ($content !== null)) {
if(is_single($id) && in_the_loop()) {
if ( null !== the_field('subscriber_title', $id)) {
return the_field('subscriber_title', $id);
}
}
}
return $content;
}
Which currently should show the "subscriber_title" custom field instead of the regular title (This will eventually only show to logged in subscribed users, but trying to get this basic functionality working first). However its replacing the title correctly, but the subscriber title is also being shown at some random places on the page such as above the header, and on the blog post list its showing the replacement title AND the original title (For example "New TitleOld Title") is there any other checks i can code in that will make it only replace it in two cases, titles on the page itself, and titles on links to go to that page.
This is the problem return the_field('subscriber_title', $id);.
the_field function outputs (echo) the value, you need to change it to get_field

Display Advanced Custom Field for Woocommerce applied to subcategory on parent category

I have an advanced custom field set up to show on a woocommerce subcategory that allows the user to define a colour via the color picker field.
This field will apply that colour to a number of elements related to that sub category (Styling the sub category thumbnail, the product page itself etc).
Im currently using as per the the ACF documentation this code to pull the field in and display it on the subcategory page:
$term = get_queried_object();
$color = get_field('colour', $term); // Get ACF Field
This works fine until it comes to the parent category for the sub pages. I am unable to call the field in for the sub categories of the parent. I understand I need to use get_terms(). I am unable ot get that working though.
This is some code I found which I have added to the loop on content-product_cat.php. However it just breaks the woocommerce loop. What would I need to do to this code to get the parent category page to show all the child subcategories each with its related color field?
// current term
$current_term = get_queried_object();
// child terms
// this returns an array of terms
$args = array(
'taxonomy' => 'YOUR TAXONOMY HERE',
'parent' => $current_term->term_id,
// you may need other arguments depending on your needs
);
$child_terms = get_terms($args);
// you need to maybe loop through the child terms gotte
// to pick which one you want to use
// I'm assuming that you only want to use the first one
$child_term = false; // set it to false to begin with
// we'll use this later
if ($child_terms) {
$child_term = $child_terms[0];
}
// make a decision
if ($child_term) {
// get field value(s) from child term
$color = get_field('color', $child_term);
} else {
// get field value(s) from current term
$color = get_field('color', $current_term);
}
// do something with the values
echo $color;
I found the solution here:
https://wordpress.stackexchange.com/a/341632
add_action('woocommerce_after_subcategory_title', 'wpse_add_custom_text_under_category_title', 10);
function wpse_add_custom_text_under_category_title($category) {
$term_id = 'product_cat_'.$category->term_id;
the_field('krotki_opis_kategorii', $term_id);
}
From Alex Uleberg:
get_queried_object_id on shop archive page it will return the id of the page and not the category. When you use the hook, the $category object will be passed in through the hook.

Some Product Titles on Woocommerce are Not in Order

Please help me.
I'm selling some products in Woocommerce.
I've modified the "sort by" function to make it simple, so it will only sort by price and title. The default sorting is alphabetically (ascending).
But strangely, not all product titles are in order alphabetically. For example, this one:
The "Swe in Black" is supposed to appear before "Tally Ho Fan Back Red", but it appears otherwise.
And this one also:
The last three products are not in order alphabetically. Moreover, there always appears a blank white space before the last product.
Here's my product page link:
cardstory.co/collections
Strange is, this only happens in some browsers (e.g. Firefox, Chrome). But not in Safari.
--
Here's my custom function.php code for product sorting:
<?php
// Edit WooCommerce dropdown menu item of shop page//
// Options: menu_order, popularity, rating, date, price, price-desc
function my_woocommerce_catalog_orderby( $orderby ) {
unset($orderby["popularity"]);
unset($orderby["rating"]);
unset($orderby["date"]);
return $orderby;
}
add_filter( "woocommerce_catalog_orderby", "my_woocommerce_catalog_orderby", 20 );
//Attach function to filter hook
add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');
//add new options to $sortby var passed into filter
function custom_woocommerce_catalog_orderby($sortby) {
$sortby['name_asc'] = "Sort by name: A-Z";
$sortby['name_desc'] = "Sort by name: Z-A";
return $sortby;
}
//Attach our function to the filter hook:
add_filter('woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args');
//Function to handle choices
function custom_catalog_ordering_args($args) {
global $wp_query;
// Changed the $_SESSION to $_GET
if ($_GET['orderby'] == "name_asc") {
$args['orderby'] = 'title';
$args['order'] = "ASC";
} else if ($_GET['orderby'] == "name_desc") {
$args['orderby'] = 'title';
$args['order'] = "DESC";
}
return $args;
}
I've disabled all plugins for this page but nothing works.
Any help is highly appreciated. Thank you! :)

Categories