星期四, 19 6 月, 2025
No Result
View All Result
育心文具行

育心文具行

  • 首頁
  • 心得x體驗
  • 筆記x備忘
  • 閒談x雜記
  • 我的工具箱
  • 關於我
育心文具行
  • 首頁
  • 心得x體驗
  • 筆記x備忘
  • 閒談x雜記
  • 我的工具箱
  • 關於我
No Result
View All Result
育心文具行
Home 筆記x備忘

WordPress 之中藍新金流開啟後, Elementor 外掛有時會打不開的問題處理

2024-01-03
in 筆記x備忘
Reading Time:3 mins read
A A
3

WordPress 有很多外掛,在藍新金流啟用時,同時有使用 Elementor 這個 page builder 外掛,在某些編輯視窗中,可能會造成 AJAX “/wp-json/elementor/v1/globals” 路徑中抓不到 wc_get_chosen_shipping_method_ids() 這個 function 的錯誤訊息。

這邊查了一下資料,不只是藍新金流外掛會這樣,應該 woocommerce 外掛和 page builder 都有可能造成類似的錯誤。紀錄一下避免老了忘記。

— 2024-01-02 update —

最近又遇到一樣的問題,查看 log 發現是 wc_get_chosen_shipping_method_ids() 裡面的問題:

2024/01/02 19:30:32 [error] 1870242#1870242: *46728 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Call to a member function get() on null in /xxx/xxx/wp-content/plugins/woocommerce/includes/wc-cart-functions.php:394
Stack trace:
#0 /xxx/xxx/wp-content/plugins/newebpay/class-newebpay.php(1673): wc_get_chosen_shipping_method_ids()

...

查看 woocommerce 的程式碼(參考),並且參考有類似問題的 stackoverflow:

function wc_get_chosen_shipping_method_ids() {
	$method_ids     = array();
	$chosen_methods = WC()->session->get( 'chosen_shipping_methods', array() );
	foreach ( $chosen_methods as $chosen_method ) {
		$chosen_method = explode( ':', $chosen_method );
		$method_ids[]  = current( $chosen_method );
	}
	return $method_ids;
}

判斷應該是 WC()->session 取不到 get 參數,實際上就是這樣沒錯, woocommerce 當下這個沒有判斷 session 為空的狀況。
可以參考原文,於 class-newebpay.php:1676 附近, wc_get_chosen_shipping_method_ids() 呼叫之前判斷 WC()->session 是否為空。這邊於底下原文中添加新的 code 。

— 以下原文 —

附上錯誤訊息:

Fatal error: Uncaught Error: Call to undefined function wc_get_chosen_shipping_method_ids() in /xxx/wp-content/plugins/newebpay/class-newebpay.php:1676 Stack trace: #0 /xxx/wp-includes/class-wp-hook.php(303): newebpay_alter_payment_gateways(Array) ...

附上發現錯誤的版本資訊,wordpress 核心版本為 5.8.1 , Elementor 版本為 3.4.3:

/**
 * newebpay Payment Gateway
 * Plugin URI: http://www.newebpay.com/
 * Description: 藍新金流收款/物流 模組
 * Version: 1.0.3
 * Author URI: http://www.newebpay.com/
 * Author: 藍新金流 newebpay
 * Plugin Name:   藍新金流
 * @class       newebpay
 * @extends     WC_Payment_Gateway
 * @version
 * @author  Pya2go Libby
 * @author  Pya2go Chael
 * @author  Spgateway Geoff
 * @author  Spgateway_Pay2go Q //20170217 1.0.1
 * @author  Spgateway_Pay2go jack //20170622 1.0.2
 * @author  Spgateway_Pay2go Stally //20180420 1.0.3 20181018 1.0.4 20181222 newebpay 1.0.0 20190417 1.0.1 20190711 1.0.2 20200326 1.0.3
 */

目前的解決方法是在 class-newebpay.php:1676 的 newebpay_alter_payment_gateways() 內添加判斷,檢查是否有方法,沒方法就直接跳回不動作。
另外也在 plugin meta 上修改版本號和描述,避免被直接更新。不過這個外掛也沒上版控,所以屆時可能需要也是手動更新嘍。

