Неустранимая ошибка с перехваченной функцией woocommerce_thankyou

Мне нужно распечатать разные сообщения в зависимости от способа оплаты на странице благодарности WooCommerce.

Я использую приведенный ниже код, но он приводит к сбою моего веб-сайта и показывает следующую ошибку:

Неустранимая ошибка: необработанная ошибка: вызов функции-члена get_payment_method() на int...

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>';

    }
    
}

Кто-нибудь, кто может сказать мне, где что-то идет не так?


person Community    schedule 05.10.2020    source источник


Ответы (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 );
person 7uc1f3r    schedule 05.10.2020