Skip to content
intermediate Phase 76 · Debugging Advanced

Checkout Debugging

45m
1 problems
Topic Progress 0%

Quote Debugging

Quote Data Inspection

use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Quote\Model\QuoteFactory;

class QuoteDebugger
{
    private $checkoutSession;
    private $quoteFactory;

    public function __construct(
        CheckoutSession $checkoutSession,
        QuoteFactory $quoteFactory
    ) {
        $this->checkoutSession = $checkoutSession;
        $this->quoteFactory = $quoteFactory;
    }

    public function debugCurrentQuote()
    {
        $quote = $this->checkoutSession->getQuote();

        return [
            'quote_id' => $quote->getId(),
            'items_count' => $quote->getItemsCount(),
            'items_qty' => $quote->getItemsQty(),
            'subtotal' => $quote->getSubtotal(),
            'grand_total' => $quote->getGrandTotal(),
            'shipping_address' => $quote->getShippingAddress()->debug(),
            'payment_method' => $quote->getPayment()->getMethod(),
            'coupon_code' => $quote->getCouponCode(),
        ];
    }
}

Quote Issues

// Common quote problems:
// 1. Empty quote after add to cart
// Solution: Check cookie/session configuration

// 2. Quote not saving
// Solution: Check quote object before save()
$quote->setData('key', 'value');
$quote->save(); // Verify save returns true

// 3. Items not loading
// Solution: Force item reload
$quote->load($quote->getId());
$quote->getItemsCollection()->load();

// 4. Price mismatch
// Solution: Recalculate totals
$quote->collectTotals();
$quote->save();

Quote Logging

// Enable quote logging
$logger = $this->objectManager->get(\Psr\Log\LoggerInterface::class);
$logger->info('Quote debug', [
    'quote_id' => $quote->getId(),
    'items' => $quote->getAllItems(),
    'totals' => $quote->getTotals(),
]);

Key Points

  • Check quote ID and items count
  • Verify totals are calculated correctly
  • Log quote data for debugging
  • Clear cart/quote cache when testing

Payment Debugging

Payment Method Debug

use Magento\Payment\Model\Method\Interface as PaymentInterface;

class PaymentDebugger
{
    public function debugPayment($quote)
    {
        $payment = $quote->getPayment();
        
        return [
            'method' => $payment->getMethod(),
            'method_title' => $payment->getMethodInstance()->getTitle(),
            'status' => $payment->getAdditionalInformation('status'),
            'po_number' => $payment->getPoNumber(),
            'cc_type' => $payment->getCcType(),
            'cc_last4' => $payment->getCcLast4(),
        ];
    }
}

Payment Gateway Debug

// Enable payment logging
// app/code/Vendor/Module/Logger/Handler.php
protected $fileName = '/var/log/payment.log';

// Log payment request/response
$logger->info('Payment request', [
    'method' => $method,
    'amount' => $amount,
    'currency' => $currency,
]);

$logger->info('Payment response', [
    'status' => $response->getStatus(),
    'transaction_id' => $response->getTransactionId(),
    'message' => $response->getMessage(),
]);

Common Payment Issues

// 1. Payment method not available
// - Check if method is enabled in admin
// - Verify country/region restrictions
// - Check min/max order amounts

// 2. Payment declined
// - Check gateway credentials
// - Verify test vs production mode
// - Check card details validation

// 3. Payment not saving
// - Check payment model save method
// - Verify quote is not empty
// - Check for exceptions

Key Points

  • Enable payment logging for debugging
  • Check payment method availability
  • Verify gateway credentials
  • Test with sandbox/test mode first

Checkout Flow Debugging

Checkout Steps

// Checkout flow:
// 1. Cart -> Shipping Address
// 2. Shipping Address -> Shipping Method
// 3. Shipping Method -> Payment Method
// 4. Payment Method -> Order Review
// 5. Order Review -> Place Order

// Debug each step
use Magento\Checkout\Model\Type\Onepage;

class CheckoutDebugger
{
    public function debugStep($step)
    {
        $onepage = $this->objectManager->get(Onepage::class);
        
        switch ($step) {
            case 'shipping':
                return $onepage->getQuote()->getShippingAddress()->debug();
            case 'shipping_method':
                return $onepage->getQuote()->getShippingMethod();
            case 'payment':
                return $onepage->getQuote()->getPayment()->debug();
        }
    }
}

