How hide out of stock products in cross sell products
-
Hi I need to know how hide out of stock products in cross sell products in cart page. I already know we can hide out of stock products via WooCommerce > Settings > Products but that will hide all out of stock products even in their own pages and categories. I specifically need to hide them only in cross sell products which suggest to customers in cart page.
Thanks
-
Hi there ??
It sounds like you may have the option enabled to still sell products on backorder. Please go to the product settings, and check that you have enabled the stock management setting and that the backorders are disallowed.
You can check the following link to know where are these settings located: https://www.screencast.com/t/rtNP1Wteou
If you would like to know more about this topic, please feel free to read our documentation by clicking on the following link: https://docs.woocommerce.com/document/managing-products/#inventory-tab
I hope this helps ??
Thanks for reply but seems everything is in same order you mentioned, the out of stock product which I don’t want to be shown to customer is set on “do not allow” for allow back orders and manage stock is checked and it’s value is 0 because it’s out of stock but still appears as a suggestion to customer in cart page.
- This reply was modified 3 years, 11 months ago by flashsystem.
Hi there,
There’s a post with a snippet for hiding out of stock items in the Related Items section of the Single Product page.
Adjusting it to target the cross-sells template instead appears to work and hide them in that section:
/** * Temporarily enable hide out of stock items. * * @param string $template_name * @param string $template_path * @param bool $located * @param array $args */ function iconic_enable_hide_out_of_stock_items( $template_name, $template_path, $located, $args ) { if( $template_name !== "cart/cross-sells.php" ) { return; } add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', function( $option ) { return "yes"; }, 10, 1 ); } /** * Temporarily disable hide out of stock items. * * @param string $template_name * @param string $template_path * @param bool $located * @param array $args */ function iconic_disable_hide_out_of_stock_items( $template_name, $template_path, $located, $args ) { if( $template_name !== "cart/cross-sells.php" ) { return; } add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', function( $option ) { return "no"; }, 10, 1 ); } add_action( 'woocommerce_before_template_part', 'iconic_enable_hide_out_of_stock_items', 10, 4 ); add_action( 'woocommerce_after_template_part', 'iconic_disable_hide_out_of_stock_items', 10, 4 );
That code should be added to your child theme’s
functions.php
file or via a plugin that allows custom functions to be added, such as the Code Snippets plugin. Please don’t add custom code directly to your parent theme’sfunctions.php
file as this will be wiped entirely when you update.Thanks @kellymetal can u give me the needed code to hide out of stock products for Up-Sells and Cross-Sells specifically?
I’m not sure how to manipulate this code in order to do that.
Hi there,
Please try this to see if it works — just tweaked the check on which template is being loaded, and it looks like it’s working on my test site:
/** * Temporarily enable hide out of stock items. * * @param string $template_name * @param string $template_path * @param bool $located * @param array $args */ function iconic_enable_hide_out_of_stock_items( $template_name, $template_path, $located, $args ) { if( $template_name !== "cart/cross-sells.php" AND $template_name !== "single-product/up-sells.php") { return; } add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', function( $option ) { return "yes"; }, 10, 1 ); } /** * Temporarily disable hide out of stock items. * * @param string $template_name * @param string $template_path * @param bool $located * @param array $args */ function iconic_disable_hide_out_of_stock_items( $template_name, $template_path, $located, $args ) { if( $template_name !== "cart/cross-sells.php" AND $template_name !== "single-product/up-sells.php") { return; } add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', function( $option ) { return "no"; }, 10, 1 ); } add_action( 'woocommerce_before_template_part', 'iconic_enable_hide_out_of_stock_items', 10, 4 ); add_action( 'woocommerce_after_template_part', 'iconic_disable_hide_out_of_stock_items', 10, 4 );
@kellymetal thanks for reply
unfortunately this didn’t work, the out of stock products are still being suggested to customer in cart page.- This reply was modified 3 years, 11 months ago by flashsystem.
Can anyone help? I think it’s a must be feature but lacks in woocommerce.
Hi there,
Please share a copy of your site’s System Status. You can find it via WooCommerce > Status. Select “Get system report” and then “Copy for support”. Once you’ve done that, you can paste it here in your reply.
@kellymetal Here you go, note it’s in Farsi if you needed translation of any part of it tell me, thanks:
### WordPress Environment ### WordPress address (URL): https://flashsystem.ir Site address (URL): https://flashsystem.ir WC Version: 4.8.0 REST API Version: ? 4.8.0 WC Blocks Version: ? 3.8.1 Action Scheduler Version: ? 3.1.6 WC Admin Version: ? 1.7.3 Log Directory Writable: ? WP Version: 5.6 WP Multisite: – WP Memory Limit: 512 ??????? WP Debug Mode: – WP Cron: ? Language: fa_IR External object cache: – ### Server Environment ### Server Info: LiteSpeed PHP Version: 7.3.23 PHP Post Max Size: 512 ??????? PHP Time Limit: 600 PHP Max Input Vars: 10000 cURL Version: 7.72.0 OpenSSL/1.1.1h SUHOSIN Installed: – MySQL Version: 5.7.32 Max Upload Size: 512 ??????? Default Timezone is UTC: ? fsockopen/cURL: ? SoapClient: ? DOMDocument: ? GZip: ? Multibyte String: ? Remote Post: ? Remote Get: ? ### Database ### WC Database Version: 4.8.0 WC Database Prefix: wp_ ????? ?????? ?????? ????: 24.13MB ?????? ???????? ?????? ????: 18.33MB ?????? ???????? ???? ?????? ????: 5.80MB wp_woocommerce_sessions: ????: 0.06MB + ????: 0.00MB + ????? MyISAM wp_woocommerce_api_keys: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_woocommerce_attribute_taxonomies: ????: 0.00MB + ????: 0.01MB + ????? MyISAM wp_woocommerce_downloadable_product_permissions: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_woocommerce_order_items: ????: 0.02MB + ????: 0.01MB + ????? MyISAM wp_woocommerce_order_itemmeta: ????: 0.11MB + ????: 0.08MB + ????? MyISAM wp_woocommerce_tax_rates: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_woocommerce_tax_rate_locations: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_woocommerce_shipping_zones: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_woocommerce_shipping_zone_locations: ????: 0.00MB + ????: 0.01MB + ????? MyISAM wp_woocommerce_shipping_zone_methods: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_woocommerce_payment_tokens: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_woocommerce_payment_tokenmeta: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_woocommerce_log: ????: 0.00MB + ????: 0.00MB + ????? MyISAM ahc_browsers: ????: 0.00MB + ????: 0.00MB + ????? MyISAM ahc_countries: ????: 0.01MB + ????: 0.00MB + ????? MyISAM ahc_hits: ????: 0.00MB + ????: 0.00MB + ????? MyISAM ahc_keywords: ????: 0.00MB + ????: 0.00MB + ????? MyISAM ahc_recent_visitors: ????: 0.00MB + ????: 0.00MB + ????? MyISAM ahc_refering_sites: ????: 0.00MB + ????: 0.00MB + ????? MyISAM ahc_searching_visits: ????: 0.00MB + ????: 0.00MB + ????? MyISAM ahc_search_engines: ????: 0.00MB + ????: 0.00MB + ????? MyISAM ahc_search_engine_crawlers: ????: 0.00MB + ????: 0.00MB + ????? MyISAM ahc_title_traffic: ????: 0.00MB + ????: 0.00MB + ????? MyISAM ahc_visitors: ????: 0.00MB + ????: 0.00MB + ????? MyISAM ahc_visits_time: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_actionscheduler_actions: ????: 0.06MB + ????: 0.11MB + ????? InnoDB wp_actionscheduler_claims: ????: 0.02MB + ????: 0.02MB + ????? InnoDB wp_actionscheduler_groups: ????: 0.02MB + ????: 0.02MB + ????? InnoDB wp_actionscheduler_logs: ????: 0.05MB + ????: 0.03MB + ????? InnoDB wp_berocket_termmeta: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_commentmeta: ????: 0.01MB + ????: 0.01MB + ????? MyISAM wp_comments: ????: 0.12MB + ????: 0.05MB + ????? MyISAM wp_dokan_announcement: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_dokan_orders: ????: 0.02MB + ????: 0.03MB + ????? InnoDB wp_dokan_refund: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_dokan_withdraw: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_ewwwio_images: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_links: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_ms_snippets: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_options: ????: 6.59MB + ????: 0.34MB + ????? MyISAM wp_postmeta: ????: 4.34MB + ????: 0.95MB + ????? MyISAM wp_posts: ????: 1.03MB + ????: 0.13MB + ????? MyISAM wp_revslider_css: ????: 0.10MB + ????: 0.01MB + ????? MyISAM wp_revslider_layer_animations: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_revslider_navigations: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_revslider_settings: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_revslider_sliders: ????: 0.02MB + ????: 0.00MB + ????? MyISAM wp_revslider_slides: ????: 0.09MB + ????: 0.00MB + ????? MyISAM wp_revslider_static_slides: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_rg_form: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_rg_form_meta: ????: 0.25MB + ????: 0.00MB + ????? MyISAM wp_rg_form_view: ????: 0.09MB + ????: 0.04MB + ????? MyISAM wp_rg_incomplete_submissions: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_rg_lead: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_rg_lead_detail: ????: 0.00MB + ????: 0.01MB + ????? MyISAM wp_rg_lead_detail_long: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_rg_lead_meta: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_rg_lead_notes: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_smush_dir_images: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_snippets: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_statistics_exclusions: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_statistics_historical: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_statistics_pages: ????: 0.02MB + ????: 0.06MB + ????? MyISAM wp_statistics_search: ????: 0.38MB + ????: 0.25MB + ????? MyISAM wp_statistics_useronline: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_statistics_visit: ????: 0.01MB + ????: 0.02MB + ????? MyISAM wp_statistics_visitor: ????: 1.86MB + ????: 2.33MB + ????? MyISAM wp_termmeta: ????: 0.02MB + ????: 0.02MB + ????? MyISAM wp_terms: ????: 0.06MB + ????: 0.10MB + ????? MyISAM wp_term_relationships: ????: 0.05MB + ????: 0.09MB + ????? MyISAM wp_term_taxonomy: ????: 0.11MB + ????: 0.05MB + ????? MyISAM wp_tm_taskmeta: ????: 0.02MB + ????: 0.03MB + ????? InnoDB wp_tm_tasks: ????: 0.02MB + ????: 0.02MB + ????? InnoDB wp_usermeta: ????: 0.19MB + ????: 0.11MB + ????? MyISAM wp_users: ????: 0.01MB + ????: 0.02MB + ????? MyISAM wp_wc_admin_notes: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wc_admin_note_actions: ????: 0.02MB + ????: 0.02MB + ????? InnoDB wp_wc_category_lookup: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wc_customer_lookup: ????: 0.05MB + ????: 0.03MB + ????? InnoDB wp_wc_download_log: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_wc_order_coupon_lookup: ????: 0.02MB + ????: 0.03MB + ????? InnoDB wp_wc_order_product_lookup: ????: 0.05MB + ????: 0.06MB + ????? InnoDB wp_wc_order_stats: ????: 0.02MB + ????: 0.05MB + ????? InnoDB wp_wc_order_tax_lookup: ????: 0.02MB + ????: 0.03MB + ????? InnoDB wp_wc_product_meta_lookup: ????: 0.05MB + ????: 0.09MB + ????? InnoDB wp_wc_reserved_stock: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wc_tax_rate_classes: ????: 0.02MB + ????: 0.02MB + ????? InnoDB wp_wc_webhooks: ????: 0.00MB + ????: 0.00MB + ????? MyISAM wp_woocommerce_ir: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wpf_filters: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wpgmza: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wpgmza_categories: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wpgmza_category_maps: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wpgmza_circles: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wpgmza_maps: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wpgmza_polygon: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wpgmza_polylines: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wpgmza_rectangles: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wt_iew_action_history: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wt_iew_cron: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wt_iew_ftp: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_wt_iew_mapping_template: ????: 0.02MB + ????: 0.00MB + ????? InnoDB wp_yoast_indexable: ????: 1.52MB + ????: 0.33MB + ????? InnoDB wp_yoast_indexable_hierarchy: ????: 0.05MB + ????: 0.05MB + ????? InnoDB wp_yoast_migrations: ????: 0.02MB + ????: 0.02MB + ????? InnoDB wp_yoast_primary_term: ????: 0.02MB + ????: 0.03MB + ????? InnoDB wp_yoast_prominent_words: ????: 0.02MB + ????: 0.03MB + ????? InnoDB wp_yoast_seo_links: ????: 0.08MB + ????: 0.03MB + ????? MyISAM wp_yoast_seo_meta: ????: 0.02MB + ????: 0.02MB + ????? MyISAM wp_zhk_updater_logs: ????: 0.13MB + ????: 0.00MB + ????? InnoDB ### Post Type Counts ### attachment: 549 br_product_filter: 1 custom_css: 2 gf_live_preview: 1 nav_menu_item: 36 oembed_cache: 2 page: 19 post: 26 product: 161 product_variation: 131 revision: 31 shop_order: 134 shop_order_refund: 1 us_client: 5 us_footer: 1 us_header: 2 vc_grid_item: 2 ### Security ### Secure connection (HTTPS): ? Hide errors from visitors: ? ### Active Plugins (28) ### ?????? ???: ???? rocketgenius – 2.2.6 ?? ???????: ???? Automattic – 4.1.7 AMP: ???? ??????? ????? AMP – 2.0.9 Classic Editor: ???? WordPress Contributors – 1.6 Code Snippets: ???? Code Snippets Pro – 2.14.0 Sidebar Widgets by CodeLights: ???? CodeLights – 1.4 ?????: ???? ????? – 0.27 Multiple Columns for Gravity Forms: ???? WebHolism – 3.1.5 Global Site Tag Tracking: ???? naa986 – 1.0.1 Glue for Yoast SEO & AMP: ???? Joost de Valk – 0.7 Insert Headers and Footers: ???? WPBeginner – 1.5.0 WPBakery Visual Composer: ???? Michael M - WPBakery.com – 5.2.1 ?????? ??? ?????: ???? ?????? ??? ????? – 2.3.1.4 ?????? ??? ? ??? ???????: ???? MahdiY – 2.2.6 ??????? ?????: ???? ??????? ????? – 4.0.5 PHP Compatibility Checker: ???? WP Engine – 1.5.0 ??????? ??????? ??????? ???? ??? - ????: ???? Torob – 1.2.1 ????? SSL ????: ???? Really Simple Plugins – 4.0.5 Shortcodes Ultimate: ???? Vladimir Anokhin – 5.9.6 String Locator: ???? Clorith – 2.4.2 Styles & Layouts Gravity Forms: ???? Sushil Kumar – 4.2.9 UpSolution Header Builder: ???? UpSolution – 2.0.2 ???????: ???? ???????? – 4.8.0 Yoast SEO Premium: ???? ???? ?????? – 15.1.2 ????????? ?????? ???? - ????????? ??????????? ??: ???? David Anderson Ruhani Rabin Team Updraft – 3.1.6 ???? ?????? ?????: ???? ??? ?????? ????? – 3.0.3 ?????? ?????? ??? ???????? ???? ???????: ???? Masoud Amini – 4.6.1 ??????????? ?????? ?????: ???? ????? – 2.1.1 ### Inactive Plugins (1) ### WooCommerce Boost Sales Premium: ???? Andy Ha (villatheme.com) – 1.4.2.1 ### Dropin Plugins (1) ### advanced-cache.php: advanced-cache.php ### Settings ### API Enabled: ? Force SSL: – Currency: IRT (?????) Currency Position: right_space Thousand Separator: , Decimal Separator: . Number of Decimals: 0 Taxonomies: Product Types: external (external) grouped (grouped) simple (simple) variable (variable) Taxonomies: Product Visibility: exclude-from-catalog (exclude-from-catalog) exclude-from-search (exclude-from-search) featured (featured) outofstock (outofstock) rated-1 (rated-1) rated-2 (rated-2) rated-3 (rated-3) rated-4 (rated-4) rated-5 (rated-5) Connected to WooCommerce.com: – ### WC Pages ### ?????? ???????: #4756 - /shop/ ??? ????: #6810 - /cart/ ????? ????: #4758 - /checkout/ ???? ??????: #6840 - /%d9%88%d8%b1%d9%88%d8%af-%d8%ab%d8%a8%d8%aa-%d9%86%d8%a7%d9%85/ ????? ? ?????: #6293 - /terms/ ### Theme ### Name: IranKala Version: 8.0.5 Author URL: https://sreza-salehi.ir Child Theme: ? – ??? ??? ?? ??? ?????? ? ???? ???? ??????? ?? ????? ???? ????? ??????? ?? ???? ?? ????? ???? ??????? ????. ????? ?? ????? ???? ?????? ?? ?????? WooCommerce Support: ? ### Templates ### Overrides: irankala/woocommerce/archive-product.php irankala/woocommerce/cart/cross-sells.php irankala/woocommerce/cart/mini-cart.php irankala/woocommerce/content-product.php irankala/woocommerce/content-widget-price-filter.php irankala/woocommerce/global/quantity-input.php irankala/woocommerce/loop/orderby.php irankala/woocommerce/loop/rating.php irankala/woocommerce/single-product/related.php irankala/woocommerce/single-product/review-meta.php irankala/woocommerce/single-product/up-sells.php irankala/woocommerce/single-product.php ### Action Scheduler ### ????? ???: 122 Oldest: 2020-11-27 11:29:51 +0000 Newest: 2020-12-28 11:18:14 +0000 ?? ?? ?????: 1 Oldest: 2021-01-03 16:20:55 +0000 Newest: 2021-01-03 16:20:55 +0000
@flashsystem
Many thanks for that.
I note that your theme has a template override for the cross sells and the related items section. These might contain custom code that is displaying the out of stock items.
Please change the theme to a default such as TwentyNineteen or [Storefront](https://woocommerce.com/storefront/) (which is a free theme from WooCommerce.com) and test to see if that solves the issue.I hope that helps you to figure it out.
Feel free to get back to us if you have further questions.@mouli Thanks for reply
So I think best option is to ask the creator of the theme to investigate it because I need this theme.@flashsystem
I think that is a great plan.
My suggestion of temporarily switching the theme wa only to confirm that the theme is causing this before you go to the theme developers themselves.Anyway, I shall close this topic as solved but feel free to open a new one if you need further help.
I came up with this solution that does what you ask for, and also hides the “You may be interested in…” title in case there’s no product to display.
I did override /wp-content/plugins/woocommerce/templates/cart/cross-sells.php, creating a file in my child theme /wp-content/themes/MY-CHILD-THEME/woocommerce/content-product.php, with the following code:
<?php /** * Cross-sells * * This template can be overridden by copying it to yourtheme/woocommerce/cart/cross-sells.php. * * HOWEVER, on occasion WooCommerce will need to update template files and you * (the theme developer) will need to copy the new files to your theme to * maintain compatibility. We try to do this as little as possible, but it does * happen. When this occurs the version of the template file will be bumped and * the readme will list any important changes. * * @see https://docs.woocommerce.com/document/template-structure/ * @package WooCommerce\Templates * @version 4.4.0 */ defined( 'ABSPATH' ) || exit; if ( $cross_sells ) : ?> <?php ob_start(); foreach ( $cross_sells as $cross_sell ) : $stock_status = $cross_sell->get_stock_status(); if ( $stock_status != 'outofstock' ) { // only display product if it is not out of stock $post_object = get_post( $cross_sell->get_id() ); setup_postdata( $GLOBALS['post'] =& $post_object ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited, Squiz.PHP.DisallowMultipleAssignments.Found wc_get_template_part( 'content', 'product' ); } endforeach; $html = ob_get_clean(); if ($html) { // only display the whole cross sells section of there's something to display ?> <div class="cross-sells"> <?php $heading = apply_filters( 'woocommerce_product_cross_sells_products_heading', __( 'You may be interested in…', 'woocommerce' ) ); if ( $heading ) : ?> <h2><?php echo esc_html( $heading ); ?></h2> <?php endif; ?> <?php woocommerce_product_loop_start(); ?> <?php echo $html; ?> <?php woocommerce_product_loop_end(); ?> </div> <?php } ?> <?php endif; wp_reset_postdata();
Hope that helps!
- This reply was modified 3 years, 8 months ago by rAthus. Reason: Fixed typos
- The topic ‘How hide out of stock products in cross sell products’ is closed to new replies.