Is there a way/plugin in WordPress WooCommerce to restrict a certain product from being DOWNLOADED in a certain region. Remember this is not suppose to be site wide, but on a product to product basis.
What I imagine is that on checkout if a product has download restrictions enabled a function pulls the current user’s location (country) and compares it against an array of permitted countries for that product. If there is a match the check in proceeds if not, it returns a message informing the user that the product they requested is not available for download in their country. THE QUESTION IS, DOES SUCH A PLUGIN, FEATURE, FUNCTION or SNIPPET exist, if so where?
UPDATE: Seeing that there was no answer I have gone ahead and started creating something on my own. I have no previous PHP experience so please help me make this code concise. You can try it. IS THIS CORRECT?
UPDATE (SOLUTION): Woocommerce now has a built in functionality that checks user location and stores it for the shop owner to use in custom functions, go wild with it 🙂
The following code goes into your theme’s functions.php file. It will add a “Region Settings” panel to your product page’s add/edit page under the “General Tab”. It has two options, “restriction type:” which can be set to “Allow” or “Deny” and the “Regions: ” option where you specify the countries that will be affected. If a product’s region settings are not set, it will allow everyone to access it.
/**
* Mazwi WooCommerce Region Control BETA
* ------------------------------------
*
*
*
* Execute code if the user's country (set for each product) is allowed
*
* Author: Taf Makura
* Thanks to Remi Corson's Tutorial
*/
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
// Display Fields
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
?>
<?php
// Select
woocommerce_wp_select(
array(
'id' => '_restriction-type',
'label' => __( 'Restriction type', 'woocommerce' ),
'options' => array(
'allow' => __( 'Allow', 'woocommerce' ),
'deny' => __( 'Deny', 'woocommerce' ),
)
)
);
// Create Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_regions',
'label' => __( 'Regions', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Please enter two letter country codes. Each country code should be followed by a coma Example: ZW, AU, ZA, US ', 'woocommerce' )
)
);
echo '</div>';
}
function woo_add_custom_general_fields_save( $post_id ){
// Select
$woocommerce_select = $_POST['_restriction-type'];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, '_restriction-type', esc_attr( $woocommerce_select ) );
// Textarea
$woocommerce_textarea = $_POST['_regions'];
if( !empty( $woocommerce_textarea ) )
update_post_meta( $post_id, '_regions', esc_html( $woocommerce_textarea ) );
}
The following code goes into the template .php file where the conditional execution is suppose to happen. I can imagine if you place the add to cart loop (add to cart button) here it will allow you to control which products can be bought in certain countries. On a product by product basis.
<?php global $woocommerce;
// Get restriction type (deny or allow) for current product
$restriction_type = get_post_meta( $post->ID, '_restriction-type', true );
// Get region(s) the above restriction type is applied to
$regions = get_post_meta( $post->ID, '_regions', true );
// Turns this string into an array.
$regions_array = (explode(',', str_replace('/s+/','', $regions)));
// Get users current IP location from WooCommerce
$base_country = (..... YOU NEED TO GET THE USER LOCATION ISO COUNTRY CODE ......)
// If user's current IP location is either allowed, is not denied or is not set in the region settings = success
if( $restriction_type == 'allow' && in_array($base_country , $regions_array) || $restriction_type == 'deny' && !in_array($base_country , $regions_array) || $restriction_type == '' || $regions == '' ) {
if ($restriction_type == '' || $regions == '') {
// Code to execute on success if a product is not set (NOTE: It will not be restricted)
echo('This product's region control has not been set, you can set it in WP Admin');
}
// Code to execute on success if a products region settings are set to allow access
echo('YOU ARE IN');
} else {
// Code to execute when region is restricted
echo(' you are restricted,');
}
?>
I’m not sure if you saw/tried this, but according to http://docs.woothemes.com/document/configuring-woocommerce-settings/ you can do what you are asking for.