Fix 'Attempt to read property total on null' warning in Gateway block#584
Fix 'Attempt to read property total on null' warning in Gateway block#584proteusbr1 wants to merge 1 commit intopagarme:masterfrom
Conversation
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).
|
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. |
There was a problem hiding this comment.
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 accessingWC()->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 callingget_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.
| if (isset(WC()->cart)) { | ||
| return WC()->cart->total; | ||
| } | ||
| return 0; |
There was a problem hiding this comment.
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.
Qual o tipo de PR é esse? (marque todos os aplicáveis)
Descrição
Este PR corrige um
Warning: Attempt to read property "total" on nullreportado no Sentry. O erro ocorria no métodogetCartTotalsquandoWC()->cartnão estava inicializado (ex: requisições de API).Alterações realizadas:
isset(WC()->cart)antes de acessar a propriedadetotal.0caso o carrinho não exista.Cenários testados