• You MUST read the Babiato Rules before making your first post otherwise you may get permanent warning points or a permanent Ban.

    Our resources on Babiato Forum are CLEAN and SAFE. So you can use them for development and testing purposes. If your are on Windows and have an antivirus that alerts you about a possible infection: Know it's a false positive because all scripts are double checked by our experts. We advise you to add Babiato to trusted sites/sources or disable your antivirus momentarily while downloading a resource. "Enjoy your presence on Babiato"

Help with adding javascript code

leros2004

Member
Mar 15, 2019
48
15
8
Hi to all, having a conflict with 2 plugins, i contacted the authors, so the one suggested to add this code to the others plugin.
I contacted the 2nd author but he didn't reply.
So it is necessary some knowledge of programming which I do not have, maybe someone knows how to add this code:

Code:
/**
  * Support for Price Based on Country.
  */
$(document.body).on('wc_price_based_country_after_ajax_geolocation', function() {
    defaultPrice = priceContainer.html();
});

to this plugin:

Code:
<?php
/*
Plugin Name: Better Variation Price for Woocommerce
Plugin Slug: wbvp
Text Domain: better-variation-price-for-woocommerce
Description: Replace the Woocommerce variable products price range with the lowest price or the selected variation price.
Version: 1.2.2
Author: Josserand Gallot
Author URI: https://josserandgallot.com/
Text Domain: better-variation-price-for-woocommerce
Domain Path: /languages
*/

if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

if( ! class_exists( 'WBVP' ) ) :

class WBVP {

    /**
     * @var string
     */
    public $version;

    /**
     * @var string
     */
    private $plugin_path;

    /**
     * @var string
     */
    private $plugin_name;

    /**
     * @var array
     */
    private $options;

    public function __construct() {
        $this->version = '1.2.2';
        $this->plugin_path = plugin_dir_url( __FILE__ );
        $this->plugin_name = plugin_basename( __FILE__ );
        $this->options['better_variation'] = get_option( 'wbvp_better_variation', 'yes' );
        $this->options['show_lowest_price'] = get_option( 'wbvp_lowest_price', 'yes' );
        $this->options['hide_reset'] = get_option( 'wbvp_hide_reset', 'yes' );
    }

    /**
     * Initialize the plugin
     */
    public function initialize() {

        $this->setup_translation();

        if ( $this->options['better_variation'] == 'yes' )
            $this->better_variation();

        if ( $this->options['show_lowest_price'] == 'yes' )
            $this->show_lowest_price();

        if ( $this->options['hide_reset'] == 'yes' )
            $this->hide_reset();

        if ( is_admin() )
            $this->create_option_page();

    }

    /**
     * Activate Better Variation
     */
    private function better_variation() {
        add_action( 'wp_enqueue_scripts', function () {
            wp_enqueue_script( 'wbvp', $this->plugin_path . 'assets/js/plugin.min.js', ['jquery'], false, true );
        } );
    }

    /**
     * Hide reset variation link
     */
    private function hide_reset() {
        add_filter( 'woocommerce_reset_variations_link', '__return_null' );
    }

    /**
     * Show lowest price instead of price range
     */
    private function show_lowest_price() {
        add_filter( 'woocommerce_bundle_force_old_style_price_html', '__return_true' );
        add_filter( 'woocommerce_variable_sale_price_html', [$this, 'lowest_price_html'], 10, 2 );
        add_filter( 'woocommerce_variable_price_html', [$this, 'lowest_price_html'], 10, 2 );
    }

    public static function lowest_price_html ( $price, $product ) {

        $variation_prices = $product->get_variation_prices();
        $lowest_regular_price = min( $variation_prices['regular_price'] );
        $lowest_sale_price = min( $variation_prices['sale_price'] );

        if ( floatval( $lowest_sale_price ) < floatval( $lowest_regular_price ) ) {
            $lowest_variation_id = array_search( $lowest_sale_price, $variation_prices['sale_price'] );
        } else {
            $lowest_variation_id = array_search( $lowest_regular_price, $variation_prices['regular_price'] );
        }

        $lowest_variation = wc_get_product( $lowest_variation_id );
        $lowest_variation_price_html = $lowest_variation->get_price_html();

        $html_price = '<span class="price-from">' . __( 'From:', 'better-variation-price-for-woocommerce' ) . '</span> ' . $lowest_variation_price_html;

        return $html_price;

    }

