Skip to content

Fix 'Attempt to read property total on null' warning in Gateway block#584

Open
proteusbr1 wants to merge 1 commit intopagarme:masterfrom
proteusbr1:fix/null-cart-warning
Open

Fix 'Attempt to read property total on null' warning in Gateway block#584
proteusbr1 wants to merge 1 commit intopagarme:masterfrom
proteusbr1:fix/null-cart-warning

Conversation

@proteusbr1
Copy link

Qual o tipo de PR é esse? (marque todos os aplicáveis)

  • Refatoração
  • Adição de funcionalidade
  • Correção de bug
  • Otimização
  • Atualização de documentação

Descrição

Este PR corrige um Warning: Attempt to read property "total" on null reportado no Sentry. O erro ocorria no método getCartTotals quando WC()->cart não estava inicializado (ex: requisições de API).

Alterações realizadas:

  • Adicionada verificação isset(WC()->cart) antes de acessar a propriedade total.
  • Retorno de fallback 0 caso o carrinho não exista.

Cenários testados

  • Cenário de Erro: Verificado que a leitura de propriedade em nulo é evitada com a checagem.

This commit adds a safety check in 'Gateway::getCartTotals' to ensure 'WC()->cart' exists before accessing its 'total' property. This prevents warnings in contexts where the cart is not initialized (e.g., API requests).
Copilot AI review requested due to automatic review settings January 2, 2026 16:41
@backstage-catalog-validator
Copy link

⚠️ Este repositório ainda não está catalogado no Backstage. ⚠️

Por favor, catalogue-o seguindo as instruções nesta documentação. [Via VPN].

💁 Qualquer problema ou dúvida, estamos no Slack, basta abrir um ticket no canal #help-foundation-platform.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a "Attempt to read property 'total' on null" warning in the Gateway block that occurred when WC()->cart was not initialized (e.g., during API requests). The fix adds a null check before accessing the cart's total property and returns 0 as a fallback.

Key changes:

  • Added isset(WC()->cart) check before accessing WC()->cart->total
  • Returns 0 as a fallback value when cart is not initialized
Comments suppressed due to low confidence (1)

src/Block/Checkout/Gateway.php:186

  • The code doesn't check if wc_get_order($orderId) returns null before calling get_total() on it. If the order doesn't exist or cannot be retrieved, this will cause the same "Attempt to read property on null" error that this PR is fixing for the cart scenario. Consider adding a null check and returning a fallback value (e.g., 0) similar to the cart case.
        $order = wc_get_order($orderId);
        return $order->get_total();

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +179 to +182
if (isset(WC()->cart)) {
return WC()->cart->total;
}
return 0;
Copy link

Copilot AI Jan 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this fix prevents the null pointer error, there is no test coverage for the new scenario where WC()->cart is null. Consider adding a test case that verifies the method returns 0 when the cart is not initialized, to prevent regression of this bug fix.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments