完全禁用WooCommerce端點
P粉596191963
P粉596191963 2023-08-27 23:22:49
0
2
530
<p>我上網搜尋了很多,但還沒找到答案。 所以我在這裡依賴專家們。 </p> <p>我想停用一些WooCommerce的端點。網路上告訴我可以透過<code>woocommerce_account_menu_items</code>鉤子取消設定WooCommerce選單項,如下所示:</p> <pre class="brush:php;toolbar:false;">add_filter ( 'woocommerce_account_menu_items', 'my_remove_my_account_links' ); function my_remove_my_account_links( $menu_links ){ /*** 取消註解對應的行以刪除特定的 * WooCommerce 我的帳戶畫面中的端點。*/ //unset( $menu_links['dashboard'] ); // Remove Dashboard //unset( $menu_links['edit-address'] ); // Addresses //unset( $menu_links['payment-methods'] ); // Remove Payment Methods //unset( $menu_links['orders'] ); // Remove Orders //unset( $menu_links['downloads'] ); // Disable Downloads //unset( $menu_links['edit-account'] ); // Remove Account details tab //unset( $menu_links['customer-logout'] ); // Remove Logout link return $menu_links; }</pre> <p>但這裡的一個大問題是,這只是在前端刪除了選單連結。 我仍然可以透過直接URL存取取消設定的端點。所以當我輸入<code>https://example.de/myaccount/[unset-endpoint]</code>時,我仍然可以存取內容。 </p> <p>我找到了一種透過直接網站存取重定向的方法。我使用了位於付款方式範本(/woocommerce/templates/myaccount/payment-methods.php)中的鉤子<code>woocommerce_before_account_payment_methods</code>來重定向回儀表板:</p> <pre class="brush:php;toolbar:false;">function redirect_forbidden_​​access_account_endpoints(){ wp_redirect(wc_get_account_endpoint_url('dashboard')); } add_action('woocommerce_before_account_payment_methods', 'redirect_forbidden_​​access_account_endpoints');</pre> <p>這個方法非常好用,但只適用於<code>payment-methods</code>端點。我嘗試過對原生的<code>down​​loads</code>端點和自訂端點進行相同的操作,但沒有成功。 </p> <p>所以我的問題是:有沒有一種可靠的解決方案,將從特定禁用的WooCommerce端點的URL訪問重定向到儀表板? </p>
P粉596191963
P粉596191963

全部回覆(2)
P粉725827686

您可以透過以下兩種方式來實現:

  1. 在背景設定中放置空值
    # 前往WooCommerce > 設定 > 高級,然後在帳戶端點輸入框中,您可以刪除特定端點的值並儲存空值。

    透過這種方式,您將不會在帳戶頁面上看到端點頁面或選單項,如果您造訪該URL,您將在造訪的URL上看到主頁。

  2. 取消設定查詢變數
    您可以使用過濾器鉤子取消設定查詢變數。 https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/class-wc-query.php#L85
    在第85行,您可以找到具有所有查詢變數的函數。

    https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/class-wc-query.php#L232
    而在第232行,您可以找到取得查詢變數的函數,它也具有篩選器。您可以使用篩選器並取消設定所需的端點。

    如果您使用此方法,您還需要從導覽選單項目中取消設定該項,您還需要重新儲存固定連結設定。

    然後,如果您造訪該端點的URL,您將在造訪的URL上看到主頁。

在這兩種情況下,您都不會看到404頁面。

P粉217629009

答案是:是的,有!我的鉤子寫錯了。我現在使用了wp鉤。這樣合法嗎?

function redirect_forbidden_access(){
    $current_endpoint = WC()->query->get_current_endpoint();
    if($current_endpoint == "payment-methods" 
      || $current_endpoint == "add-payment-method"
      || $current_endpoint == "edit-payment-method" 
      || $current_endpoint == "[custom-endpoint]")
    {
        wp_redirect(wc_get_account_endpoint_url('dashboard'));
    }
}
add_action('wp', 'redirect_forbidden_access');

這就是解決方法。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!