【问题标题】:Fatal error issue with woocommerce_thankyou hooked function [duplicate]woocommerce_thankyou 挂钩函数的致命错误问题 [重复]
【发布时间】:2021-01-20 01:23:48
【问题描述】:

我需要在 WooCommerce 感谢页面上根据付款方式打印不同的消息。

我正在使用下面的代码,但它使我的网站崩溃并显示以下错误:

致命错误:未捕获的错误:调用 int 上的成员函数 get_payment_method()...

add_action( 'woocommerce_thankyou', 'bbloomer_add_content_thankyou' );
 
function bbloomer_add_content_thankyou($order) {
         if( 'bacs' == $order->get_payment_method() ) {

echo '<h2 class="h2thanks">Get 20% off</h2><p class="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase!</p>';
         }
    else{
        echo '<h2 class="h2thanks">Get 100% off</h2><p class="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase!</p>';

    }
    
}

谁能告诉我哪里出了问题?

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce payment-method


    【解决方案1】:

    通过woocommerce_thankyou 挂钩,您可以访问$order_id,而不是$order 对象本身,因此会出现错误。

    要获取$order 对象,您可以使用wc_get_order( $order_id ); 其中通过$order_id 获得$order 对象。

    所以你得到:

    function bbloomer_add_content_thankyou( $order_id ) {    
        // Get $order object
        $order = wc_get_order( $order_id );
        
        // Is a WC_Order
        if ( is_a( $order, 'WC_Order' ) ) {
            // Payment method = bacs
            if( $order->get_payment_method() == 'bacs' ) {
                echo '<h2 class="h2thanks">Get 20% off</h2><p class="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase!</p>';
            } else {
                echo '<h2 class="h2thanks">Get 100% off</h2><p class="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase!</p>';
            }
        }
    }
    add_action( 'woocommerce_thankyou', 'bbloomer_add_content_thankyou', 10, 1 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多