diff --git a/.wordpress-org/icon-256x256.png b/.wordpress-org/icon-256x256.png index d4ac838..bb89077 100644 Binary files a/.wordpress-org/icon-256x256.png and b/.wordpress-org/icon-256x256.png differ diff --git a/README.txt b/README.txt index f4224b1..b137f6d 100644 --- a/README.txt +++ b/README.txt @@ -3,8 +3,8 @@ Contributors: addonify Tags: compare, woocommerce compare, products comparison, compare products, compare woocommerce, addonify, woocommerce Requires at least: 5.9 -Tested up to: 6.2.2 -Stable tag: 1.1.10 +Tested up to: 6.3.1 +Stable tag: 1.1.11 Requires PHP: 7.4 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -74,6 +74,10 @@ Yes, Addonify Product Compare should work with all themes if the theme authors h Yes, you can select the fields from Dashboard > Addonify > Compare > Settings to display in the compare table. += Is there a shortcode for adding product compare button? + +Yes, there is. Use `[addonify_compare_button]` to add product compare button. `product_id` , `button_label`, `classes`, and `button_icon_position` are the shortcode attributes that can be used. Shortcode attribute, `product_id` is required in order to display the compare button outside the products loop. Value for shortcode attribute `classes` should be CSS classes separated by a space. The value for `button_icon_position`, should be either 'left', 'right' or 'none';. For more information [check doc.](https://docs.addonify.com/kb/woocommerce-compare-products/getting-started/compare-button/) + = I'm a developer, is it possible to customize frontend output? = Yes, you can do it. Copy template from "/plublic/templates" the plugin's folder and paste them inside "/addonify/addonify-compare-products" of your theme's folder. For more information, read the [plugin's documentation](https://docs.addonify.com/kb/woocommerce-compare-products/) @@ -98,6 +102,17 @@ Yes, you can do it. Copy template from "/plublic/templates" the plugin's folder == Changelog == += 1.1.11 - 04 August, 2023 = + +- Added: Option to enable compare product button for logged in user. +- Added: Options to enable compare product button on product single page. +- Added: Option to enable product button on products loop. +- Added: Shortcode, `[addonify_compare_button]`, for adding compare button. +- Updated: Plugin setting page link moved before the 'Deactivate' link in plugins list page. +- Updated: Compare dock is now visible only if there is compare button on a page. +- Tested: Up to WooCommerce version 8.0.3. +- Tested: WordPress version 6.3.1. + = 1.1.10 - 20 June, 2023 = - Fix: Translation issue. String "N/A" is now translation ready. #181 @@ -168,20 +183,20 @@ Yes, you can do it. Copy template from "/plublic/templates" the plugin's folder - Tested: with WordPress version 6.0.2. -== 1.0.3 - 7 August 2022 == += 1.0.3 - 7 August 2022 = - Updated: Removed button from button.addonify-cp-button in custom.js -== 1.0.2 - 29 July 2022 == += 1.0.2 - 29 July 2022 = - Updated: Readme.txt file. - Fixed: Author URL. -== 1.0.1 - 29 July 2022 == += 1.0.1 - 29 July 2022 = - Added: wordpress.org graphics. - Fixed: Typo in vue js files. -== 1.0.0 - 28 July 2022 == += 1.0.0 - 28 July 2022 = - New: Initial release \ No newline at end of file diff --git a/addonify-compare-products.php b/addonify-compare-products.php index a2fe132..cc5a9c5 100644 --- a/addonify-compare-products.php +++ b/addonify-compare-products.php @@ -10,7 +10,7 @@ * Plugin Name: Addonify - Compare Products For WooCommerce * Plugin URI: https://wordpress.org/plugins/addonify-compare-products/ * Description: Addonify Compare Products is a WooCommerce extension that allows website visitors to compare multiple products on your online store. - * Version: 1.1.10 + * Version: 1.1.11 * Author: Addonify * Author URI: https://addonify.com/ * License: GPL-2.0+ @@ -24,7 +24,8 @@ die; } -define( 'ADDONIFY_COMPARE_PRODUCTS_VERSION', '1.1.10' ); +define( 'ADDONIFY_COMPARE_PRODUCTS_VERSION', '1.1.11' ); +define( 'ADDONIFY_COMPARE_PRODUCTS_BASENAME', plugin_basename( __FILE__ ) ); define( 'ADDONIFY_CP_DB_INITIALS', 'addonify_cp_' ); define( 'ADDONIFY_CP_PLUGIN_PATH', dirname( __FILE__ ) ); @@ -64,8 +65,21 @@ function deactivate_addonify_compare_products() { */ function run_addonify_compare_products() { - $plugin = new Addonify_Compare_Products(); - $plugin->run(); + if ( class_exists( 'WooCommerce' ) ) { + $plugin = new Addonify_Compare_Products(); + $plugin->run(); + } else { + add_action( + 'admin_notices', + function() { + ?> +
'+o+"
\n\t\t\t\t'+o+"
\n\t\t\t\t0)) { + body.removeClass('addonify-compare-dock-is-visible'); + return; + } let product_ids = getLocalItem('product_ids'); @@ -282,6 +291,7 @@ * Display dock if products available in storage. */ function addonifyCompareProductsDisplayDock() { + if (compareItemsCount === 0) { body.removeClass('addonify-compare-dock-is-visible'); } else { diff --git a/public/assets/src/scss/components/_buttons.scss b/public/assets/src/scss/components/_buttons.scss index e6b087e..ef8248d 100644 --- a/public/assets/src/scss/components/_buttons.scss +++ b/public/assets/src/scss/components/_buttons.scss @@ -64,6 +64,8 @@ button.addonify-cp-fake-button { -------------------------------------------------------------------------- */ .addonify-cp-button, +.woocommerce-js .addonify-cp-button, +.woocommerce-js button.addonify-cp-button, .woocommerce ul.products li.product .button.addonify-cp-button, .woocommerce-page ul.products li.product .button.addonify-cp-button { diff --git a/public/class-addonify-compare-products-public.php b/public/class-addonify-compare-products-public.php index 0d76895..f52625a 100644 --- a/public/class-addonify-compare-products-public.php +++ b/public/class-addonify-compare-products-public.php @@ -38,6 +38,51 @@ class Addonify_Compare_Products_Public { */ private $version; + /** + * The label of compare button. + * + * @since 1.1.11 + * @access private + * @var string $compare_button_label The label of compare button. + */ + private $compare_button_label; + + /** + * The true|false value to display icon in compare button. + * + * @since 1.1.11 + * @access private + * @var boolean $display_compare_button_icon The boolean value to display icon in compare button. + */ + private $display_compare_button_icon; + + /** + * The icon of compare button. + * + * @since 1.1.11 + * @access private + * @var string $compare_button_icon The icon of compare button. + */ + private $compare_button_icon; + + /** + * The position of icon in compare button. + * + * @since 1.1.11 + * @access private + * @var string $compare_button_icon_position The position of icon in compare button. + */ + private $compare_button_icon_position; + + /** + * The template arguments needed to render compare button. + * + * @since 1.1.11 + * @access private + * @var array $compare_button_template_args The template arguments needed to render compare button. + */ + private $compare_button_template_args; + /** * Initialize the class and set its properties. @@ -59,24 +104,66 @@ public function __construct( $plugin_name, $version ) { */ public function public_init() { - if ( - ! class_exists( 'WooCommerce' ) || - (int) addonify_compare_products_get_option( 'enable_product_comparison' ) !== 1 - ) { + if ( addonify_compare_products_get_option( 'enable_product_comparison' ) !== '1' ) { return; } + if ( addonify_compare_products_get_option( 'enable_login_required' ) === '1' && ! is_user_logged_in() ) { + return; + } + + $this->compare_button_label = addonify_compare_products_get_option( 'compare_products_btn_label' ); + $this->display_compare_button_icon = addonify_compare_products_get_option( 'compare_products_btn_show_icon' ); + $this->compare_button_icon = addonify_compare_products_get_option( 'compare_products_btn_icon' ); + $this->compare_button_icon_position = addonify_compare_products_get_option( 'compare_products_btn_icon_position' ); + + $this->compare_button_template_args = $this->prepare_compare_button_template_args(); + // Register scripts and styles for the frontend. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); - // Add the compare button to the product catalog. - switch ( addonify_compare_products_get_option( 'compare_products_btn_position' ) ) { - case 'before_add_to_cart': - add_action( 'woocommerce_after_shop_loop_item', array( $this, 'render_compare_button' ), 5 ); - break; - default: - add_action( 'woocommerce_after_shop_loop_item', array( $this, 'render_compare_button' ), 15 ); + if ( addonify_compare_products_get_option( 'enable_product_comparison_on_archive' ) === '1' ) { + + // Add the compare button on the product catalog. + switch ( addonify_compare_products_get_option( 'compare_products_btn_position' ) ) { + case 'before_add_to_cart': + add_action( + 'woocommerce_after_shop_loop_item', + array( $this, 'render_compare_button' ), + 5 + ); + break; + default: + add_action( + 'woocommerce_after_shop_loop_item', + array( $this, 'render_compare_button' ), + 15 + ); + } + } + + if ( addonify_compare_products_get_option( 'enable_product_comparison_on_single' ) === '1' ) { + add_action( + 'woocommerce_before_add_to_cart_form', + array( $this, 'render_compare_button_before_single_cart_form' ) + ); + add_action( + 'woocommerce_after_add_to_cart_quantity', + array( $this, 'render_compare_button_after_single_quantity_field' ) + ); + add_action( + 'woocommerce_before_add_to_cart_button', + array( $this, 'render_compare_button_before_single_add_to_cart_button' ) + ); + add_action( + 'woocommerce_after_add_to_cart_button', + array( $this, 'render_compare_button_after_single_add_to_cart_button' ) + ); + add_action( + 'woocommerce_after_add_to_cart_form', + array( $this, 'render_compare_button_after_single_cart_form' ) + ); } // Add custom markup into footer to display comparison modal. @@ -100,6 +187,8 @@ public function public_init() { // Register shortocode to display comparison table in the comparison page. add_shortcode( 'addonify_compare_products', array( $this, 'render_shortcode_content' ) ); + + add_shortcode( 'addonify_compare_button', array( $this, 'compare_button_shortcode_callback' ) ); } @@ -110,12 +199,30 @@ public function public_init() { */ public function enqueue_styles() { - wp_enqueue_style( 'perfect-scrollbar', plugin_dir_url( __FILE__ ) . 'assets/build/css/conditional/perfect-scrollbar.css', array(), $this->version ); + wp_enqueue_style( + 'perfect-scrollbar', + plugin_dir_url( __FILE__ ) . 'assets/build/css/conditional/perfect-scrollbar.css', + array(), + $this->version, + 'all' + ); if ( is_rtl() ) { - wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'assets/build/css/public-rtl.css', array(), $this->version ); + wp_enqueue_style( + $this->plugin_name, + plugin_dir_url( __FILE__ ) . 'assets/build/css/public-rtl.css', + array(), + $this->version, + 'all' + ); } else { - wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'assets/build/css/public.css', array(), $this->version ); + wp_enqueue_style( + $this->plugin_name, + plugin_dir_url( __FILE__ ) . 'assets/build/css/public.css', + array(), + $this->version, + 'all' + ); } if ( (int) addonify_compare_products_get_option( 'load_styles_from_plugin' ) === 1 ) { @@ -142,9 +249,21 @@ public function enqueue_styles() { */ public function enqueue_scripts() { - wp_enqueue_script( 'perfect-scrollbar', plugin_dir_url( __FILE__ ) . 'assets/build/js/conditional/perfect-scrollbar.min.js', null, $this->version, true ); + wp_enqueue_script( + 'perfect-scrollbar', + plugin_dir_url( __FILE__ ) . 'assets/build/js/conditional/perfect-scrollbar.min.js', + null, + $this->version, + true + ); - wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'assets/build/js/public.min.js', array( 'jquery' ), $this->version, true ); + wp_enqueue_script( + $this->plugin_name, + plugin_dir_url( __FILE__ ) . 'assets/build/js/public.min.js', + array( 'jquery' ), + $this->version, + true + ); $localize_args = array( 'ajaxURL' => admin_url( 'admin-ajax.php' ), @@ -165,7 +284,62 @@ public function enqueue_scripts() { 'addonifyCompareProductsJSObject', $localize_args ); + } + + + /** + * Callback function for add_shortcode function to render compare button via shortcode. + * + * @since 1.1.11 + * + * @param array $atts Shortcode attributes. + */ + public function compare_button_shortcode_callback( $atts ) { + + $shortcode_atts = shortcode_atts( + array( + 'product_id' => 0, + 'button_label' => $this->compare_button_label, + 'classes' => '', + 'button_icon_position' => $this->compare_button_icon_position, + ), + $atts, + 'addonify_compare_button' + ); + + global $product; + + if ( isset( $shortcode_atts['product_id'] ) ) { + $product = wc_get_product( (int) $shortcode_atts['product_id'] ); + } + + if ( ! $product || ! ( $product instanceof WC_Product ) ) { + ob_start(); + echo esc_html__( 'Invalid product.', 'addonify-compare-products' ); + return ob_get_clean(); + } + + $button_template_args = array( + 'product' => $product, + 'button_label' => $shortcode_atts['button_label'], + 'classes' => array( + 'addonify-cp-shortcode-button', + $shortcode_atts['classes'], + ), + 'button_icon' => '', + ); + + if ( 'none' !== $shortcode_atts['button_icon_position'] ) { + $button_template_args['button_icon'] = addonify_compare_products_get_selected_compare_button_icon( $this->compare_button_icon ); + + $button_template_args['classes'][] = ( 'left' === $shortcode_atts['button_icon_position'] ) + ? 'icon-position-left' : + 'icon-position-right'; + } + ob_start(); + do_action( 'addonify_compare_products_compare_button', $button_template_args ); + return ob_get_clean(); } /** @@ -308,6 +482,30 @@ public function ajax_products_search_callback() { wp_die(); } + /** + * Prepare button label, button CSS classes, and button icon for compare button. + * + * @since 1.1.11 + */ + public function prepare_compare_button_template_args() { + + $button_args = array( + 'button_label' => $this->compare_button_label, + 'classes' => array(), + 'button_icon' => '', + ); + + if ( '1' === $this->display_compare_button_icon ) { + $button_args['button_icon'] = addonify_compare_products_get_selected_compare_button_icon( $this->compare_button_icon ); + } + + $button_args['classes'][] = ( 'left' === $this->compare_button_icon_position ) + ? 'icon-position-left' : + 'icon-position-right'; + + return $button_args; + } + /** * Generating "Compare" button @@ -316,7 +514,7 @@ public function ajax_products_search_callback() { */ public function render_compare_button() { - do_action( 'addonify_compare_products_compare_button' ); + do_action( 'addonify_compare_products_compare_button', $this->prepare_compare_button_template_args() ); } @@ -374,6 +572,99 @@ public function render_shortcode_content() { return ob_get_clean(); } + /** + * Render compare button in product single before cart form. + * + * @since 1.1.11 + */ + public function render_compare_button_before_single_cart_form() { + + $button_position = addonify_compare_products_get_option( 'compare_products_btn_position_on_single' ); + + if ( 'before_add_to_cart_form' === $button_position ) { + + echo '