WPC Buy Now Button for WooCommerce Shortcodes

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

Before starting, here is an overview of the WPC Buy Now Button for WooCommerce Plugin and the shortcodes it provides:

Plugin Icon
WPC Buy Now Button for WooCommerce

"WPC Buy Now Button for WooCommerce is a user-friendly plugin that allows you to integrate a direct 'Buy Now' button into your WooCommerce store, simplifying and speeding up the checkout process."

★★★★☆ (9) Active Installs: 4000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [wpcbn_btn_archive]
  • [wpcbn_btn_single]

WPC Buy Now Button for WooCommerce [wpcbn_btn_archive] Shortcode

The wpc-buy-now-button shortcode is a powerful tool that generates a ‘Buy Now’ button. It enables customers to quickly add products to their cart or checkout page. This shortcode uses the ‘archive_shortcode’ function to display the button. It accepts one parameter ‘id’, which can be used to specify a particular product. If no ‘id’ is provided, the button is displayed for all simple, in-stock, and purchasable products. The button text and appearance can be customized using filters. The ‘Buy Now’ button directs customers to either the cart or checkout page based on the ‘redirect’ setting.

Shortcode: [wpcbn_btn_archive]

Parameters

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

  • id – Identifier for the specific product to be purchased

Examples and Usage

Basic example – A simple usage of the wpcbn_btn_archive shortcode to display a “Buy Now” button for a specific product using its ID.

[wpcbn_btn_archive id=123 /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'wpcbn_btn_archive', [ $this, 'archive_shortcode' ] );

Shortcode PHP function:

function archive_shortcode( $atts ) {
					$output = '';

					$atts = shortcode_atts( [
						'id' => null
					], $atts, 'wpcbn_btn_archive' );

					$btn_text = self::get_setting( 'button_text', '' );

					if ( empty( $btn_text ) ) {
						$btn_text = esc_html__( 'Buy now', 'wpc-buy-now-button' );
					}

					$btn_text  = apply_filters( 'wpcbn_btn_archive_text', $btn_text, $atts );
					$btn_class = apply_filters( 'wpcbn_btn_archive_class', 'wpcbn-btn wpcbn-btn-archive button product_type_simple add_to_cart_button', $atts );
					$btn_href  = apply_filters( 'wpcbn_redirect', self::get_setting( 'redirect', 'checkout' ) ) === 'cart' ? wc_get_cart_url() : wc_get_checkout_url();

					if ( ! $atts['id'] ) {
						global $product;

						if ( $product && $product->is_type( 'simple' ) && $product->is_in_stock() && $product->is_purchasable() ) {
							$output .= sprintf( '<a href="%s?' . self::$param . '=%s" data-quantity="1" class="%s" data-product_id="%s" rel="nofollow">%s</a>', $btn_href, $product->get_ID(), $btn_class, $product->get_ID(), $btn_text );
						}
					} else {
						$output .= sprintf( '<a href="%s?' . self::$param . '=%s" data-quantity="1" class="%s" data-product_id="%s" rel="nofollow">%s</a>', $btn_href, absint( $atts['id'] ), $btn_class, absint( $atts['id'] ), $btn_text );
					}

					return apply_filters( 'wpcbn_btn_archive', $output, $atts );
				}

Code file location:

wpc-buy-now-button/wpc-buy-now-button/wpc-buy-now-button.php

WPC Buy Now Button for WooCommerce [wpcbn_btn_single] Shortcode

The wpc-buy-now-button shortcode is responsible for creating a “Buy Now” button for single products. It accepts an ‘id’ attribute, which can specify a particular product. If no ‘id’ is provided, the button is generated for the global product. The button text and class are customizable, with default values set as ‘Buy now’ and ‘wpcbn-btn wpcbn-btn-single single_add_to_cart_button button alt’ respectively.

Shortcode: [wpcbn_btn_single]

Parameters

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

  • id – Specifies the unique ID of the product

Examples and Usage

Basic example – A simple usage of the shortcode to display the buy now button for the current product in context.

[wpcbn_btn_single /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'wpcbn_btn_single', [ $this, 'single_shortcode' ] );

Shortcode PHP function:

function single_shortcode( $atts ) {
					$output = '';

					$atts = shortcode_atts( [
						'id' => null
					], $atts, 'wpcbn_btn_single' );

					$btn_text = self::get_setting( 'button_text', '' );

					if ( empty( $btn_text ) ) {
						$btn_text = esc_html__( 'Buy now', 'wpc-buy-now-button' );
					}

					$btn_text  = apply_filters( 'wpcbn_btn_single_text', $btn_text, $atts );
					$btn_class = apply_filters( 'wpcbn_btn_single_class', 'wpcbn-btn wpcbn-btn-single single_add_to_cart_button button alt', $atts );

					if ( ! $atts['id'] ) {
						global $product;

						if ( $product ) {
							$output .= sprintf( '<button type="submit" name="' . esc_attr( self::$param ) . '" value="%d" class="%s" data-product_id="%s">%s</button>', $product->get_ID(), $btn_class, $product->get_ID(), $btn_text );
						}
					} else {
						$output .= sprintf( '<button type="submit" name="' . esc_attr( self::$param ) . '" value="%d" class="%s" data-product_id="%s">%s</button>', absint( $atts['id'] ), $btn_class, absint( $atts['id'] ), $btn_text );
					}

					return apply_filters( 'wpcbn_btn_single', $output, $atts );
				}

Code file location:

wpc-buy-now-button/wpc-buy-now-button/wpc-buy-now-button.php

Conclusion

Now that you’ve learned how to embed the WPC Buy Now Button for WooCommerce 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.

Comments

Leave a Reply

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