WooCommerce 隱藏(關閉) 購物車、結帳功能 僅展示商品方法

Wordpress / 2023-01-30

前言

最近接到一個 Case ,因為網站的結帳金額較大,手續費被扣的相當不划算,加上擔心金流風險,決定在重新架設的時候關閉相關功能,僅展示商品即可,以下將提供幾種方法來達到實際效果

操作方法

選擇以下一種適合您的方法,並將代碼貼至(子)主題的 functions.php 即可

方法一(代碼隱藏)

結帳及購物車功能仍會保留,僅隱藏購買按鈕

add_action( 'init', 'woocommerce_hide_buy_button' );
function woocommerce_hide_buy_button(){
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}

方法二(代碼徹底關閉,擇一即可)

代碼1:將產品視為不可購買狀態

add_filter( 'woocommerce_is_purchasable', '__return_false');

代碼2:移除價格、結帳重定向…等

add_action( 'template_redirect', 'sola_wc_catalog_mode' );
  
function sola_wc_catalog_mode() {
    // Do nothing if the logged in user is a administrator
    if( is_user_logged_in() && current_user_can('manage_options') ){
        return;
    }
    // Remove add to cart button from archive pages and single product pages
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    
    // Redirect cart and checkout pages to the shop page
    if( is_cart() || is_checkout() ){
        wp_redirect(wc_get_page_permalink('shop'));
        exit;
    }
    // Hide all prices, including the products in a widget
    add_filter( 'woocommerce_get_price_html', function(){
        return '';
    });
    // Remove orders and downloads items from dashboard links
    add_filter ( 'woocommerce_account_menu_items', function( $menu_items ){
        unset( $menu_items['orders'] );
        unset( $menu_items['downloads'] );
        return $menu_items;
    });
}

方法三(第三方插件)

ELEX WooCommerce Catalog Mode

安裝後至 WooCommerce 設定中找到 Catalog Mode 即可將商店改為展示模式
螢幕擷取畫面 2024-01-28 011139