Skip to content

Tips for testing

Matt Allan edited this page Mar 18, 2024 · 4 revisions

Code snippets

When developing, testing flows via UI can become a bottleneck for productivity.

These handy code snippets can help speed up the process, but they are not recommended for accurately testing user flows.

WC Subscriptions renewal – successful payment

This code will process a subscription renewal, assuming a valid payment token exists.

$subscription_id = 647; // <-- Replace ID with a subscription ID on your store.
$subscription = wcs_get_subscription( $subscription_id );
$subscription->update_status( 'active' );
do_action( 'woocommerce_scheduled_subscription_payment', $subscription->get_id() );

WC Subscriptions renewal – failed payment

This code will create a failing WCPay payment token before processing a renewal.

// Create a failing payment token 
// Assumed WCPay as payment gateway
$token = new WC_Payment_Token_CC();
$subscription_id = 647; // <-- Replace ID with a subscription ID on your store.

$token->set_token( 'pm_card_visa_chargeDeclined' ); // <-- Insert the PM id of the card you want to test.
// 'pm_card_visa' for success
$token->set_gateway_id( 'woocommerce_payments' );
$token->set_card_type( 'visa' );
$token->set_last4( 1234 );
$token->set_expiry_month( 12 );
$token->set_expiry_year( 2030 );
$token->save();

$subscription = wcs_get_subscription( $subscription_id );
$subscription->add_payment_token( $token );

do_action( 'woocommerce_scheduled_subscription_payment', $subscription->get_id() );

Remove all orders and subscriptions from the database.

Will remove for both HPOS and non-HPOS.

This SQL query should be run via cli, phpMyAdmin, Sequel Pro, or similar.

TRUNCATE TABLE wp_wc_orders;
TRUNCATE TABLE wp_wc_orders_meta;
TRUNCATE TABLE wp_wc_order_addresses;
TRUNCATE TABLE wp_wc_order_operational_data;


DELETE FROM wp_posts WHERE post_type IN ('shop_subscription', 'shop_order', 'shop_order_refund', 'shop_order_placehold');
DELETE pm
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL;

DELETE FROM wp_usermeta WHERE meta_key = '_wcs_subscription_ids_cache';
DELETE from wp_actionscheduler_actions WHERE hook = 'woocommerce_scheduled_subscription_payment';