附上修改後資訊:

<?php
/**
 * newebpay Payment Gateway
 * Plugin URI: http://www.newebpay.com/
 * Description: 藍新金流收款/物流 模組 修復了 elementor 開啟會有 wc_get_chosen_shipping_method_ids 錯誤的問題 
 * Version: 99.99.99
 * Author URI: http://www.newebpay.com/
 * Author: 藍新金流 newebpay 修改過 By Jerry
 * Plugin Name:   藍新金流
 * @class       newebpay
 * @extends     WC_Payment_Gateway
 * @version
 * @author  Pya2go Libby
 * @author  Pya2go Chael
 * @author  Spgateway Geoff
 * @author  Spgateway_Pay2go Q //20170217 1.0.1
 * @author  Spgateway_Pay2go jack //20170622 1.0.2
 * @author  Spgateway_Pay2go Stally //20180420 1.0.3 20181018 1.0.4 20181222 newebpay 1.0.0 20190417 1.0.1 20190711 1.0.2 20200326 1.0.3
 */
add_action('plugins_loaded', 'newebpay_gateway_init', 0);

function newebpay_gateway_init() {
    if (!class_exists('WC_Payment_Gateway')) {
        return;
    }

    class WC_newebpay extends WC_Payment_Gateway {

 ...

    // 選擇藍新金流超商取貨後 payment只輸出藍新金流
    function newebpay_alter_payment_gateways($list) {
        if(isset($_GET['pay_for_order']) && isset($_GET['key'])) {
            $order_id = wc_get_order_id_by_order_key($_GET['key']);
            $order = wc_get_order($order_id);
            if($order->has_shipping_method('newebpay_cvscom')) {
                $list = array('WC_newebpay');
            }
        } elseif(!is_admin()) { //後台無wc_get_chosen_shipping_method_ids function
            if ( !function_exists( 'wc_get_chosen_shipping_method_ids' ) ) { 
                return $list;
            } 

            // 2024-01-02 更新判斷
            $session = WC()->session;
            if($session){
                $chosen_shipping = wc_get_chosen_shipping_method_ids();
                //判斷購物車內商品是否全為虛擬商品 全為虛擬商品時會無法選擇物流方式 導致session的chosen_shipping會維持上次所選
                $virtual_count = 0;
                $cart_items = WC()->cart->get_cart();
                foreach ($cart_items as $key => $cart_item) {
                    $virtual_count += ($cart_item['data']->is_virtual()) ? 1 : 0;
                }
                if (@in_array('newebpay_cvscom', $chosen_shipping) && $virtual_count < count($cart_items)) {
                    $list = array('WC_newebpay');
                }
            } // end 判斷
        }

        return $list;
    }
    add_filter('woocommerce_payment_gateways', 'newebpay_alter_payment_gateways', 100);

...
Tags: elementornewebpayphpwordpress藍新
Share22Tweet14
Previous Post

Linux SSH 斷線後保持 session 的工具 – Screen

Next Post

WordPress SSL 錯誤修復

Related Posts

筆記x備忘

解決 PHP 的 dompdf 中文字體亂碼的方法

記錄一下一個之前解決過的問題。 在使用 ...

2025-01-03
100
筆記x備忘

Sublime Text3 檢查 Codeigniter4 專案的外掛

使用 Sublime Text 檢查 C...

2024-12-16
100
筆記x備忘

解決 PHP8.2 mb_convert_encoding 方法已經棄用的問題。

PHP 中 mb_convert_enc...

2024-10-27
100
筆記x備忘

Nginx 中同一個網域設定不同應用程式/目錄

技能點還沒有點太多的領域一點點成果就會獲...

2024-08-22
100
筆記x備忘

WordPress 不用外掛置換網域

雖然不是第一次做這件事了,不過還真的不常...

2024-07-30
100
筆記x備忘

ionCube 加密/混淆 PHP for Codeigniter4 筆記

前陣子在搞 Android 的混淆,在第...

2024-03-10
100
Load More
Next Post

Wordpress SSL 錯誤修復

Wordpress 核心架構圖/堆棧圖 相關檔案結構說明鏈結

購物車串接第三方金流 session 會被清空的原因

Comments 3

  1. Bolin Lai says:
    2 年 ago

    兩年後換綠界的外掛也有這個問題,感謝提供解決方案。

  2. JCL says:
    3 年 ago

    這個問題花了我好多時間才發現原來是藍新的問題
    原本不只ELEMENTOR無法開啟,連裡面的CONTACT FORM小工具也會出錯,測試寄不出去,顯示 “這個網站有重大的問題” ><"
    我照你寫的加進去就恢復正常了
    太感謝了!!

    • Jerry Lin says:
      3 年 ago

      不客氣,我也是遇到去找來的答案ww

全站搜尋

No Result
View All Result

關於我

育心文具行

Jerry Lin

程式設計師

育心文具行是一家文具店,老闆的小兒子是一個設計師,職業是設計程式,興趣是設計人,倒是還沒有實作的機會。
--
所有的相遇都不是巧合。
在廣告公司待了三年有兩年多在做內容產品,離職後繼續在內容產業走了四年多,最後選則離開台灣的公司體制在家 SOHO ,碰上了 2020 年疫情才知道原來自己這是走在時代的潮流尖端。 感謝許多老闆和貴人相助,至今目前尚能存活,在台北街頭努力著。

近期文章

  • 於 MacOS 中,整合自然語言,自動化添加行事曆事件
  • 使用 deepwiki-open 針對程式碼產生 WIKI
  • 使用 Tailwind CLI 快速建立靜態 CSS 檔的基本步驟
  • Visual Studio Code 的 PHP CS Fixer 設定
  • MacOS 添加本地伺服器的 DNS

分類

  • 心得x體驗 (45)
  • 筆記x備忘 (77)
  • 閒談x雜記 (13)

贊助買咖啡

Facebook Instagram RSS

標籤

ai android cloudflare flutter git ios javascript linode linux MAC macos mis nginx php plugin QBQ!問題背後的問題 server work sublime sublime text vps vue.js vue3 What if What If? 如果這樣,會怎樣? wordpress 前端 原則 原子習慣 團隊,從傳球開始 團隊,從傳球開始:五百年都難以超越的 UCLA 傳奇教練伍登培養優越人才和團隊的領導心法 壓力測試 外掛 如果這樣,會怎樣?:胡思亂想的搞怪趣問 正經認真的科學妙答 心得 情緒 活動心得 灰階思考 約翰‧漢尼斯 股癌 自慢10 自慢10:18項修練 記錄 謝孟恭 讀書心得 這一生,你想留下什麼?

文章分類

  • 心得x體驗 (45)
  • 筆記x備忘 (77)
  • 閒談x雜記 (13)

近期文章

  • 於 MacOS 中,整合自然語言,自動化添加行事曆事件
  • 使用 deepwiki-open 針對程式碼產生 WIKI
  • 使用 Tailwind CLI 快速建立靜態 CSS 檔的基本步驟
  • Visual Studio Code 的 PHP CS Fixer 設定
  • MacOS 添加本地伺服器的 DNS

近期留言

  • 「Bolin Lai」於〈WordPress 之中藍新金流開啟後, Elementor 外掛有時會打不開的問題處理〉發佈留言
  • 「Jerry Lin」於〈WordPress 之中藍新金流開啟後, Elementor 外掛有時會打不開的問題處理〉發佈留言
  • 「JCL」於〈WordPress 之中藍新金流開啟後, Elementor 外掛有時會打不開的問題處理〉發佈留言

文章月曆

2025 年 6 月
日 一 二 三 四 五 六
1234567
891011121314
15161718192021
22232425262728
2930  
« 5 月    

© 2020 hipster.crazyjerry.studio - a blog about experience, notes and nonsense, by Jerry Lin.

No Result
View All Result
  • 首頁
  • 心得x體驗
  • 筆記x備忘
  • 閒談x雜記
  • 我的工具箱
  • 關於我

© 2020 hipster.crazyjerry.studio - a blog about experience, notes and nonsense, by Jerry Lin.