job.answiz.com
  • 4
Votes
name
name Punditsdkoslkdosdkoskdo

Woocommerce _ artefact price depending on country"

I have 2 questions regarding Woocommerce for Wordpress.

I'm working on a site that sells speakers to the Danish market.

Question one:

Can I detect the IP of a visitor and detect which country the person is from? I guess this can be done with some ClientLocation api.

Can I then disable all shopping relatet pages and buttons if a person is not from Denmark. Fx: hiding the add to cart, cart and checkout. I still want the persons to be able to see the prices, they should just not have the option to buy them.

Question 2:

Lets say that question one was sucessfull made. Then I would like to show different prices for other contries than Denmark. So if you are visiting the site from one country, the price is XXX and from another country the price is XXXX.
Let's say:

In USA the price is = $500 
And in UK the price = £400

(This has nothing to do with currency. The market price is just different in different countries.)

I've looked at this plugin: http://wordpress.org/plugins/woocomerce-price-by-country/ It allowed me to write different prices for each product, but when I testet it with http://geopeeker.com/ I hadn't worked at all.

Can you give me some pointets or some links to some plugins that you know of?

UPDATE

I managed to solve question 1. I store the visitors country in a cookie with IP location XML API And then I could just create an if statement, saying that if the country was not equal to Denmark, then the add to cart, cart etc. should be removed.

So yeah, I would really appreciate it if anyknow could give me an idea on how I can solve question 2.

I'm able to detect country, but not able to specify a price of each product to the given country.

2'nd update:

Just to let any interested readers know, I ended up buying this plugin. which is working perfectly!

This is one of the most common things asked by eCommerce store owners, and surely there are more than just one solution for this particular problem.

=> WooCommerce Booster

My favorite solution for the problem is Booster for WooCommerce because not only it allows you to set different prices depending upon country of your choice, but you can also change the currency for each country too.

Even you could set a single price, and make it change currency based upon location and it will automatically do the price calculation by fetching exchange rates. You may read more about it at WooCommerce Prices and Currencies by Country - Booster for WooCommerce

=> WooCommerce Price Based on Country

Although WooCommerce boster does the job for you, but it comes with a lot of added features and complexity. If you are sure not to use any of them then WooCommerce Price Based on Country is a handy solution. This plugin is dedicated to this single issue.

  • 1
Reply Report
=== WooCommerce Product Price Based on Countries ===
Contributors: oscargare
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=NG75SHRLAX28L
Tags: price based country, dynamic price based country, price by country, dynamic price, woocommerce, geoip
Requires at least: 3.6.1
Tested up to: 4.2.2
Stable tag: 1.3.5
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Add multicurrency support to WooCommerce, allowing you set product's prices in different currencies based on country of your site's visitor.

== Description ==

**WooCommerce Product Price Based on Countries** is a extension for WooCommerce that allows you to set prices and receive payments in different currencies.

The plugin detect automatically visitor's country using geolocation features and display the currency and price you have defined previously for this country (or region). 
You have two ways to set product's price for each region:

* Calculate price by applying a exchange rate.
* Set price manually.


**Key Features**

* Easy settings and integrated with Woocommerce settings page.
* Geolocation integrated with WooCommerce Gelocation feature.
* Multicurrency: allows to receive payments in different currencies, reducing the costs of currency conversions.
* Include regular price and sale price by region.
* It's possible set a exchange rate to automatically calculate price for a region.
* Automatically detects of customer's country, with price and currency set accordingly.
* Refresh price and currency on order preview, cart and shop when country changes on checkout page.
* Included action hook and shortcode to add a country selector to front-end.


**Requirements**

* WordPress 3.6 or later
* WooCommerce 2.3.0 or later.

== Installation ==

1. Download, install and activate the plugin.
1. Go to WooCommerce -> Settings -> Product Price Based on Country and configure as required.
1. Go to the product page and sets the price for the countries you have configured avobe.

= Adding a country selector to the front-end =

Once you’ve added support for multiple country and their currencies, you could display a country selector in the theme. You can display the country selector with a shortcode or as a hook.

**Shortcode**

[wcpbc_country_selector]

**PHP Code**

do_action('wcpbc_manual_country_selector');

= Customize country selector (only for developers) =

1. Add action "wcpbc_manual_country_selector" to your theme.
1. To customize the country selector:
	1. Create a directory named "woocommerce-product-price-based-on-countries" in your theme directory. 
	1. Copy to the directory created avobe the file "country-selector.php" included in the plugin.
	1. Work with this file.

== Frequently Asked Questions ==

