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

Add support for High Performance Order Storage #144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
30 changes: 24 additions & 6 deletions includes/class-wc-background-swedbank-pay-queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SwedbankPay\Payments\WooCommerce;

use Automattic\WooCommerce\Utilities\OrderUtil;
use WC_Background_Process;
use WC_Logger;

Expand Down Expand Up @@ -246,13 +247,30 @@ public function dispatch_queue() {
*/
private function get_post_id_by_meta( $key, $value ) {
global $wpdb;

return $wpdb->get_var(
$wpdb->prepare(
"SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = %s AND meta_value = %s;",
$key,
$value
if ( ! OrderUtil::custom_orders_table_usage_is_enabled() ) {
return $wpdb->get_var(
$wpdb->prepare(
"SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = %s AND meta_value = %s;",
$key,
$value
)
);
}
$orders = wc_get_orders(
array(
'return' => 'ids',
'limit' => 1,
'meta_query' => array(
array(
'key' => $key,
'value' => $value,
),
),
)
);
if ( ! empty( $orders ) ) {
return (string) $orders[0];
}
return null;
}
}
8 changes: 5 additions & 3 deletions includes/class-wc-gateway-swedbank-pay-cc.php
Original file line number Diff line number Diff line change
Expand Up @@ -986,9 +986,10 @@ public function thankyou_page( $order_id ) {
$token->save();

// Replace token
delete_post_meta( $order->get_id(), '_payex_replace_token' );
delete_post_meta( $order->get_id(), '_payment_tokens' );
$order->delete_meta_data( '_payex_replace_token' );
$order->delete_meta_data( '_payment_tokens' );
$order->add_payment_token( $token );
$order->save();

wc_add_notice( __( 'Payment method was updated.', 'swedbank-pay-woocommerce-payments' ) );

Expand Down Expand Up @@ -1078,8 +1079,9 @@ public function process_payment( $order_id ) {
}

// Replace token
delete_post_meta( $order->get_id(), '_payment_tokens' );
$order->delete_meta_data( '_payment_tokens' );
$order->add_payment_token( $token );
$order->save();

if ( self::wcs_is_payment_change() ) {
wc_add_notice( __( 'Payment method was updated.', 'swedbank-pay-woocommerce-payments' ) );
Expand Down
29 changes: 14 additions & 15 deletions includes/class-wc-swedbank-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

defined( 'ABSPATH' ) || exit;

use Automattic\WooCommerce\Utilities\OrderUtil;
use WC_Order;
use Exception;

Expand All @@ -21,7 +22,7 @@ public function __construct() {
);

// Add meta boxes
add_action( 'add_meta_boxes', __CLASS__ . '::add_meta_boxes' );
add_action( 'add_meta_boxes', __CLASS__ . '::add_meta_boxes', 10, 2 );

// Add action buttons
add_action( 'woocommerce_order_item_add_action_buttons', __CLASS__ . '::add_action_buttons', 10, 1 );
Expand Down Expand Up @@ -62,23 +63,22 @@ public function add_valid_order_statuses( $statuses, $order ) {
* Add meta boxes in admin
* @return void
*/
public static function add_meta_boxes() {
global $post_id;

$order = wc_get_order( $post_id );
public static function add_meta_boxes( $screen_id, $order ) {
$order = wc_get_order( $order );

if ( $order ) {
$payment_method = $order->get_payment_method();
if ( in_array( $payment_method, WC_Swedbank_Plugin::PAYMENT_METHODS, true ) ) {
$payment_id = get_post_meta( $post_id, '_payex_payment_id', true );
$payment_id = $order->get_meta( '_payex_payment_id' );
if ( ! empty( $payment_id ) ) {
$hook = OrderUtil::custom_orders_table_usage_is_enabled() ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order';
add_meta_box(
'swedbank_payment_actions',
__( 'Swedbank Pay Payments Actions', 'swedbank-pay-woocommerce-payments' ),
__CLASS__ . '::order_meta_box_payment_actions',
'shop_order',
$hook,
'side',
'default'
'high'
);
}
}
Expand All @@ -89,11 +89,9 @@ public static function add_meta_boxes() {
* MetaBox for Payment Actions
* @return void
*/
public static function order_meta_box_payment_actions() {
global $post_id;

$order = wc_get_order( $post_id );
$payment_id = get_post_meta( $post_id, '_payex_payment_id', true );
public static function order_meta_box_payment_actions( $order ) {
$order = wc_get_order( $order );
$payment_id = $order->get_meta( '_payex_payment_id' );
if ( empty( $payment_id ) ) {
return;
}
Expand All @@ -118,7 +116,7 @@ public static function order_meta_box_payment_actions() {
array(
'gateway' => $gateway,
'order' => $order,
'order_id' => $post_id,
'order_id' => $order->get_id(),
'payment_id' => $payment_id,
'info' => $result,
),
Expand Down Expand Up @@ -168,7 +166,8 @@ public static function add_action_buttons( $order ) {
* @return void
*/
public static function admin_enqueue_scripts( $hook ) {
if ( 'post.php' === $hook ) {
$hook_to_check = OrderUtil::custom_orders_table_usage_is_enabled() ? wc_get_page_screen_id( 'shop-order' ) : 'post.php';
if ( $hook_to_check === $hook ) {
// Scripts
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_register_script(
Expand Down
15 changes: 8 additions & 7 deletions includes/class-wc-swedbank-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private static function migrate_orders() {
continue;
}

$has_migrated = get_post_meta( $order->get_id(), '_sb_has_migrated', true );
$has_migrated = $order->get_meta( '_sb_has_migrated', true );
if ( ! empty( $has_migrated ) ) {
self::log( sprintf( '[WARNING] Order #%s has been skipped. It has been already migrated.',
$order->get_id()
Expand All @@ -173,11 +173,11 @@ private static function migrate_orders() {
}

// Change payment method
update_post_meta( $order->get_id(), '_payment_method', 'payex_psp_cc' );
$order->set_payment_method( 'payex_psp_cc' );

// Check if the order has an assigned card
$card_id = get_post_meta( $order->get_id(), '_payex_card_id', true );
if ( ! empty( $card_id) && count( $order->get_payment_tokens() ) === 0 ) {
$card_id = $order->get_meta( '_payex_card_id', true );
if ( ! empty( $card_id ) && count( $order->get_payment_tokens() ) === 0 ) {
// Load Saved Credit Card
$post = get_post( $card_id );
if ( ! $post ) {
Expand Down Expand Up @@ -218,17 +218,18 @@ private static function migrate_orders() {
}

// Change recurring payment method if needs
$recurring = get_post_meta( $order->get_id(), '_recurring_payment_method' );
$recurring = $order->get_meta( '_recurring_payment_method' );
if ( ! empty( $recurring ) ) {
update_post_meta( $order->get_id(), '_recurring_payment_method', 'payex_psp_cc' );
$order->update_meta_data( '_recurring_payment_method', 'payex_psp_cc' );
}

self::log( sprintf( '[INFO] The order #%s has been migrated.',
$order->get_id()
) );

// Migration flag
update_post_meta( $order->get_id(), '_sb_has_migrated', '1' );
$order->update_meta_data( '_sb_has_migrated', '1' );
$order->save();
}
}

Expand Down
9 changes: 6 additions & 3 deletions includes/class-wc-swedbank-subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public static function add_subscription_card_id( $order_id ) {
*/
public static function update_failing_payment_method( $subscription, $renewal_order ) {
// Delete tokens
//delete_post_meta( $subscription->get_id(), '_payment_tokens' );
//$subscription->delete_meta_data( '_payment_tokens' );
//$subscription->save();
}

/**
Expand All @@ -157,7 +158,8 @@ public static function update_failing_payment_method( $subscription, $renewal_or
*/
public static function delete_resubscribe_meta( $resubscribe_order ) {
// Delete tokens
delete_post_meta( $resubscribe_order->get_id(), '_payment_tokens' );
$resubscribe_order->delete_meta_data( '_payment_tokens' );
$resubscribe_order->save();
}

/**
Expand Down Expand Up @@ -263,7 +265,8 @@ public static function save_subscription_payment_meta( $subscription, $meta_tabl

if ( 'swedbankpay_meta' === $meta_table && 'token_id' === $meta_key ) {
// Delete tokens
delete_post_meta( $subscription->get_id(), '_payment_tokens' );
$subscription->delete_meta_data( '_payment_tokens' );
$subscription->save();

// Add tokens
$tokens = explode( ',', $meta_value );
Expand Down
14 changes: 12 additions & 2 deletions swedbank-pay-woocommerce-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* Version: 6.3.0
* Text Domain: swedbank-pay-woocommerce-payments
* Domain Path: /languages
* WC requires at least: 5.5.1
* WC tested up to: 7.0.0
* WC requires at least: 6.4.0
* WC tested up to: 7.3.0
*/

use SwedbankPay\Payments\WooCommerce\WC_Swedbank_Plugin;
Expand All @@ -37,6 +37,7 @@ public function __construct() {
// Actions
add_action( 'plugins_loaded', array( $this, 'init' ), 0 );
add_action( 'woocommerce_loaded', array( $this, 'woocommerce_loaded' ), 20 );
add_action( 'before_woocommerce_init', array( $this, 'declare_compatibility_with_custom_order_tables' ) );
}

/**
Expand Down Expand Up @@ -70,6 +71,15 @@ public function woocommerce_loaded() {
WC_Swedbank_Pay::register_gateway( 'WC_Gateway_Swedbank_Pay_Mobilepay' );
WC_Swedbank_Pay::register_gateway( 'WC_Gateway_Swedbank_Pay_Trustly' );
}

/**
* Declare compatibilty with WooCommerce custom order tables.
*/
public function declare_compatibility_with_custom_order_tables() {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}
}

new WC_Swedbank_Pay();