    /**
     * Create Option Page
     */
    private function create_option_page() {

        /* Add section to Woocommerce product tab */
        add_filter( 'woocommerce_get_sections_products', function ( $sections ) {
            $sections['wbvp'] = __( 'Better Variation Price', 'better-variation-price-for-woocommerce' );
            return $sections;
        } );

        /* Create the option page */
        add_filter( 'woocommerce_get_settings_products', function ( $settings, $current_section ) {
            if ( $current_section != 'wbvp' ) return $settings;

            $wbvp_settings = [
                [
                    'name'        => __( 'Better Variation Price for Woocommerce', 'better-variation-price-for-woocommerce' ),
                    'id'        => 'wbvp',
                    'type'        => 'title',
                    'desc'        => __( 'The following options are used to configure Better Variation Price for Woocommerce', 'better-variation-price-for-woocommerce' ),
                ], [
                    'name'        => __( 'Better Variation Price', 'better-variation-price-for-woocommerce' ),
                    'id'        => 'wbvp_better_variation',
                    'type'        => 'checkbox',
                    'default'    => 'yes',
                    'desc'        => __( 'Change main price when selecting variation.', 'better-variation-price-for-woocommerce' )
                ], [
                    'name'        => __( 'Show lowest price', 'better-variation-price-for-woocommerce' ),
                    'id'        => 'wbvp_lowest_price',
                    'type'        => 'checkbox',
                    'default'    => 'yes',
                    'desc'        => __( 'Show lowest variation price instead of price range.', 'better-variation-price-for-woocommerce' )
                ], [
                    'name'        => __( 'Hide Reset Variations Link', 'better-variation-price-for-woocommerce' ),
                    'id'        => 'wbvp_hide_reset',
                    'type'        => 'checkbox',
                    'default'    => 'yes',
                    'desc'        => __( 'Hide the "clear" link that appears when you select a variation.', 'better-variation-price-for-woocommerce' )
                ],
            ];

            $wbvp_settings[] = [ 'type' => 'sectionend', 'id' => 'wbvp' ];
            return $wbvp_settings;

        }, 10, 2);

        /* Plugin settings shortcut */
        add_filter( 'plugin_action_links_' . $this->plugin_name, function ( $links ) {
            $action_links = array(
                'settings' => '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=products&section=wbvp' ) . '" aria-label="' . __( 'View Better Variation Price for Woocommerce settings', 'better-variation-price-for-woocommerce' ) . '">' . __( 'Settings', 'better-variation-price-for-woocommerce' ) . '</a>',
            );
            return array_merge( $action_links, $links );
        }, 10, 4 );

    }

    /**
     * Setup translation
     */
    private function setup_translation() {

        add_action( 'init', function() {
            load_plugin_textdomain( 'better-variation-price-for-woocommerce', false, 'better-variation-price-for-woocommerce/languages' );
        });

        add_filter( 'load_textdomain_mofile', function($mofile, $domain) {
            if ( 'better-variation-price-for-woocommerce' === $domain && false !== strpos( $mofile, WP_LANG_DIR . '/plugins/' ) ) {
                $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
                $mofile = WP_PLUGIN_DIR . '/' . dirname( plugin_basename( __FILE__ ) ) . '/languages/' . $domain . '-' . $locale . '.mo';
            }
            return $mofile;
        }, 10, 2 );

    }

}

/**
 * Run the main class
 */
function run_wbvp() {
    $wbvp = new WBVP();
    $wbvp->initialize();
}
run_wbvp();

endif;
 
Hi to all, having a conflict with 2 plugins, i contacted the authors, so the one suggested to add this code to the others plugin.
I contacted the 2nd author but he didn't reply.
So it is necessary some knowledge of programming which I do not have, maybe someone knows how to add this code:

Code:
/**
  * Support for Price Based on Country.
  */