= That way can I test that the prices are displayed correctly for a given country? =

If you are in a test environment, you can configure the test mode in the setting page.

In a production environment you can use a privacy VPN tools like [hola](http://hola.org/) or [ZenMate](https://zenmate.com/)

You should do the test in a private browsing window to prevent data stored in the session.

== Screenshots ==

1. /assets/screenshot-1.png
2. /assets/screenshot-2.png
3. /assets/screenshot-3.png
4. /assets/screenshot-4.png

== Changelog ==

= 1.3.5 =
* Added: Ready for WooCommerce 2.4

= 1.3.4 =
* Fixed: Country of Base Location not in list of countries.
* Added: Improved settings page.

= 1.3.3 =
* Fixed: The manual price is not saved in external/affiliate products.
* Fixed: The exchange rate only supports dot as decimal separator.
* Added: Support for WooCommerce Price Filter Widget (beta).

= 1.3.2 =
* Required: WooCommerce 2.3.0 or or later!
* Fixed: Incorrect currency conversion for variable products.
* Added: Integrate with WooCommerce geolocation function.
* Added: Improved test mode.
* Added: Radio button to select the price method (calculate by exchange rate or manually) for each product.

= 1.3.1 =
* Fixed: Price before discount not show for variable products with sale price.

= 1.3.0 =
* Added: Exchange rate to apply when price leave blank.
* Added: Hook and template to add a country selector.
* Fixed minor bugs.

= 1.2.5 =
* Fixed bug that breaks execution of cron jobs when run from wp-cron.php.
* Fixed bug: Error in uninstall procedure.

= 1.2.4 =
* Fixed bug that break style in variable products.
* Fixed bug: prices not show in variable products.

= 1.2.3 =
* Added: Sale price by groups of countries.
* Added: Refresh prices and currency when user changes billing country on checkout page.
* Fixed minor bugs.

= 1.2.2 =
* Fixed bug that not show prices per countries group when added a new variation using the "add variation" button.
* Fixed bug: product variation currency label is wrong.

= 1.2.1 =
* Fixed bug that not allow set prices in variable products.

= 1.2 =
* Added: REST service is replaced by GEOIP Database.
* Added: Improvements in the plugin settings page.
* Added: Debug mode

= 1.1 =
* Added: currency identifier per group of countries.
* Fixed bug in settings page.

= 1.0.1 =
* Fixed a bug that did not allow to add more than one group of countries.

= 1.0 =
* Initial release!

== Upgrade Notice ==

= 1.3.2 =
1.3.2 is a major update so it is important that you make backups before upgrade. Required WooCommerce 2.3.0 or later, ensure you have installed a compatible version of WooCommerce.
  • 1
Reply Report

0

For the 2nd part of your question: If you are only using simple product types (without variations) then you can add custom price fields to the product data page and filter the price using woocommerce_get_price_html.

add_filter('woocommerce_get_price_html','so24863612_custom_price');
function so24863612_custom_price(){
   global $post;
   $_postID = $post->ID;
   $product = get_product( $_postID );
   $UK_price = get_post_meta($_postID, '_UK_price', true); //loads custom meta data 
   $return_price = $product->get_regular_price(); //default to regular price
   if (!empty($UK_price)) { 
      $return_price = $UK_price;
   }
   return $return_price;
}

You can create and save custom fields on the product page like this:

//Display custom fields on product data page in admin
add_action( 'woocommerce_product_options_general_product_data', 'so24963039_display_custom_general_tab_fields' );
function so24963039_display_custom_general_tab_fields() {
    global $woocommerce, $post;
    $UK_price = get_post_meta( $post->ID, '_UK_price', true );

    woocommerce_wp_text_input(
        array(
        'id' => '_UK_price',
        'label' => __( 'UK Price (£)', 'woocommerce' ),
        'value' => $UK_price,
        'desc_tip' => 'false'
        )
    );
}

//Save custom fields to access via get_post_meta
add_action( 'woocommerce_process_product_meta', 'so24963039_save_custom_general_tab_fields' );
function so24963039_save_custom_general_tab_fields ($post_id) {

    $woocommerce_UK_price = $_POST['_UK_price'];
    if( !empty( $woocommerce_UK_price ) )
    update_post_meta( $post_id, '_UK_price', esc_attr( $woocommerce_UK_price ) );   

}

-----------------For products with Variations----------------------------

WARNING: Variable products are much more complicated and I'm not nearly as confident in this answer as I am with the simple products part above, but here's my current understanding either way. I had some mini-cart display issues that I had to hack around when using this method (which I will explain at the end), but the totals are calculated correctly in both the mini-cart and the regular cart.

First we want to add new fields to each variant on the variation tab of existing products:

add_action( 'woocommerce_product_after_variable_attributes', 'so24963039_variable_fields', 10, 2 ); //Display Fields
function so24963039_variable_fields( $loop, $variation_data ) {

echo '<tr><td>';
woocommerce_wp_text_input(
    array(
        'id' => '_variant_UK_price['.$loop.']',
        'label' => __( 'UK Price (£)', 'woocommerce' ),
        'desc_tip' => 'false',
        'value' => $variation_data['_variant_UK_price'][0]
    )
);
echo '</td></tr>';
}

We also need to add them dynamically whenever the user adds new variants on the edit product page:

add_action( 'woocommerce_product_after_variable_attributes_js', 'so24963039_variable_fields_js' ); //JS to add fields for dynamically added new variations
function so24963039_variable_fields_js(){ //add fields to new variations that get added 
echo '<tr><td>';
woocommerce_wp_text_input(
    array(
        'id' => '_variant_UK_price[ + loop + ]',
        'label' => __( 'UK Price (£)', 'woocommerce' ),
        'desc_tip' => 'false',
        'value' => $variation_data['_variant_UK_price'][0]
    )
);
echo '</td></tr>';
}

Then we save changes to the custom fields in the variation meta data:

add_action( 'woocommerce_process_product_meta_variable', 'so24963039_save_variable_fields', 10, 1 ); //Save variation fields
function so24963039_save_variable_fields( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) {
    $variable_sku = $_POST['variable_sku'];
    $variable_post_id = $_POST['variable_post_id'];

    // Variant Tier 1 Price
    $_variant_UK_price = $_POST['_variant_UK_price'];
    for ( $i = 0; $i < sizeof( $variable_sku ); $i++) {
        $variation_id = (int) $variable_post_id[$i];
        if ( isset( $_variant_UK_price[$i] ) ) {
        update_post_meta( $variation_id, '_variant_UK_price', stripslashes($_variant_UK_price[$i] ) );
        }
    }
}
}

