Skip to content

Commit

Permalink
Merge pull request #124 from degica/fix/skipCreateSessionWhenCartEmpty
Browse files Browse the repository at this point in the history
Skip creating checkout session when the cart is empty
  • Loading branch information
Dinwy authored Dec 18, 2024
2 parents 2855300 + 7ed4a52 commit 38f8c3c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions includes/class-wc-gateway-komoju-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function get_payment_method_script_handles()

public function get_payment_method_data()
{
if (!is_admin() && (is_null(WC()->cart) || WC()->cart->is_empty())) {
return;
}

// We lazily fetch one session to be shared by all payment methods with dynamic fields.
static $checkout_session;
if (is_null($checkout_session)) {
Expand Down
10 changes: 8 additions & 2 deletions includes/class-wc-gateway-komoju-single-slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,22 @@ public function create_session_for_fields()
{
$komoju_api = $this->komoju_api;
$currency = get_woocommerce_currency();
$order_total = 0;

if (WC()->cart) {
$order_total = $this->get_order_total();
}

$session_params = [
'amount' => self::to_cents($this->get_order_total(), $currency),
'amount' => self::to_cents($order_total, $currency),
'currency' => $currency,
'default_locale' => self::get_locale_or_fallback(),
'metadata' => [
'woocommerce_note' => 'This session is only for rendering inline fields, and will not be completed.',
],
];

if ($this->get_order_total() == 0) {
if ($order_total == 0) {
return null;
}

Expand Down
8 changes: 6 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;

/*
* Plugin Name: KOMOJU Payments
* Plugin URI: https://github.com/komoju/komoju-woocommerce
Expand Down Expand Up @@ -130,7 +133,7 @@ function register_komoju_payment_method_type()

add_action(
'woocommerce_blocks_payment_method_type_registration',
function ($payment_method_registry) {
function (PaymentMethodRegistry $payment_method_registry) {
$gateways = WC()->payment_gateways()->payment_gateways();

if ($gateways) {
Expand All @@ -140,6 +143,7 @@ function ($payment_method_registry) {
}
}
}
});
}
);
}
}
1 change: 1 addition & 0 deletions uninstall.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* WooCommerce Komoju Payment Gateway
* Uninstall - removes all options from DB when user deletes the plugin via WordPress backend.
Expand Down

0 comments on commit 38f8c3c

Please sign in to comment.