Below, you’ll find a detailed guide on how to add the WPC Smart Compare 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 Smart Compare for WooCommerce Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the WPC Smart Compare for WooCommerce Plugin and the shortcodes it provides:
"WPC Smart Compare for WooCommerce is a highly intuitive plugin that enhances your e-commerce store by allowing customers to compare products in a sleek, user-friendly interface."
- [woosc]
- [woosc_list]
- [woosc_quick_table]
WPC Smart Compare for WooCommerce [woosc] Shortcode
The Woo Smart Compare plugin’s shortcode is designed to create a customizable comparison button for WooCommerce products. The shortcode_btn function receives attributes, checks if a product ID is set, and retrieves product details. If the product belongs to specified categories, it generates a comparison button. The button can be styled as a link or a standard button, with customizable text and optional icons. It also includes the product’s name and image in the data attributes. The output is filtered through ‘woosc_button_html’ before returning, allowing for further customization.
Shortcode: [woosc]
Parameters
Here is a list of all possible woosc shortcode parameters and attributes:
id
– The unique identifier for the WooCommerce product.type
– Determines whether the button is a ‘link’ or a ‘button’.
Examples and Usage
Basic example – Display a compare button for a specific product using its ID
[woosc id=123 /]
Advanced examples
Display a compare link instead of a button for a specific product using its ID
[woosc id=123 type=link /]
Display a compare button for the current product (must be used inside a product loop)
[woosc /]
Please note that the ‘woosc’ shortcode will output nothing if used outside a product loop and without specifying a product ID.
PHP Function Code
In case you have difficulties debugging what causing issues with [woosc]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'woosc', [ $this, 'shortcode_btn' ] );
Shortcode PHP function:
function shortcode_btn( $attrs ) {
$output = $product_name = $product_image = '';
$attrs = shortcode_atts( [
'id' => null,
'type' => self::get_setting( 'button_type', 'button' )
], $attrs );
if ( ! $attrs['id'] ) {
global $product;
if ( $product && is_a( $product, 'WC_Product' ) ) {
$attrs['id'] = $product->get_id();
$product_name = $product->get_name();
$product_image_id = $product->get_image_id();
$product_image = wp_get_attachment_image_url( $product_image_id );
}
} else {
if ( $_product = wc_get_product( $attrs['id'] ) ) {
$product_name = $_product->get_name();
$product_image_id = $_product->get_image_id();
$product_image = wp_get_attachment_image_url( $product_image_id );
}
}
if ( $attrs['id'] ) {
// check cats
$cats = self::get_setting( 'search_cats', [] );
if ( ! empty( $cats ) && ( $cats[0] !== '0' ) ) {
if ( ! has_term( $cats, 'product_cat', $attrs['id'] ) ) {
return '';
}
}
// button class
$class = 'woosc-btn woosc-btn-' . esc_attr( $attrs['id'] ) . ' ' . self::get_setting( 'button_class' );
// button text
$text = self::localization( 'button', esc_html__( 'Compare', 'woo-smart-compare' ) );
if ( ( $button_icon = self::get_setting( 'button_icon', 'no' ) ) !== 'no' ) {
$class .= ' woosc-btn-has-icon';
$icon = apply_filters( 'woosc_button_normal_icon', self::get_setting( 'button_normal_icon', 'woosc-icon-1' ) );
if ( $button_icon === 'left' ) {
$class .= ' woosc-btn-icon-text';
$text = '<span class="woosc-btn-icon ' . esc_attr( $icon ) . '"></span><span class="woosc-btn-text">' . esc_html( $text ) . '</span>';
} elseif ( $button_icon === 'right' ) {
$class .= ' woosc-btn-text-icon';
$text = '<span class="woosc-btn-text">' . esc_html( $text ) . '</span><span class="woosc-btn-icon ' . esc_attr( $icon ) . '"></span>';
} else {
$class .= ' woosc-btn-icon-only';
$text = '<span class="woosc-btn-icon ' . esc_attr( $icon ) . '"></span>';
}
}
if ( $attrs['type'] === 'link' ) {
$output = '<a href="' . esc_url( '?add-to-compare=' . $attrs['id'] ) . '" class="' . esc_attr( $class ) . '" data-id="' . esc_attr( $attrs['id'] ) . '" data-product_name="' . esc_attr( $product_name ) . '" data-product_image="' . esc_attr( $product_image ) . '" rel="' . esc_attr( apply_filters( 'woosc_button_rel', 'nofollow' ) ) . '">' . $text . '</a>';
} else {
$output = '<button class="' . esc_attr( $class ) . '" data-id="' . esc_attr( $attrs['id'] ) . '" data-product_name="' . esc_attr( $product_name ) . '" data-product_image="' . esc_attr( $product_image ) . '">' . $text . '</button>';
}
}
return apply_filters( 'woosc_button_html', $output, $attrs['id'] );
}
Code file location:
woo-smart-compare/woo-smart-compare/wpc-smart-compare.php
WPC Smart Compare for WooCommerce [woosc_list] Shortcode
The Woo Smart Compare plugin shortcode, ‘woosc_list’, allows you to display a list of selected products for comparison. The shortcode accepts product IDs as parameters, which are then converted into integers. It returns a div containing a table of the specified products.
Shortcode: [woosc_list]
Parameters
Here is a list of all possible woosc_list shortcode parameters and attributes:
products
– list of product IDs to be displayed in comparison table.
Examples and Usage
Basic example – A simple usage of the shortcode to display a comparison table of products. The ‘products’ parameter is used to specify the IDs of the products to be compared.
[woosc_list products="1,2,3"/]
Advanced examples
Using the shortcode to display a comparison table of products by referencing both ID and title. The table will first try to load by ID, but if not found, it will try to load by title. Note that the product IDs must be separated by commas without spaces.
[woosc_list products="1,2,3"/]
Another advanced example would be using the shortcode to display a comparison table of a single product. This can be useful when you want to highlight a specific product on a page or post.
[woosc_list products="1"/]
Note: The product IDs in the ‘products’ parameter must correspond to actual product IDs in your WooCommerce store for the comparison table to display correctly.
PHP Function Code
In case you have difficulties debugging what causing issues with [woosc_list]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'woosc_list', [ $this, 'shortcode_list' ] );
Shortcode PHP function:
function shortcode_list( $attrs ) {
$attrs = shortcode_atts( [
'products' => null,
], $attrs );
if ( $attrs['products'] ) {
$attrs['products'] = array_map( 'absint', explode( ',', $attrs['products'] ) );
}
return '<div class="woosc_list woosc-list woosc_page woosc-page">' . self::get_table( false, $attrs['products'], 'page' ) . '</div>';
}
Code file location:
woo-smart-compare/woo-smart-compare/wpc-smart-compare.php
WPC Smart Compare for WooCommerce [woosc_quick_table] Shortcode
The Woo Smart Compare plugin shortcode ‘woosc_quick_table’ generates a quick comparison table for related products. It retrieves related products of a current product and displays them in a comparison table.
Shortcode: [woosc_quick_table]
Examples and Usage
Basic example – Display a quick comparison table for a specific product using the product’s ID.
[woosc_quick_table id="123" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [woosc_quick_table]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'woosc_quick_table', [ $this, 'shortcode_quick_table' ] );
Shortcode PHP function:
function shortcode_quick_table() {
global $product;
if ( ! $product ) {
return '';
}
$product_id = $product->get_id();
$related = wc_get_related_products( $product_id );
if ( empty( $related ) ) {
return '';
}
array_unshift( $related, $product_id );
$quick_table_class = 'woosc-quick-table label-column-' . self::get_setting( 'quick_table_label', 'no' );
ob_start();
?>
<section class="<?php echo esc_attr( apply_filters( 'woosc_quick_table_class', $quick_table_class ) ); ?>">
<?php
do_action( 'woosc_before_quick_table', $product );
echo apply_filters( 'woosc_quick_table_heading', '<h2>' . self::localization( 'quick_table_heading', esc_html__( 'Quick Comparison', 'woo-smart-compare' ) ) . '</h2>' );
?>
<div class="woosc-quick-table-products">
<?php
do_action( 'woosc_before_quick_table_products', $product );
echo self::get_table( false, $related, 'quick_table' );
do_action( 'woosc_after_quick_table_products', $product );
?>
</div>
<?php do_action( 'woosc_after_quick_table', $product ); ?>
</section>
<?php
return ob_get_clean();
}
Code file location:
woo-smart-compare/woo-smart-compare/wpc-smart-compare.php
Conclusion
Now that you’ve learned how to embed the WPC Smart Compare 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.
Leave a Reply