Checkout Session Debug

// Check checkout session data
$session = $this->objectManager->get(
    \Magento\Checkout\Model\Session::class
);

$debug = [
    'quote_id' => $session->getQuoteId(),
    'step' => $session->getStepData(),
    'last_real_order_id' => $session->getLastRealOrderId(),
    'additional_data' => $session->getAdditionalData(),
];

Common Checkout Errors

// 1. "No shipping methods available"
// - Check shipping address is complete
// - Verify shipping methods enabled
// - Check carrier configuration

// 2. "Invalid payment method"
// - Verify payment method is available
// - Check quote totals
// - Review payment configuration

// 3. "Order could not be created"
// - Check inventory availability
// - Verify quote is not empty
// - Review exception logs

Key Points

  • Debug each checkout step separately
  • Check session data between steps
  • Review exception.log for errors
  • Test with different scenarios

JavaScript Debugging

Checkout JavaScript

// RequireJS module debug
require(['Magento_Checkout/js/view/payment'], function(viewPayment) {
    console.log('Payment view loaded:', viewPayment);
});

// Customer data debug
require(['Magento_Customer/js/customer-data'], function(customerData) {
    console.log('Cart data:', customerData.get('cart')());
    console.log('Customer data:', customerData.get('customer')());
});

// Checkout data debug
require(['Magento_Checkout/js/model/quote'], function(quote) {
    console.log('Quote:', quote);
    console.log('Shipping address:', quote.shippingAddress());
    console.log('Payment method:', quote.paymentMethod());
});

Common JS Errors

// 1. "TypeError: Cannot read property of undefined"
// Solution: Check if element exists before access
var element = document.querySelector('.element');
if (element) {
    // Do something
}

// 2. "ReferenceError: X is not defined"
// Solution: Ensure RequireJS module is loaded
require(['module'], function(Module) {
    // Use Module
});

// 3. "Network request failed"
// Solution: Check API endpoint and authentication
fetch('/rest/V1/checkout', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'X-Requested-With': 'XMLHttpRequest'
    }
});

Key Points

  • Use browser console for JS debugging
  • Check RequireJS modules are loaded
  • Verify AJAX requests in Network tab
  • Handle async operations properly

Practice Problems

0 / 1 solved
Debug Checkout Issue

Debug a checkout issue where the payment method is not available.

Solution
// Debug steps:

// 1. Check payment method configuration
$paymentMethod = $this->paymentHelper->getMethodInstance($code);
if (!$paymentMethod->isAvailable()) {
    $logger->warning('Payment method not available', [
        'code' => $code,
        'reason' => $paymentMethod->getNotAvailableReason(),
    ]);
}

// 2. Verify quote totals
$quote = $this->checkoutSession->getQuote();
$logger->info('Quote totals for payment check', [
    'subtotal' => $quote->getSubtotal(),
    'grand_total' => $quote->getGrandTotal(),
    'currency' => $quote->getQuoteCurrencyCode(),
]);

// 3. Check min/max amounts
if ($quote->getGrandTotal() < $paymentMethod->getConfigData('min_order_total')) {
    $logger->warning('Below minimum order amount');
}

// 4. Review exception.log for errors

Quiz

1. How do you debug empty cart?

Question 1 options

2. Where are payment logs stored?

Question 2 options

3. What causes "No shipping methods"?

Question 3 options

4. How to access checkout quote in JS?

Question 4 options

Flashcards

Question

Empty cart debug?

Answer

Check quote ID, items, session data

Question

Payment log location?

Answer

var/log/payment.log

Question

No shipping methods cause?

Answer

Incomplete shipping address or config

Question

Access quote in JS?

Answer

require(['Magento_Checkout/js/model/quote'])

Revision Notes

Key Takeaways

  • 1. Debug quote data to find cart issues
  • 2. Enable payment logging for gateway debugging
  • 3. Check each checkout step separately
  • 4. Use RequireJS for JavaScript debugging

Interview Tips

  • Explain the checkout flow
  • Discuss common payment issues
  • Know how to debug quote and cart

Cheat Sheet

Checkout Debugging

  • Quote: check ID, items, totals
  • Payment: var/log/payment.log
  • Shipping: check address config
  • JS: require(['Magento_Checkout/js/model/quote'])