$(document.body).on('wc_price_based_country_after_ajax_geolocation', function() {
    defaultPrice = priceContainer.html();
});

to this plugin:

Code:
<?php
/*
Plugin Name: Better Variation Price for Woocommerce
Plugin Slug: wbvp
Text Domain: better-variation-price-for-woocommerce
Description: Replace the Woocommerce variable products price range with the lowest price or the selected variation price.
Version: 1.2.2
Author: Josserand Gallot
Author URI: https://josserandgallot.com/
Text Domain: better-variation-price-for-woocommerce
Domain Path: /languages
*/

if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

if( ! class_exists( 'WBVP' ) ) :

class WBVP {

    /**
     * @var string
     */
    public $version;

    /**
     * @var string
     */
    private $plugin_path;

    /**
     * @var string
     */
    private $plugin_name;

    /**
     * @var array
     */
    private $options;

    public function __construct() {
        $this->version = '1.2.2';
        $this->plugin_path = plugin_dir_url( __FILE__ );
        $this->plugin_name = plugin_basename( __FILE__ );
        $this->options['better_variation'] = get_option( 'wbvp_better_variation', 'yes' );
        $this->options['show_lowest_price'] = get_option( 'wbvp_lowest_price', 'yes' );
        $this->options['hide_reset'] = get_option( 'wbvp_hide_reset', 'yes' );
    }

    /**
     * Initialize the plugin
     */
    public function initialize() {

        $this->setup_translation();

        if ( $this->options['better_variation'] == 'yes' )
            $this->better_variation();

        if ( $this->options['show_lowest_price'] == 'yes' )
            $this->show_lowest_price();

        if ( $this->options['hide_reset'] == 'yes' )
            $this->hide_reset();

        if ( is_admin() )
            $this->create_option_page();

    }

    /**
     * Activate Better Variation
     */
    private function better_variation() {
        add_action( 'wp_enqueue_scripts', function () {
            wp_enqueue_script( 'wbvp', $this->plugin_path . 'assets/js/plugin.min.js', ['jquery'], false, true );
        } );
    }

    /**
     * Hide reset variation link
     */
    private function hide_reset() {
        add_filter( 'woocommerce_reset_variations_link', '__return_null' );
    }

    /**
     * Show lowest price instead of price range
     */
    private function show_lowest_price() {
        add_filter( 'woocommerce_bundle_force_old_style_price_html', '__return_true' );
        add_filter( 'woocommerce_variable_sale_price_html', [$this, 'lowest_price_html'], 10, 2 );
        add_filter( 'woocommerce_variable_price_html', [$this, 'lowest_price_html'], 10, 2 );
    }

    public static function lowest_price_html ( $price, $product ) {

        $variation_prices = $product->get_variation_prices();
        $lowest_regular_price = min( $variation_prices['regular_price'] );
        $lowest_sale_price = min( $variation_prices['sale_price'] );

        if ( floatval( $lowest_sale_price ) < floatval( $lowest_regular_price ) ) {
            $lowest_variation_id = array_search( $lowest_sale_price, $variation_prices['sale_price'] );
        } else {
            $lowest_variation_id = array_search( $lowest_regular_price, $variation_prices['regular_price'] );
        }

        $lowest_variation = wc_get_product( $lowest_variation_id );
        $lowest_variation_price_html = $lowest_variation->get_price_html();

        $html_price = '<span class="price-from">' . __( 'From:', 'better-variation-price-for-woocommerce' ) . '</span> ' . $lowest_variation_price_html;

        return $html_price;

    }

