Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip creating checkout session when the cart is empty #124

Merged
merged 6 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading