How sort cart items by price – WooCommerce

Sometime is useful order the cart items by price.

For example, I needed to sort items of cart in a plugin that does discounts like ‘Buy 3 t-shirts get one free’. If there are three t-shirt on cart, the plugin applies the discount at the first t-short. Sorting by price the plugin applies the discount to the shirt cheapest.

The function is the callback of the hook woocommerce_cart_loaded_from_session of WooCommerce. When the the cart is recovered from the session, we collect the cart items in the array $cart sort. This array will be the element sorted by the function uasort.
This function sorts an array such that array indices maintain their correlation with the array elements they are associated with, using a user-defined comparison function.
In the example I used the function wpm_sort_by_price, to sort the array $cart_sort in ascending order. To sort in descending order you should use the function ‘wpm_sort_by_price_desc’.

Change the name to pdf quote in YITH WooCommerce Request a Quote

Plugin info: YITH WooCommerce Request a Quote Premium

With this code you can customize the name of the pdf quote document generated when an administrator send the quote to the customer.

Add this code in the file functions.php of your theme or child-theme, change the string ‘custom_name_’ with the string that you prefer.

The variable $quote_id is necessary to make the document unique inside the folder.


If you want mask the number of quote, you can use php functions like uniqid changing the line 7 with

return uniqid('custom_name_').'.pdf';

Minicart total fix for YITH WooCommerce Dynamic Pricing and Discounts

Plugin info: YITH WooCommerce Dynamic Pricing and Discounts Premium

In some themes the WooCommerce Minicart Widget has an issue in the calculation of totals, because the discounts in the latest product added in the cart are not showed.

This is a fix that calculates the total after that the plugin applies the discounts. Add this code inside the file functions.php of your theme or child-theme.