Include only specific categories in WooCommerce product categories widget - wordpress

I'm using WooCommerce plugin for Wordpress. It comes with a widget called WooCommerce Product Categories which can display a drop-down of all your product categories. I have searched online and found the following code which will exclude certain categories from appearing, categories with ID 16 and 20 in this snippet:
add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args' );
function woo_product_cat_widget_args( $cat_args ) {
$cat_args['exclude'] = array('16','20');
return $cat_args;
}
What I need is the opposite. I want a filter/function similar to above, but which enables me to specify which categories to include - i.e. exclude everything but the IDs that I specify.

You can try this;
add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args' );
function woo_product_cat_widget_args( $cat_args ) {
$cat_args['include'] = array('16','20');
return $cat_args;
}
actually you can use any of these arguments that listed on this page https://codex.wordpress.org/Template_Tags/wp_list_categories
Hope that helps!

Related

How do I add product attributes to the product page in WooCommerce?

I'm using WooCommerce and Elementor Pro. Is there any way to add specific product attributes (e.g. gross and net weight) below the Add to cart button?
It seems like an obvious thing but I haven't found options or snippets for it.
First you add attributes to product. Then you use this snippet in functions.php:
// maybe remove tab with attributes
add_filter( 'woocommerce_product_tabs', 'remove_info', 100, 1 );
function remove_info( $tabs ) {
unset($tabs['additional_information']);
return $tabs;
}
// show attributes
add_action( 'woocommerce_single_product_summary', 'summary_attributes', 35 );
function summary_attributes() {
global $product;
if ( $product->has_attributes() ) {
wc_display_product_attributes( $product );
}
}
Setting Up WooCommerce Attributes
go to Products > Attributes
Add the attribute name
Slug will automatically be created, and we can leave the rest of these options untouched.
Click “Add attribute” button and your attribute will be added.
Configure The Attributes
After creating attribute, now we can add different variation on your product. Click on the Configure Terms option.
Enter the variation
Add some short description
Click on the add new tab
To understand it in a better way you can follow this tutorial

Woocommerce - Group by first letter

I am trying to display the products in my Shop page like that:
A
Asd
Asddd
B
Beer
Bear
and so on. I managed to do this for the categories by overriding and using the woocommerce_output_product_categories action and for them it works, but I want to do this for the products as well(since Woocommerce gives you the option to show products or categories in the shop page). Thank you!
There can be so many possible solutions. But for me this can be done like this:
add_action( 'woocommerce_shop_loop', 'wc_shop_loop', 30 );
function wc_shop_loop() {
global $product, $last_title_first_letter_95845949545454;
$title = $product->get_title();
if ( $last_title_first_letter_95845949545454 !== $title[0] ) {
$last_title_first_letter_95845949545454 = $title[0];
woocommerce_product_loop_end(); // let's close the loop.
echo '<h3>'.$last_title_first_letter_95845949545454. '</h3>'; // add a letter heading.
woocommerce_product_loop_start(); // open a new loop start.
}
}
Tested to work on shop page and product category page.
You will need to work on its css.

Remove product-category slug from Woocommerce

I'm having problems with my pagination. The root of the problem is that the product categories goes down 3 levels.
I used the script from the timersys gist: https://gist.github.com/timersys/78a2305d810c27efcb10ce62712d1d95
Then to accommodate the 3rd level of the categories I added the following:
add_filter('request', function( $vars ) {
global $wpdb;
if(isset($vars['error'])){
$lastVar = basename($_SERVER['REQUEST_URI']);
$vars['attachment'] = $lastVar;
}
//rest of the code
}
from here: https://wordpress.stackexchange.com/questions/258053/remove-product-category-slug-plugin-works-with-1-subcategroy-not-with-2/258085#258085
So everything is working and filtering correctly now except when I change pages on the pagination, it takes me to a single product instead of the next page.

How to sort woocommerce categories & products without plugins?

I have about 400+ products categories & 5000+ Products in my woocommerce list and I modify them regularly so can't use "category and Taxonomy order" or similar plugins and it must be done programmatically.
Need to sort categories and products by name (title).
I'm trying to use this code but it's not working:
add_filter('woocommerce_shortcode_products_query', 'my_woocommerce_catalog_orderby');
function my_woocommerce_catalog_orderby( $args ) {
$args['orderby'] = 'title'; // or name, etc
$args['order'] = 'asc';
return $args;
}
Also not this one:
add_filter('woocommerce_get_catalog_ordering_args', 'my_woocommerce_catalog_orderby');
...
Woocommerce products settings, widgets, etc. didn't work for me.
I saw some similar questions but not working answer.
Any idea?
After a few days struggling I came up with a total solution for woocommerce sorting.
I found a part of solution here for products to make a custom sort option:
https://docs.woocommerce.com/document/custom-sorting-options-ascdesc/
Which adds an option to woocommerce catalog sorting list. To change it check:
Woocommerce Settings -> Products Tab -> Display -> Default Product Sorting
and select your desired sorting option which you added using the solution above.
Then, for categories (or actually sub categories), add the code below to functions.php :
add_filter( 'woocommerce_product_subcategories_args', 'custom_woocommerce_get_subcategories_ordering_args' );
function custom_woocommerce_get_subcategories_ordering_args( $args ) {
$args['orderby'] = 'title';
return $args;
}

Woocommerce choose grid or list view for each category

I want the possibility to choose between grid and list view for each Woocommerce category. I have found this plugin: https://nl.wordpress.org/plugins/woocommerce-grid-list-toggle/
However, the plugin is meant for the shopper to choose whether to display items in grid view or list view. What I truly want is the ability to assign a view for each category in the back-end.
Example:
Category A is displayed as grid
Category B is displayed as list
Breaking my head over this.
Similar to this question you need to filter template_include. You need to call your custom archive template archive-list-view.php and save it in your theme's woocommerce folder. Obviously, you can name it anything you like, you will just have to adjust the code below to match.
Folder structure:
/theme-folder/functions.php
/theme-folder/woocommerce/archive-list-view.php
On the template_include filter we will check if we are on the term archive for the nussmylch product category. If so, we'll look for and supply the new template. Otherwise, the standard template is used.
EDIT: incorrect WooCommerce function is_product_taxonomy() was used. is_product_category() is needed to check for a specific category.
add_filter( 'template_include', 'so_33615903_custom_category_template', 20 );
function so_33615903_custom_category_template( $template ) {
// check you are on the taxonomy archive for specific category
if ( is_product_category( 'nussmylch' ) ) {
$new_template = locate_template( array( 'woocommerce/archive-list-view.php' ) );
if ( '' != $new_template ) {
$template = $new_template ;
}
}
return $template;
}
Working Example

Resources