Woocommerce Payments Shortcode

Below, you’ll find a detailed guide on how to add the Woocommerce Payments Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Woocommerce Payments Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Woocommerce Payments Plugin and the shortcodes it provides:

Plugin Icon
WooPayments – Fully Integrated Solution Built and Supported by Woo

"WooPayments is a comprehensive payment solution for WooCommerce. Built and supported by Woo, it streamlines all transactions, making online payments swift, secure, and seamless."

★★☆✩✩ (53) Active Installs: 600000+ Tested with: 6.2.3 PHP Version: 7.3
Included Shortcodes:
  • [subscriptions]

Woocommerce Payments [subscriptions] Shortcode

The ‘subscriptions’ shortcode is designed to display a user’s active subscriptions. It fetches data based on user ID and subscription status. In detail, the shortcode retrieves all subscriptions associated with a given user ID. It then filters these subscriptions based on the status attribute. If the status is neither ‘all’ nor ‘any’, it removes subscriptions that don’t match the specified status. The filtered subscriptions are then displayed using the ‘myaccount/my-subscriptions.php’ template.

Shortcode: [subscriptions]

Parameters

Here is a list of all possible subscriptions shortcode parameters and attributes:

  • user_id – User’s unique identifier to fetch subscriptions
  • status – Filter subscriptions by their status

Examples and Usage

Basic example – A simple usage of the shortcode to display active subscriptions for a specific user. Here, we are using user_id as 12. This will display all the active subscriptions for the user with the ID 12.

[subscriptions user_id=12 status=active /]

Advanced examples

The shortcode can be used to display all subscriptions, regardless of status, for a specific user. In this example, we’re using user_id as 10. This will display all subscriptions for the user with the ID 10, regardless of whether they are active, on hold, cancelled, or expired.

[subscriptions user_id=10 status=all /]

Additionally, the shortcode can be used to display only the subscriptions with a specific status for a specific user. In this example, we’re using user_id as 15 and status as ‘on-hold’. This will display all the ‘on-hold’ subscriptions for the user with the ID 15.

[subscriptions user_id=15 status=on-hold /]

PHP Function Code

In case you have difficulties debugging what causing issues with [subscriptions] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'subscriptions', __CLASS__ . '::do_subscriptions_shortcode' );

Shortcode PHP function:

function do_subscriptions_shortcode( $attributes ) {
		$attributes = shortcode_atts(
			array(
				'user_id' => 0,
				'status'  => 'active',
			),
			$attributes,
			'subscriptions'
		);

		$subscriptions = wcs_get_users_subscriptions( $attributes['user_id'] );

		// Limit subscriptions to the appropriate status if it's not "any" or "all".
		if ( 'all' !== $attributes['status'] && 'any' !== $attributes['status'] ) {
			/** @var WC_Subscription $subscription */
			foreach ( $subscriptions as $index => $subscription ) {
				if ( ! $subscription->has_status( $attributes['status'] ) ) {
					unset( $subscriptions[ $index ] );
				}
			}
		}

		// Load the subscription template, and return its content using Output Buffering.
		ob_start();
		wc_get_template(
			'myaccount/my-subscriptions.php',
			array(
				'subscriptions' => $subscriptions,
				'user_id'       => $attributes['user_id'],
			),
			'',
			WC_Subscriptions_Core_Plugin::instance()->get_subscriptions_core_directory( 'templates/' )
		);

		return ob_get_clean();
	}

Code file location:

woocommerce-payments/woocommerce-payments/vendor/woocommerce/subscriptions-core/includes/admin/class-wc-subscriptions-admin.php

Conclusion

Now that you’ve learned how to embed the Woocommerce Payments Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *