Customizing My Account Orders list post per page in Woocommerce - php

Woocommerce 2.6.x has a special page at the user account (My Account) area where it displays the user's previous Orders.
This page is now paginated and it displays as default 15 items/page.
Here the screenshot of the woocommerce storefront theme Orders area with 8 lines:
I Can't find the way to change this.
How can I show only 7 items instead of the default number?
Thanks.

Using a custom hooked function in woocommerce_my_account_my_orders_query hook, you can alter the orders query customizing the post_per_page argument to 7, just as you want.
Here is that code:
add_filter( 'woocommerce_my_account_my_orders_query', 'custom_my_account_orders', 10, 1 );
function custom_my_account_orders( $args ) {
$args['posts_per_page'] = 7;
return $args;
}
For woocommerce 3+ use limit instead:
add_filter( 'woocommerce_my_account_my_orders_query', 'custom_my_account_orders', 10, 1 );
function custom_my_account_orders( $args ) {
// Set the post per page
$args['limit'] = 7;
return $args;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Note: Normally the default value for storefront theme and other themes too when displaying the list of orders in my account pages is 10 (but not 15).

Related

Hide add to cart button when product visibility is hidden in WooCommerce

Every night I load in a CSV with my suppliers products. They remove and add products in every CSV. If a product is not in the CSV anymore and it was in the CSV before, my plugin will put the product visibility on hidden. This way the link still works, so no 404 errors in search console etc, but the product is not showing in my shop.
However, some customers still land on these links from different domains, i.e. google. They land on the "invisible product" and they have the possibility to click on the "in cart" button while the product is not available anymore.
Therefor my question: How can I (in functions.php?) make sure that when a product's visibility is hidden, the cart button is removed (a simple display: none; will do).
I use WP Import for importing the CSV and the way products are put on visibility:hidden is like this:
function my_is_post_to_delete($is_post_to_delete, $post_id, $import) {
// Get an instance of the product variation from a defined ID
$my_product = wc_get_product($post_id);
// Change the product visibility
$my_product->set_catalog_visibility('hidden');
// Save and sync the product visibility
$my_product->save();
return false;
}
So I need something like this:
If product_visibility is 'hidden' then remove add to cart button.
You can simply use woocommerce_is_purchasable dedicated filter hook when the product catalog visibility is "hidden", this way:
add_filter('woocommerce_is_purchasable', 'filter_product_is_purchasable', 10, 2 );
function filter_product_is_purchasable( $purchasable, $product ) {
if( 'hidden' === $product->get_catalog_visibility() ) {
$purchasable = false;
}
return $purchasable;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Note: If customer has a previous cart session with the product in it, it will be removed from it.
Update - For external (or affiliate) products try to use the following instead:
add_action( 'woocommerce_single_product_summary', 'remove_product_add_to_cart_button', 4 );
function remove_product_add_to_cart_button(){
global $product;
if( $product->is_type('external') && 'hidden' === $product->get_catalog_visibility() ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Show empty subcategories in WooCommerce 3

I tried this code, it shows root categories, but subcategories without products are still hidden.
function hide_empty_categories ( $hide_empty ) {
$hide_empty = FALSE;
// You can add other logic here too
return $hide_empty;
}
add_filter( 'woocommerce_product_subcategories_hide_empty', 'hide_empty_categories', 10, 1 );
This problem can come from some other customizations you have added yourself, some wrong settings, your main theme customizations or some third party plugin.
Something else is altering your the product categories loop as your code correctly enables to show empty product subcategories in Woocommerce and it's the right hook to be used.
It can be simplified with this simple line of code too:
add_filter( 'woocommerce_product_subcategories_hide_empty', '__return_false' );
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Remove subtotal and Shipping calculator block in Woocommerce cart totals section

In woocommerce, I am using the following to remove the shipping block in cart totals section bloc:
function disable_shipping_calc_on_cart( $show_shipping ) {
if( is_cart() ) {
return false;
}
return $show_shipping;
}
add_filter( 'woocommerce_cart_ready_to_calc_shipping', 'disable_shipping_calc_on_cart', 99 );
But in that cart totals section bloc there are some other things like the cart Subtotal and Total.
I just need to keep only Total
Any help is welcome.
You seem to keep only the total on cart totals section. For that the only way is overriding woocommerce templates.
There is 2 steps:
You should read Template structure & Overriding templates via a theme to understand how you can override woocommerce templates via your active child theme (or active theme).
So you will have to create in you active theme folder (if it doesn't exist yet) a folder named "woocommerce" and a sub-sub folder "cart"…
You will have to copy from the wp-content/plugins/woocommerce/templates/cart/cart-totals.php to your active theme woocommerce/cart/cart-totals.php (if it not exist yet).
Editing cart-totals.phptemplate and removing code:
Since WooCommerce version 2.3.6 and up: You will have to remove the code from line 32 to 59 (You will keep only fees and taxes if they are any)
If you want to keep only the total amount you will remove the code from line 32 to line 87
Then save.
You will not need your function code anymore
Now your cart totals will be something like this:
add_filter( 'woocommerce_get_order_item_totals', 'adjust_woocommerce_get_order_item_totals' );
function adjust_woocommerce_get_order_item_totals( $totals ) {
unset($totals['cart_subtotal'] );
return $totals;
}
Is this what you are looking for? Basically you asked to remove the subtotal.

How to remove attributes from product title in woocomerce order view page? [duplicate]

Ever since we upgraded to Woocommerce version 3 our order confirmations are showing huge titles that include the variation detail. I don't like how it looks and it breaks some important functionalities in some custom-made plugins.
Reference: Order Name Showing Variations since update to WC version 3
There is a filter that can be used to disable this data displaying in the title called woocommerce_product_variation_title_include_attribute_name from what I understand. But I have no idea where to apply the filter.
Is there a quick way to apply the filter to change it back to display as it did before?
This filter should work returning a false value for $should_include_attributes first argument in woocommerce_product_variation_title_include_attributes filter hook this way:
add_filter( 'woocommerce_product_variation_title_include_attributes', 'custom_product_variation_title', 10, 2 );
function custom_product_variation_title($should_include_attributes, $product){
$should_include_attributes = false;
return $should_include_attributes;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
It should just work as you expect.
Update: The shorter way is:
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
just works too.
A quick gotcha if you're using this filter to remove attributes from e-mail items. It appears that once an item has been written to an order the properties of it will not change.
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
As #freemason_17 pointed out, #LoicTheAztec's answer could potentially hide those details at other places as well so just to be sure I added a condition that would limit this to the cart page alone:
function custom_product_variation_title($should_include_attributes, $product){
if(is_cart()) {
$should_include_attributes = false;
return $should_include_attributes;
}
}
add_filter( 'woocommerce_product_variation_title_include_attributes', 'custom_product_variation_title', 10, 2 );
I use this:
/* -------------------- Remove variation names from title ------------------- */
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );

Changing "return to shop" URL for all languages with WPML plugin

In my WooCommerce Web shop I would like to change the "Return to shop" URL to a Custom URL. I tried to use the code below in the function.php file of my active theme, but it doesn't work.
On my website, I have five active languages managed by WPML commercial plugin. It also runs a script which makes sure that visitors from these countries are redirected to their own language.
/**
* Changes Return to Shop button URL on Cart page.
*
*/
function wc_empty_cart_redirect_url() {
return 'http://pacsymposium.com/';
}
add_filter( 'woocommerce_return_to_shop_redirect', 'wc_empty_cart_redirect_url' );
How can I make this working to get the current language shop link?
Thanks.
Update2: In your code, you need to use:
WooCommerce wc_get_page_id() function to get the WooCommerce shop page ID.
WPML wpml_object_id filter hook to get the current language translated page ID for shop.
WooCommerce wc_get_page_permalink() that is used by the filter hook itself (see HERE)
With that material, you can get the current translated link of the shop (or any other link).
So your code is going to be:
add_filter( 'woocommerce_return_to_shop_redirect', 'wc_empty_cart_redirect_url' );
function wc_empty_cart_redirect_url() {
// Getting the shop ID
$shop_id = wc_get_page_id( 'shop' );
// Getting the current language ID for the shop page
$current_lang_id = apply_filters( 'wpml_object_id', $shop_id, 'page', TRUE );
// Getting the post object for the ID
$post = get_post($current_lang_id);
// Getting the slug from this post object
$slug = $post->post_name;
// We re-use wc_get_page_permalink() function just like in this hook
$link = wc_get_page_permalink( $slug );
return $link;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Finally I tested and it works…

Categories