Booster for WooCommerce Shortcode

Below, you’ll find a detailed guide on how to add the Booster for WooCommerce 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 Booster for WooCommerce Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Booster for WooCommerce Plugin and the shortcodes it provides:

Plugin Icon
Booster for WooCommerce

"Booster for WooCommerce is an all-in-one plugin solution for your e-commerce site. It provides advanced features to supercharge your WooCommerce store, enhancing its functionality and user experience."

★★★★☆ (489) Active Installs: 60000+ Tested with: 6.3.2 PHP Version: 7.2
Included Shortcodes:
  • [wcj_shortcode]

Booster for WooCommerce [wcj_shortcode] Shortcode

The Woocommerce-Jetpack plugin shortcode enables customization of various attributes such as visibility, user role, and language. It checks whether a module is enabled and validates user privileges.

Shortcode: [wcj_shortcode]

Parameters

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

  • plus – Defines a multiplier for the result of the shortcode.
  • before – Text to be displayed before the shortcode result.
  • after – Text to be displayed after the shortcode result.
  • visibility – Controls the visibility of the shortcode based on user role.
  • wrong_user_text – Text to be displayed when the user role does not match the visibility.
  • wrong_user_text_not_logged_in – Text to be displayed when the user is not logged in.
  • site_visibility – Controls the visibility of the shortcode based on the page type.
  • location – Controls the visibility of the shortcode based on user’s location.
  • not_location – Displays the shortcode if the user is not in the specified location.
  • wpml_language – Controls the visibility of the shortcode based on the site language.
  • wpml_not_language – Displays the shortcode if the site language is not specified.
  • billing_country – Displays the shortcode if the user’s billing country matches the specified value.
  • not_billing_country – Displays the shortcode if the user’s billing country does not match the specified value.
  • payment_method – Displays the shortcode if the user’s payment method matches the specified value.
  • not_payment_method – Displays the shortcode if the user’s payment method does not match the specified value.
  • module – Specifies the module to be used with the shortcode.
  • find – Specifies a value to find within the shortcode result.
  • replace – Specifies a value to replace the found value in the shortcode result.
  • strip_tags – Removes HTML tags from the shortcode result if set to ‘yes’.
  • on_empty – Specifies a value to return if the shortcode result is empty.
  • on_zero – Specifies a value to return if the shortcode result is zero.
  • time – Specifies a time value to be used with the shortcode.
  • multiply – Specifies a multiplier to be used with the shortcode result.

Examples and Usage

Basic example – A basic usage of the shortcode to display a module with a specific ID. This will only display the module if it is enabled.

[wcj_shortcode module="module_id"]

Advanced examples

Using the shortcode to display a module with specific attributes. This will only display the module if it is enabled and if the time is correct. The ‘before’ and ‘after’ attributes allow you to add content before and after the module.

[wcj_shortcode module="module_id" time="12:00:00" before="Before content" after="After content"]

Using the shortcode to display a module only for users with a specific role. The ‘visibility’ attribute is used to specify the user role. If the user does not have the correct role, the ‘wrong_user_text’ message will be displayed.

[wcj_shortcode module="module_id" visibility="administrator" wrong_user_text="Sorry, you do not have the correct permissions to view this content."]

Using the shortcode to display a module only if the user is located in a specific location. The ‘location’ attribute is used to specify the location. If the user is not in the correct location, the module will not be displayed.

[wcj_shortcode module="module_id" location="US"]

Using the shortcode to display a module only if the user’s billing country matches a specific country. The ‘billing_country’ attribute is used to specify the country. If the user’s billing country does not match, the module will not be displayed.

[wcj_shortcode module="module_id" billing_country="US"]

PHP Function Code

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

Shortcode line:

add_shortcode( $the_shortcode, array( $this, 'wcj_shortcode' ) );

Shortcode PHP function:

function wcj_shortcode( $atts, $content, $shortcode ) {

			// Init.
			if ( empty( $atts ) ) {
				$atts = array();
			}

			// Add child class specific atts.
			$atts = $this->add_extra_atts( $atts );

			// Merge atts with global defaults.
			$global_defaults = array(
				'plus'                          => 1,
				'before'                        => '',
				'after'                         => '',
				'visibility'                    => '', // user_visibility.
				'wrong_user_text'               => '', // '<p>' . __( 'Wrong user role!', 'woocommerce-jetpack' ) . '</p>'.
				'wrong_user_text_not_logged_in' => '',
				'site_visibility'               => '',
				'location'                      => '', // user_location.
				'not_location'                  => '', // user_location.
				'wpml_language'                 => '',
				'wpml_not_language'             => '',
				'billing_country'               => '',
				'not_billing_country'           => '',
				'payment_method'                => '',
				'not_payment_method'            => '',
				'module'                        => '',
				'find'                          => '',
				'replace'                       => '',
				'strip_tags'                    => 'no',
				'on_empty'                      => '',
				'on_zero'                       => 0,
				'time'                          => '',
				'multiply'                      => 1,
			);
			$atts            = array_merge( $global_defaults, $atts );

			// Check for required atts.
			$atts = $this->init_atts( $atts );
			if ( false === ( $atts ) ) {
				return '';
			}

			if (
				false === filter_var( $atts['plus'], FILTER_VALIDATE_BOOLEAN )
				&& class_exists( 'WCJ_Plus' )
			) {
				return '';
			}

			// Check for module enabled.
			if ( '' !== $atts['module'] && ! wcj_is_module_enabled( $atts['module'] ) ) {
				/* translators: %s: search term */
				return '<p>' . sprintf( __( '"%s" module is not enabled!', 'woocommerce-jetpack' ), $atts['module_name'] ) . '</p>';
			}

			// Check if time is ok.
			if ( '' !== $atts['time'] && ! wcj_check_time( $atts['time'] ) ) {
				return '';
			}

			// Check if privileges are ok.
			if ( '' !== $atts['visibility'] ) {
				global $wcj_pdf_invoice_data;
				$visibilities          = str_replace( ' ', '', $atts['visibility'] );
				$visibilities          = explode( ',', $visibilities );
				$is_iser_visibility_ok = false;
				foreach ( $visibilities as $visibility ) {
					if ( 'admin' === $visibility ) {
						$visibility = 'administrator';
					}
					if ( isset( $wcj_pdf_invoice_data['user_id'] ) && 0 === $wcj_pdf_invoice_data['user_id'] ) {
						if ( 'guest' === $visibility ) {
							$is_iser_visibility_ok = true;
							break;
						}
					} else {
						$user_id = ( isset( $wcj_pdf_invoice_data['user_id'] ) ? $wcj_pdf_invoice_data['user_id'] : 0 );
						if ( wcj_is_user_role( $visibility, $user_id ) ) {
							$is_iser_visibility_ok = true;
							break;
						}
					}
				}
				if ( ! $is_iser_visibility_ok ) {
					if ( ! is_user_logged_in() ) {
						$login_form = '';
						$login_url  = '';
						if ( false !== strpos( $atts['wrong_user_text_not_logged_in'], '%login_form%' ) ) {
							ob_start();
							woocommerce_login_form();
							$login_form = ob_get_clean();
						}
						if ( false !== strpos( $atts['wrong_user_text_not_logged_in'], '%login_url%' ) ) {
							$login_url = wp_login_url( get_permalink() );
						}
						return str_replace( array( '%login_form%', '%login_url%' ), array( $login_form, $login_url ), $atts['wrong_user_text_not_logged_in'] );
					} else {
						return $atts['wrong_user_text'];
					}
				}
			}

			// Check if site visibility is ok.
			if ( '' !== $atts['site_visibility'] ) {
				if (
					( 'single' === $atts['site_visibility'] && ! is_single() ) ||
					( 'page' === $atts['site_visibility'] && ! is_page() ) ||
					( 'archive' === $atts['site_visibility'] && ! is_archive() ) ||
					( 'front_page' === $atts['site_visibility'] && ! is_front_page() )
				) {
					return '';
				}
			}

			// Check if location is ok.
			if (
				'' !== $atts['location'] &&
				'all' !== $atts['location'] &&
				( false === strpos( $atts['location'], ',' ) && $atts['location'] !== $this->wcj_get_user_location() ||
					false !== strpos( $atts['location'], ',' ) && ! in_array( $this->wcj_get_user_location(), array_map( 'trim', explode( ',', $atts['location'] ) ), true ) )
			) {
				return '';
			}
			if (
				'' !== $atts['not_location'] &&
				( false === strpos( $atts['not_location'], ',' ) && $atts['not_location'] === $this->wcj_get_user_location() ||
					false !== strpos( $atts['not_location'], ',' ) && in_array( $this->wcj_get_user_location(), array_map( 'trim', explode( ',', $atts['not_location'] ) ), true ) )
			) {
				return '';
			}

			// Check if language is ok.
			if ( 'wcj_wpml' === $shortcode || 'wcj_wpml_translate' === $shortcode ) {
				if ( isset( $atts['lang'] ) ) {
					$atts['wpml_language'] = $atts['lang'];
				}
				if ( isset( $atts['not_lang'] ) ) {
					$atts['wpml_not_language'] = $atts['not_lang'];
				}
			}
			if ( '' !== $atts['wpml_language'] ) {
				if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) {
					return '';
				}
				if ( ! in_array( ICL_LANGUAGE_CODE, $this->custom_explode( $atts['wpml_language'] ), true ) ) {
					return '';
				}
			}
			// Check if language is ok (not in...).
			if ( '' !== $atts['wpml_not_language'] ) {
				if ( defined( 'ICL_LANGUAGE_CODE' ) ) {
					if ( in_array( ICL_LANGUAGE_CODE, $this->custom_explode( $atts['wpml_not_language'] ), true ) ) {
						return '';
					}
				}
			}

			// Check if billing country by arg is ok.
			// phpcs:disable WordPress.Security.NonceVerification
			if ( '' !== $atts['billing_country'] ) {
				if ( ! isset( $_GET['order_id'] ) ) {
					return '';
				}
				$order_id       = sanitize_text_field( wp_unslash( $_GET['order_id'] ) );
				$orders         = new WC_Order( $order_id );
				$billing_contry = $orders->get_billing_country();
				if ( ! isset( $billing_contry ) ) {

					return '';
				}
				if ( ! in_array( $billing_contry, $this->custom_explode( $atts['billing_country'] ), true ) ) {

					return '';
				}
			}
			// Check if billing country by arg is ok (not in...).
			if ( '' !== $atts['not_billing_country'] ) {
				if ( ! isset( $_GET['order_id'] ) ) {
					return '';
				}
				$order_id       = sanitize_text_field( wp_unslash( $_GET['order_id'] ) );
				$orders         = new WC_Order( $order_id );
				$billing_contry = $orders->get_billing_country();
				if ( isset( $billing_contry ) ) {
					if ( in_array( $billing_contry, $this->custom_explode( $atts['not_billing_country'] ), true ) ) {

						return '';
					}
				}
			}

			// Check if payment method by arg is ok.
			if ( '' !== $atts['payment_method'] ) {
				if ( ! isset( $_GET['payment_method'] ) ) {
					return '';
				}
				if ( ! in_array( $_GET['payment_method'], $this->custom_explode( $atts['payment_method'] ), true ) ) {
					return '';
				}
			}
			// Check if payment method by arg is ok (not in...).
			if ( '' !== $atts['not_payment_method'] ) {
				if ( isset( $_GET['payment_method'] ) ) {
					if ( in_array( $_GET['payment_method'], $this->custom_explode( $atts['not_payment_method'] ), true ) ) {
						return '';
					}
				}
			}
			// phpcs:enable WordPress.Security.NonceVerification

			// Additional (child class specific) checks.
			if ( ! $this->extra_check( $atts ) ) {
				return '';
			}

			// Run the shortcode function.
			$shortcode_function = $shortcode;
			$result             = $this->$shortcode_function( $atts, $content );
			if ( '' !== ( $result ) ) {
				if ( 0 === $result && 0 !== $atts['on_zero'] ) {
					return $atts['on_zero'];
				}
				if ( '' !== $atts['find'] ) {
					if ( false !== strpos( $atts['find'], ',' ) && strlen( $atts['find'] ) > 2 ) {
						$find    = explode( ',', $atts['find'] );
						$replace = explode( ',', $atts['replace'] );
						if ( count( $find ) === count( $replace ) ) {
							$atts['find']    = $find;
							$atts['replace'] = $replace;
						}
					}
					$result = str_replace( $atts['find'], $atts['replace'], $result );
				}
				if ( 'yes' === $atts['strip_tags'] ) {
					$result = wp_strip_all_tags( $result );
				}
				if ( 1 !== $atts['multiply'] ) {
					$result = $result * $atts['multiply'];
				}
				return $atts['before'] . apply_filters( 'wcj_shortcode_result', $result, $atts, $content, $shortcode ) . $atts['after'];
			}
			return $atts['on_empty'];
		}

Code file location:

woocommerce-jetpack/woocommerce-jetpack/includes/classes/class-wcj-shortcodes.php

Conclusion

Now that you’ve learned how to embed the Booster for WooCommerce 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 *