Now that we have our custom variation meta data, we can access it in the custom price module like so:

add_filter('woocommerce_get_price_html','so24863612_custom_price');
function so24863612_custom_price(){
   global $post;
   $_postID = $post->ID;
   $product = get_product( $_postID );
   $product_type = $product->product_type;

   $UK_price = get_post_meta($_postID, '_UK_price', true); //covers simple products 
   if($product_type == 'variation'){ //override with variant prices
            $UK_price = get_post_meta($_postID, '_variant_$UK_price', true);
    }

   $return_price = $product->get_regular_price(); //default to regular price
   if (!empty($UK_price)) { 
      $return_price = $UK_price;
   }
   return $return_price;
}

Now, I believe that part should have everything working except for the mini-cart display. For some reason it seems like I just couldn't figure out how to get access to the variation meta data to force it to display properly in the mini cart - like I found where the mini-cart display was being generated but I was having trouble getting the right context path to access the custom variable so I ended up having to do that in the template-tags.php and pass an array of custom values to an optional parameter in my custom price function. This feels very 'wrong' in terms of how we should do things, but it gets the job done. I'm very open to hearing the 'correct' solution to this part of the problem.

In template-tags.php:

<div class="small-7 large-7 columns"><?php 
                                            $product_title = $_product->get_title();
                                            echo '<a class="cart_list_product_title" href="'.get_permalink($cart_item['product_id']).'">' . apply_filters('woocommerce_cart_widget_product_title', $product_title, $_product) . '</a>';
                                            echo '<div class="cart_list_product_price">';
                                            //original line: echo woocommerce_price($_product->get_price());

                                            /*Custom Price Override Block*/
                                            $_productID = $_product->id;
                                            $product_type = $_product->product_type;
                                            if($product_type == 'variation') {
                                                $custom_field_data = $_product->product_custom_fields;
                                                $regular_price = $custom_field_data['_regular_price'];
                                                $custom_UK_price = $custom_field_data['_variant_UK_price'];
                                                $custom_variant_prices = [$regular_price[0], $custom_UK_price[0]];
                                                echo so24863612_get_custom_price($_productID,  $custom_variant_prices ); 
                                            } else {
                                                echo so24863612_get_custom_price($_productID );
                                            }
                                            /*End Custom Price Override Block*/

                                            echo ' /</div>';
                                            echo '<div class="cart_list_product_quantity">'.__('Quantity', 'woocommerce').': '.$cart_item['quantity'].'</div>';

                                        ?></div>
  • 4
Reply Report