Below, you’ll find a detailed guide on how to add the Advanced Coupons Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Advanced Coupons Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the Advanced Coupons Plugin and the shortcodes it provides:
"Advanced Coupons is a comprehensive WooCommerce plugin that enhances your store with features like store credit, gift cards, loyalty programs, BOGO coupons, and discount rules."
- [acfw_coupon_by_categories]
- [acfw_single_coupon]
- [acfw_coupons_by_customer]
- [acfw_customer_store_credit_balance]
Advanced Coupons [acfw_coupon_by_categories] Shortcode
The Advanced Coupons for WooCommerce Free plugin shortcode, ‘acfw_coupon_by_categories’, displays coupons based on specific product categories. The shortcode queries for ‘shop_coupon’ post types that are published, and filters them by the categories specified in the shortcode attributes. If no coupons are found, it returns an empty string. The results are sorted by the ‘order_by’ attribute. If it’s set to ‘expire/asc’, the coupons are sorted by expiration date in ascending order. The coupons are then displayed using the ‘acfw-coupons-by-category-block’ template. After the shortcode is processed, the global WP Query data is reset to prevent interference with the default post object on the page.
Shortcode: [acfw_coupon_by_categories]
Parameters
Here is a list of all possible acfw_coupon_by_categories shortcode parameters and attributes:
categories
– Specifies the categories of coupons to be displayedcount
– Determines the number of coupons to show per pageorder_by
– Sets the order of the displayed coupons
Examples and Usage
Basic example – Display coupons from a specific category using its term_id.
[acfw_coupon_by_categories categories=3]
Advanced examples
Display five coupons from a specific category, sorted by post date in ascending order.
[acfw_coupon_by_categories categories=3 count=5 order_by='date/asc']
Display coupons from multiple categories. Here, the categories attribute accepts an array of term_ids.
[acfw_coupon_by_categories categories="3,4,5"]
Display coupons sorted by expiration date in descending order. This is useful when you want to show the coupons that are about to expire first.
[acfw_coupon_by_categories categories=3 order_by='expire/desc']
PHP Function Code
In case you have difficulties debugging what causing issues with [acfw_coupon_by_categories]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'acfw_coupon_by_categories', array( $this, 'render_coupons_by_category_block' ) );
Shortcode PHP function:
function render_coupons_by_category_block( $attributes ) {
// don't proceed if there are no categories set.
if ( ! is_array( $attributes ) || ! isset( $attributes['categories'] ) ) {
return '';
}
$defaults = $this->_get_coupons_by_category_atts( true );
$attributes = $this->_sanitize_parse_atts( $attributes, $defaults );
// build query object.
$args = $this->_append_sort_arguments_for_query(
array(
'post_type' => 'shop_coupon',
'post_status' => 'publish',
'posts_per_page' => $attributes['count'],
'fields' => 'ids',
'ignore_sticky_posts' => true,
'tax_query' => array(
array(
'taxonomy' => 'shop_coupon_cat',
'field' => 'term_id',
'terms' => $attributes['categories'],
),
),
),
$attributes['order_by']
);
$query = new \WP_Query( $args );
// don't proceed if there are no coupons detected in loop.
if ( empty( $query->posts ) ) {
return '';
}
// map list of IDs to list of advanced coupon objects.
$coupons = $this->_get_coupons_from_queried_ids( $query->posts );
/**
* Sort coupons by expiration when order by is set to 'expire/desc'.
* This needs to be handled via PHP so coupons with expiry are prioritized than coupons that have no expiry.
*/
if ( 'expire/asc' === $attributes['order_by'] ) {
$this->sort_coupons_list_by_expiry( $coupons, 'asc' );
}
ob_start();
$this->load_coupons_list_template(
$coupons,
'acfw-coupons-by-category-block',
$attributes
);
// reset the wp query global data to make sure we don't override the default post object in the page.
wp_reset_postdata();
return ob_get_clean();
}
Code file location:
advanced-coupons-for-woocommerce-free/advanced-coupons-for-woocommerce-free/Models/Editor_Blocks.php
Advanced Coupons [acfw_single_coupon] Shortcode
The Advanced Coupons for WooCommerce shortcode ‘acfw_single_coupon’ is used to render a specific coupon block. It first checks if a coupon ID is provided in the attributes. If not, it returns an empty string. It then sanitizes the attributes and extracts them. It checks if the premium plugin is active and creates a new Advanced Coupon object based on the coupon ID. Finally, it loads the single coupon template and returns the output.
Shortcode: [acfw_single_coupon]
Parameters
Here is a list of all possible acfw_single_coupon shortcode parameters and attributes:
coupon_id
– The unique identifier for the specific coupon.contentVisibility
– Determines visibility of the coupon content.className
– Specifies a CSS class for the coupon block.
Examples and Usage
Basic example – A shortcode is used to render a single coupon block on the site. The shortcode requires a coupon ID to function properly.
[acfw_single_coupon coupon_id=1 /]
Advanced examples
Rendering a single coupon block with additional parameters. In this case, we’re specifying both the coupon ID and the content visibility. The shortcode will first try to load the coupon by ID, but if not found, it will try to load by content visibility.
[acfw_single_coupon coupon_id=2 contentVisibility={'borderRadius':5,'borderWidth':2} /]
Rendering a single coupon block with multiple parameters. This includes the coupon ID, content visibility, and a specific CSS class name for further customization.
[acfw_single_coupon coupon_id=3 contentVisibility={'borderRadius':5,'borderWidth':2} className="custom-coupon-class" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [acfw_single_coupon]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'acfw_single_coupon', array( $this, 'render_single_coupon_block' ) );
Shortcode PHP function:
function render_single_coupon_block( $attributes ) {
// don't proceed if there is no coupon ID.
if ( ! is_array( $attributes ) || ! isset( $attributes['coupon_id'] ) ) {
return '';
}
$defaults = $this->_get_single_coupon_atts( true );
$attributes = $this->_sanitize_parse_atts( $attributes, $defaults );
extract( $attributes ); // phpcs:ignore
$is_premium = $this->_helper_functions->is_plugin_active( Plugin_Constants::PREMIUM_PLUGIN );
$coupon = $is_premium ? new \ACFWP\Models\Objects\Advanced_Coupon( absint( $coupon_id ) ) : new Advanced_Coupon( absint( $coupon_id ) );
ob_start();
$this->_helper_functions->load_single_coupon_template( $coupon, (object) $contentVisibility, $className );
return ob_get_clean();
}
Code file location:
advanced-coupons-for-woocommerce-free/advanced-coupons-for-woocommerce-free/Models/Editor_Blocks.php
Advanced Coupons [acfw_coupons_by_customer] Shortcode
The Advanced Coupons for WooCommerce Free plugin shortcode, ‘acfw_coupons_by_customer’, is designed to render coupons specific to a customer. This shortcode calls the function ‘render_coupons_by_customer_block’. It sanitizes and parses attributes for coupons, applying filters to render the customer-specific coupons.
Shortcode: [acfw_coupons_by_customer]
Examples and Usage
Basic example – Display coupons for a specific customer using their ID.
[acfw_coupons_by_customer id="123"]
Advanced examples
Display coupons for a specific customer using their ID, and specify the number of coupons to display.
[acfw_coupons_by_customer id="123" count="5"]
Display coupons for a specific customer using their ID, specify the number of coupons to display, and filter by a specific coupon type.
[acfw_coupons_by_customer id="123" count="5" type="percentage"]
Display coupons for a specific customer using their ID, specify the number of coupons to display, filter by a specific coupon type, and sort by coupon value in ascending order.
[acfw_coupons_by_customer id="123" count="5" type="percentage" sort="value_asc"]
PHP Function Code
In case you have difficulties debugging what causing issues with [acfw_coupons_by_customer]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'acfw_coupons_by_customer', array( $this, 'render_coupons_by_customer_block' ) );
Shortcode PHP function:
function render_coupons_by_customer_block( $attributes ) {
$attributes = is_array( $attributes ) ? $attributes : array();
$defaults = $this->_get_coupons_by_customer_atts( true );
$attributes = $this->_sanitize_parse_atts( $attributes, $defaults );
return apply_filters( 'acfw_render_coupons_by_customer_block', '', $attributes );
}
Code file location:
advanced-coupons-for-woocommerce-free/advanced-coupons-for-woocommerce-free/Models/Editor_Blocks.php
Advanced Coupons [acfw_customer_store_credit_balance] Shortcode
The Advanced Coupons for WooCommerce shortcode ‘acfw_customer_store_credit_balance’ displays the logged-in user’s store credit balance. The function checks if a user is logged in. If true, it retrieves and returns the user’s store credit balance in WooCommerce’s currency format.
Shortcode: [acfw_customer_store_credit_balance]
Examples and Usage
Basic example – Show the customer’s store credit balance
[acfw_customer_store_credit_balance]
This shortcode does not require any parameters or attributes as it fetches the store credit balance of the currently logged-in user. If no user is logged in, it will return an empty string.
Advanced examples
Display the store credit balance wrapped in a custom message
add_filter( 'acfw_filter_amount', function( $balance ) {
return "Your store credit balance is: " . wc_price( $balance );
} );
echo do_shortcode( '[acfw_customer_store_credit_balance]' );
In this example, we are using the ‘acfw_filter_amount’ filter to modify the output of the store credit balance. We append a custom message to the balance before it is returned by the shortcode. Please note that this requires modifying the PHP code and is not a parameter of the shortcode itself.
Please note that the ‘acfw_customer_store_credit_balance’ shortcode does not accept parameters or attributes. It simply returns the store credit balance of the currently logged-in user. However, the output can be modified using the ‘acfw_filter_amount’ filter as shown in the advanced example.
PHP Function Code
In case you have difficulties debugging what causing issues with [acfw_customer_store_credit_balance]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'acfw_customer_store_credit_balance', array( $this, 'customer_store_credit_balance' ) );
Shortcode PHP function:
function customer_store_credit_balance() {
if ( ! is_user_logged_in() ) {
return '';
}
$user_balance = apply_filters( 'acfw_filter_amount', \ACFWF()->Store_Credits_Calculate->get_customer_balance( get_current_user_id() ) );
return wc_price( $user_balance );
}
Code file location:
advanced-coupons-for-woocommerce-free/advanced-coupons-for-woocommerce-free/Models/Store_Credits/My_Account.php
Conclusion
Now that you’ve learned how to embed the Advanced Coupons Plugin shortcodes, 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.
Leave a Reply