How to sort woocommerce categories & products without plugins? - wordpress

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;
}

Related

Exclude Backorder Products from WooCommerce Composite Options

I'm using WooCommerce Composite Products to create customer configurable packages from existing items in our Woo shop. These packages need to consist of In Stock products only, ignoring anything that is set to backorder.
For "Composite Options" I am using product categories (we have too many products to add individually), however doing it this way adds all products that are currently on backorder and set to "Allow, but notify customer".
Ideally I need a solution that allows me to use product categories which ignores any products that are set to backorder.
I tried to edit an existing snippet which would create a filter for backorder products but was not able to make any difference to the front end visible products, I'm sure more needs to be done to make it work. This is the original snippet I edited:
add_filter( 'woocommerce_composite_component_options_query_args_current', 'sw_cp_exclude_out_of_stock_options' );
function sw_cp_exclude_out_of_stock_options( $args ) {
$args[ 'exclude_out_of_stock' ] = true;
return $args;
}
I'm pretty sure the following is the basis of the code needed to figure out how to exclude back order products from component options (taken from Composite Products Plugin Editor):
woocommerce composite products > includes > data > class-wc-product-composite-data-store-cpt.php Line 1040 + 1041:
`// See 'WC_CP_Component::exclude_out_of_stock_options'.
'exclude_out_of_stock' => false`
woocommerce composite products > includes > class-wc-cp-component.php Line 785 - 789:
` /**
* Controls whether out of stock component options should be hidden.
*
* #since 8.0.3
*
* #return boolean
*/
public function exclude_out_of_stock_options() {
return apply_filters( 'woocommerce_component_options_exclude_out_of_stock', 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ), $this );
}`
Can anyone help me with the automated solution to this problem?

WooCommerce Upsells Same Order as Admin

really hoping someone can help with this as I thought it would be far more simple!
Long story short, I have created a script that populates the upsells of WooCommerce products, all works great using the API and they are there. They show on the product page as expected but in a completely different order to how they were inputted in the admin area and I cannot seem to find a way for the order to follow admin?
function filter_woocommerce_upsells_orderby( $orderby ) {
return 'menu_order';
};
add_filter( 'woocommerce_upsells_orderby', 'filter_woocommerce_upsells_orderby', 10, 1 );
Above is the hook I have found but from the options I have found such as menu order / id / price etc, there is not simply an overide option to ignore the order and just take them as they are in admin!?
Please help!
I also encountered this problem. And i have an idea.
I checked all wordpress parameters about Order & Orderby.Link is https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters.
And i use the paramteters name "none",It can make your orderby not based on any sorting rules.In other words, it is sorted according to upsells. here is the code.
// ORDER BY
add_filter( 'woocommerce_upsells_orderby', 'filter_woocommerce_upsells_orderby', 10, 1 );
function filter_woocommerce_upsells_orderby( $orderby ){
return "none";
};
// ORDER
add_filter( 'woocommerce_upsells_order', 'filter_woocommerce_upsells_order', 10, 1 );
function filter_woocommerce_upsells_order(){
return 'asc'; // Default is 'desc';
};
But it is still chaotic, when adding any product to upsells, it is still random. Therefore I also used a plug-in "WooCommerce Drop/Drag For Upsells Cross-Sells", which allows you to drag your products in upsell at will.
If you have any question you can ask me.
Thanks.

Add the same product more than once, in an order from the admin panel

I need to be able to add the same product more than once within an order in WooCommerce.
This is the scenario.
I create an order manually from the admin panel of Woocommerce, and I need to be able to add the same product N times to this order.
Basically I need something like what this snippet does, but for the backend instead of for the frontend cart.
function separate_individual_cart_items( $cart_item_data, $product_id ) {
$unique_cart_item_key = md5( microtime() . rand() );
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'separate_individual_cart_items', 10, 2 );
Any suggestion to get this?
Thank you.
In order to duplicate product N times you can add any woocommerce clone duplicate plugin.
You can download a plugin on link to accomplish your task

Include only specific categories in WooCommerce product categories widget

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!

How to sort products by "featured" and price (ascending)?

I am using Woocommerce Sort & Display Plugin in order to display all the products and categories on one unique page. It is working fine, however I cannot seem to sort products in each category as such: featured product first and then price (ascending).
In Woocommerce Sort & Display Plugin settings, I have set option "Product Sort" as "Featured". So my featured products appear in first positions. But what about the next products? How are they sorted? They do not seem to follow any logic as last modified, ID or anything else.
Finally found a solution to this issue.
If you want to be able to manually sort products with Drag & Drop when Product Sort & Display Plugin is active:
Search in file "woocommerce-product-sort-and-display/classes/class-wc-psad.php" for:
$orderby = 'date';
$order = 'DESC';
replace both lines with:
$orderby = 'menu_order';
$order = 'ASC';

Resources