Problema de error fatal con woocommerce_thankyou hooked function [duplicate]

Oct 05 2020

Necesito imprimir diferentes mensajes según el método de pago en la página de agradecimiento de WooCommerce.

Estoy usando el siguiente código, pero bloquea mi sitio web y me muestra el siguiente error:

Error fatal: Error no detectado: llamada a una función miembro get_payment_method () en 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>';

    }
    
}

¿Alguien que pueda decirme dónde van mal las cosas?

Respuestas

2 7uc1f3r Oct 05 2020 at 15:15

A través del woocommerce_thankyougancho tiene acceso al $order_id, no al $orderobjeto en sí, de ahí el error.

Para obtener el $orderobjeto se puede utilizar wc_get_order( $order_id );donde mediante $order_idel $orderse obtiene el objeto.

Entonces obtienes:

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