I have several products (WooCommerce) that are available to all countries.
Due to two products being under contract with a distributor, we have to restrict two products to only be sold outside the USA.
The two products should not show at all if they are trying to be accessed via the USA.
I've tried a couple of plugins and have one semi-working.
It disables the add to cart button, but I would rather it just remove the product from the site altogether, but only if the site is being accessed from the USA.
Everywhere else would be able to see the products.
you can use WC_Geolocation class to detect user location and then exclude certain product from being displayed by modifying the products query .
so you can use the following code to achive the desired results.
Based of LoicTheAztec Suggestion i have included another check if the user is logged in and his country is also US then we should also exclude the product regardless of his location.
function exclude_product_by_geoloacted_user_country($q)
{
$location = WC_Geolocation::geolocate_ip();
$country = $location['country'];
$exluded_product = [28, 27]; //you can add here the products that you want to exclude by id
if (is_user_logged_in() && WC()->customer->get_billing_country() == "US") {
$q->set('post__not_in', (array) $exluded_product);
return $q;
}
if ($country == "US") { //here you can specify the country code
$q->set('post__not_in', (array) $exluded_product);
}
return $q;
}
add_action('woocommerce_product_query', 'exclude_product_by_geoloacted_user_country');
place the code above in your functions.php and change the product ids and you are good to go.
code is tested with StoreFront Theme.
Related
Basically, I finished building custom plugin for my client.
the only thing after products added to cart, before the checkout.
user able to change the quantity of the products, is it possible to display the selected quantity, but disabled the options to read only so client will able to see the quantity in cart page that he selected but can't change it?
and to apply this only to products that I used with my plugin either product ids or better category id because all the products there.
other product display and able to change quantity regular
by the way its regular products not virtual and not Sold Individually i need to find a way to limit clients to change quantity for some products only in cart page!, and not in product page.
I really appreciate any help.
As mentioned in the comment, you can use the woocommerce_cart_item_quantity filter for that. So that might look something like this:
function 668763_change_quantity_input( $product_quantity, $cart_item_key, $cart_item ) {
$product_id = $cart_item['product_id'];
// whatever logic you want to determine whether or not to alter the input
if ( $your_condition ) {
return '<h3>' . $item['quantity'] . '</h3>';
}
return $product_quantity;
}
add_filter( 'woocommerce_cart_item_quantity', '668763_change_quantity_input', 10, 3);
This would be just a simple example to replace the input with a h3 element containing the quantity. It can easily be adjust to alter the quantity input element to your liking.
How to make woocommerce always show price including tax based on shop location and only in checkout would woocommerce show price depends on user country.
It is possible?
Thank you
A lot later, just in case someone else still needs it.
I don't know if you can show prices based on the shop location only before the checkout, but you can surely assign a default country to the user if they have never bought before.
If they are returning customers, it's right they see the taxes according to their country, if not so it's not a so bad idea to assign them as a default country the shop country.
Translated in code:
add_filter( 'default_checkout_billing_country',function( $country ){
if ( WC()->customer->get_is_paying_customer() ) {
return $country;
}
return wc_get_base_location()['country'];
} );
add_action('woocommerce_add_to_cart' ,function(){
if( !WC()->customer->get_is_paying_customer() ) {
$country = wc_get_base_location()['country'];
WC()->customer->set_billing_country( $country );
}
} );
The default currency in the template based n woocomeerce that I am using is British pounds. The currency in the target country is different. Based on searching I found few ways of changing the currency. What is the best way?
http://www.kriesi.at/support/topic/change-of-the-woocommerce-price-currency
http://wcdocs.woothemes.com/snippets/add-a-custom-currency-symbol/
Furthermore, in which function.php file should the code be included? (one within wootemplate folder or within wp-includes directory)
Use any of the link mentioned in the above question. Add the code to functions.php of the theme. If using child theme, add it to the functions.php of child.
Here you can check this code:
add_filter('woocommerce_currency_symbol', 'change_currency_symbol', 10, 2);
function change_currency_symbol( $currency_symbol, $currency ) {
switch( $currency ) {
case 'AUD': $currency_symbol = 'AUD $'; break;
}
return $currency_symbol;
}
If you want multi currency on your shop then try wordpress plugins for this functionality. but you must need to change settings from the woo-commerce end for checkout page & shipping cost calculation. plugins will switch currency at the shop but only few of them can able manage the same currency at cart & checkout page & while shipping cost calculation.
Is it possible to 'filter' which pages are shown in the 'edit' screen for pages ( http://cl.ly/6nLC ) in Wordpress? I have looked in the action / hook section of Wordpress for plugin developers but I could not find any.
What I am trying to accomplish is is that certain users can edit certain pages (and child pages) and other persons cannot edit those pages but might be able to edit other pages.
I have allready written a plugin which makes it possible to put different users in differtent groups, which now just needs to have different rights, which user is member of which group is stored in the user_meta table.
However if there is 'any' filter hook / method for this, can someone point this out, I think I will be able to go further from there.
Kind regards.
You can use a posts_where filter to add a condition to the SQL query to filter out some pages. A load-{filename} action can be used to ensure the filter is only applied when managing pages.
add_action('load-edit.php', 'my_load_edit_php_action');
function my_load_edit_php_action() {
if ($_GET['post_type'] !== 'page') return;
add_filter('posts_where', 'my_posts_where_filter');
}
function my_posts_where_filter($sql) {
if (current_user_can('your_capability')) {
global $wpdb;
$sql = " AND $wpdb->posts.ID NOT IN (1,2,3)" . $sql;
}
return $sql;
}
How to count number of visitors for post under specific category ? Can I make such plugin who can do the whole magic ? I don't want plugin users to modify theme files or add code snippets in other theme files ...
something along the lines of adding to the post meta could do what you're wanting.
<?php
add_action('the_content', 'myplugincb');
function myplugincb() {
global $wp_query;
if (count($wp_query->posts) == 1) { //just do this for individual posts/pages
$pid = $wp_query->posts[0]->ID;
$key = 'myplugin_post_visit_counter';
update_post_meta($pid, $key, get_post_meta($pid, $key)+1);
}
}
function myplugin_show_viewed($post_id) {
return get_post_meta($post_id, 'myplugin_post_visit_counter');
}
You'd have to change that quite a few different ways depending on your desired result. You probably want to use something like Google Analytics if you're wanting to see specifics on pages visited and where the user came from etc.