    /**
     * Create Option Page
     */
    private function create_option_page() {

        /* Add section to Woocommerce product tab */
        add_filter( 'woocommerce_get_sections_products', function ( $sections ) {
            $sections['wbvp'] = __( 'Better Variation Price', 'better-variation-price-for-woocommerce' );
            return $sections;
        } );

        /* Create the option page */
        add_filter( 'woocommerce_get_settings_products', function ( $settings, $current_section ) {
            if ( $current_section != 'wbvp' ) return $settings;

            $wbvp_settings = [
                [
                    'name'        => __( 'Better Variation Price for Woocommerce', 'better-variation-price-for-woocommerce' ),
                    'id'        => 'wbvp',
                    'type'        => 'title',
                    'desc'        => __( 'The following options are used to configure Better Variation Price for Woocommerce', 'better-variation-price-for-woocommerce' ),
                ], [
                    'name'        => __( 'Better Variation Price', 'better-variation-price-for-woocommerce' ),
                    'id'        => 'wbvp_better_variation',
                    'type'        => 'checkbox',
                    'default'    => 'yes',
                    'desc'        => __( 'Change main price when selecting variation.', 'better-variation-price-for-woocommerce' )
                ], [
                    'name'        => __( 'Show lowest price', 'better-variation-price-for-woocommerce' ),
                    'id'        => 'wbvp_lowest_price',
                    'type'        => 'checkbox',
                    'default'    => 'yes',
                    'desc'        => __( 'Show lowest variation price instead of price range.', 'better-variation-price-for-woocommerce' )
                ], [
                    'name'        => __( 'Hide Reset Variations Link', 'better-variation-price-for-woocommerce' ),
                    'id'        => 'wbvp_hide_reset',
                    'type'        => 'checkbox',
                    'default'    => 'yes',
                    'desc'        => __( 'Hide the "clear" link that appears when you select a variation.', 'better-variation-price-for-woocommerce' )
                ],
            ];

            $wbvp_settings[] = [ 'type' => 'sectionend', 'id' => 'wbvp' ];
            return $wbvp_settings;

        }, 10, 2);

        /* Plugin settings shortcut */
        add_filter( 'plugin_action_links_' . $this->plugin_name, function ( $links ) {
            $action_links = array(
                'settings' => '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=products&section=wbvp' ) . '" aria-label="' . __( 'View Better Variation Price for Woocommerce settings', 'better-variation-price-for-woocommerce' ) . '">' . __( 'Settings', 'better-variation-price-for-woocommerce' ) . '</a>',
            );
            return array_merge( $action_links, $links );
        }, 10, 4 );

    }

    /**
     * Setup translation
     */
    private function setup_translation() {

        add_action( 'init', function() {
            load_plugin_textdomain( 'better-variation-price-for-woocommerce', false, 'better-variation-price-for-woocommerce/languages' );
        });

        add_filter( 'load_textdomain_mofile', function($mofile, $domain) {
            if ( 'better-variation-price-for-woocommerce' === $domain && false !== strpos( $mofile, WP_LANG_DIR . '/plugins/' ) ) {
                $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
                $mofile = WP_PLUGIN_DIR . '/' . dirname( plugin_basename( __FILE__ ) ) . '/languages/' . $domain . '-' . $locale . '.mo';
            }
            return $mofile;
        }, 10, 2 );

    }

}

/**
 * Run the main class
 */
function run_wbvp() {
    $wbvp = new WBVP();
    $wbvp->initialize();
}
run_wbvp();

endif;
If you explain to us what are you trying to do, what plugins are you using, and what is supposed to do this code, MAYBE we can help you better
 
If you explain to us what are you trying to do, what plugins are you using, and what is supposed to do this code, MAYBE we can help you better
The problem:
In the variable products when i select a variation everything works OK.
When i deselect or click the clear button the price is not showing.
I found that the problem is a conflict with the 2 plugins:

1) https://wordpress.org/plugins/woocommerce-product-price-based-on-countries/

2) https://wordpress.org/plugins/better-variation-price-for-woocommerce/

So the 1st plugin author offered this solution:
Code:
Hi there,

I can’t solve the problem in the code of Price Based on Country, but @josserand can add support for the AJAX geolocation feature of Price Based on Country by adding these two lines of javascript code:

/**
  * Support for Price Based on Country.
  */
$(document.body).on('wc_price_based_country_after_ajax_geolocation', function() {
    defaultPrice = priceContainer.html();
});
 
AdBlock Detected

We get it, advertisements are annoying!

However in order to keep our huge array of resources free of charge we need to generate income from ads so to use the site you will need to turn off your adblocker.

If you'd like to have an ad free experience you can become a Babiato Lover by donating as little as $5 per month. Click on the Donate menu tab for more info.

I've Disabled AdBlock