WooCommerce Upsells Same Order as Admin - wordpress

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.

Related

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

Ordering By Menu Order in the Wordpress REST API?

I am trying to display posts using the REST API and I want to display them in menu order based on how they are positioned in the admin menu.
I installed a plugin that allowed me to move posts to any position in the admin. I know querying the posts the normal way with wordpress there is a orderby: menu_order option, which would do what I am looking for, but I can't figure it out with the REST API.
My REST API looks like this:
https://example.com/wp-json/wp/v2/qd_leadership?_embed&per_page=100&orderby=menu_order
So I've tried that and that does not work. It says menu_order isn't an option. I also saw a post here:
Query WordPress (REST) posts by the order they appear on the admin
That had a similar question. The only answer on that post is to not have any orderby parameter and that should display them in the menu order, but that did not work for me. So I am stumped on how to order the posts from the REST API in menu order?
Its bug of wp core in rest api so you could use below hack for the solution.Please add below code in your active theme's function.php
add_filter( 'rest_post_collection_params', 'my_prefix_add_rest_orderby_params', 10, 1 );
function my_prefix_add_rest_orderby_params( $params ) {
$params['orderby']['enum'][] = 'menu_order';
return $params;
}
Tested and works.
Thanks to raju_eww for the hint in the right direction. But in case of a custom post type collection the filter hook name has to be like this:
add_filter( 'rest_custom-post-type_collection_params', 'my_prefix_add_rest_orderby_params', 10, 1 );
function my_prefix_add_rest_orderby_params( $params ) {
$params['orderby']['enum'][] = 'menu_order';
return $params;
}
found here:
https://www.timrosswebdevelopment.com/wordpress-rest-api-post-order/
Following code do sort by menu_order when orderby is not in query string (for woocommerce):
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
if(!$_GET['orderby']) {
$args['orderby'] = 'menu_order';
$args['order'] = 'asc';
}
return $args;
}
source

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 Add Product To Cart Multiple Times But As Different items

I have some custom code that adds an item to the cart, a variable product. If i add the same product twice it just increases the quantity. I would like it to add the product again as a different item in the cart.
How would i go about doing that? The item is being added to the cart using a link like domain.com?add-to-cart=3434&variation_id=4434 and so on.
The system i have developed is a product designer. So i may want to pick the same product but design it in different ways, and then add the same variation to the cart.
Is there also a way to send a unique identifier in the url to split these items up?
I want to do it by using add_to_cart but every time i do that with the variation attributes, the shipping ends up with a bug, it basically can't seem to find shipping methods:
$woocommerce->cart->add_to_cart(522,1, 523,array("attribute_colour" => "colour","attribute_size" => "a3", "attribute_invoice-numbering" => "yes", "attribute_quantity-column" => "yes", "attribute_cc-type" => "duplicate"));
While that code adds the item to the cart, it causes the following:
Warning: Invalid argument supplied for foreach() in /home/****/public_html/wp-content/plugins/woocommerce/includes/class-wc-shipping.php on line 291
There doesn‘t seem to be any available shipping methods. Please double check your address, or contact us if you need any help.
It doesn't make any sense, because the standard add to cart code in woocommerce uses add_to_cart and has none of these issues. The same if i use a URL, it works fine!
Try this !
/*
* #desc Force individual cart item
*/
function force_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','force_individual_cart_items', 10, 2 );
/*
* #desc Remove quantity selector in all product type
*/
function remove_all_quantity_fields( $return, $product ) {
return true;
}
add_filter( 'woocommerce_is_sold_individually', 'remove_all_quantity_fields', 10, 2 );
I can't tell yet if the shipping array issue is related. But, if you take a look at the beginning of the add_to_cart() method in the cart class:
// Generate a ID based on product ID, variation ID, variation data, and other cart item data
$cart_id = $this->generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data );
// See if this product and its options is already in the cart
$cart_item_key = $this->find_product_in_cart( $cart_id );
Basically, what we're seeing here is that the item must be completely unique in order to add it to the cart again. Otherwise, it will just up the quantity of the item already in the cart. So, to get around this, you will want to make the add_to_cart() parameters unique... probably via the final, $cart_item_data array.

how can I detect that option value has changed in wordpress?

I am working on a Wordpress plugin and I want to fire a different action based on each option value change. so how can I do that ?
Example:
$options = get_option('ACP_settings'); if $options['acp-select'] has changed from previous value than I want to fire different action based on selection
Please note that $option['acp-select'] is retrieved from select/option html form and has following values for selection:'book','air','SW','HW' and etc....
I hope I have posted my question clearly.
thanks for help
You can use wordpress hooks in the update_option() function.
In your case you can use updated_option or update_option_{$option} hooks.
The code for ACP_settings option would look like:
add_action( 'update_option_ACP_settings', function($old_value, $value, $option){
if( $old_value !== $value ){
// Your option was updated
// Run your code here
}
}, 10, 3 );

Resources