WordPress.org

Support

Support » Plugins and Hacks » [Resolved] [Plugin: eShop] Offer free shipping by country/zone?

[Resolved] [Plugin: eShop] Offer free shipping by country/zone?

  • daymobrew

    @daymobrew

    I would like to offer customers free shipping if the cart is over a certain amount. That amount will be different based on the destination country.

    I realise that this is not possible within the module settings so I am looking at overriding the is_shipfree() function from cart-functions.php.

    How can I access the destination country and shipping zone number?
    Is $_POST[‘country’] available?

    http://wordpress.org/extend/plugins/eshop/

Viewing 15 replies - 1 through 15 (of 26 total)
  • esmi

    @esmi

    Forum Moderator

    Try checking the eShop $_SESSION.

    daymobrew

    @daymobrew

    I found $_POST[‘ship_country’] and then I got the zone:

    // Check shipping country for free shipping – 60 for Ireland (zone 1) and 100 for UK/Europe (zone 2).
    $country_code=$wpdb->escape($_POST[‘ship_country’]);
    $country_table=$wpdb->prefix.’eshop_countries’;
    $ship_zone = $wpdb->get_var(“SELECT zone FROM $country_table WHERE code=’$country_code’ limit 1”);

    I tried redeclaring is_shipfree() in my theme’s functions.php but I get the “Cannot redeclare is_shipfree() (previously declared in /wp-content/plugins/eshop/cart-functions.php:447)” error message.

    I know that is_shipfree() has a function_exists() check but it must get loaded before my code.

    How can I resolve this? Do I have to write a small plugin?

    daymobrew

    @daymobrew

    I put the new is_shipfree() function in wp-content/plugins/aaa-eshop-shipfree/aaa-eshop-shipfree.php (to help it be activated earlier than eshop).

    Unfortunately when I active the plugin WordPress says that is_shipfree() has already been declared. I could deactivate eshop then activate my plugin and then eshop but I am reluctant to do this.

    When I put the file in wp-content/mu-plugins/aaa-eshop-shipfree.php but it didn’t appear to get executed.

    esmi

    @esmi

    Forum Moderator

    You’re right about the function in eShop loading before yours. But we could add an action or a filter to the is_shipfree() core function that you could then use in an add-on plugin or via your theme’s functions.php.

    Which would you prefer – action or filter?

    daymobrew

    @daymobrew

    I don’t have a preference.
    My first thought was a filter where the function’s return value is modified.
    http://pastebin.com/P95V3sRh
    The only variable I could think of passing was $total. Adding more will only lead to others requesting additional parameters.

    An action would be pretty much the same.

    esmi

    @esmi

    Forum Moderator

    I’ve flagged this topic up for eShop’s main developer. If he does manage come up with something soon, he’ll fill you in with the details so you can manually update your copy of the plugin immediately. any changes will be them rolled into the next plugin update.

    daymobrew

    @daymobrew

    Thanks.
    I think that I will modify my copy per my pastebin.com idea so that am not blocking my client.

    Plugin Author elfin

    @elfin

    changed for next release:

    [removed code]

    making a further necessary change.

    Plugin Author elfin

    @elfin

    codes amended, let me know if this uits your needs.

    if (!function_exists('is_shipfree')) {
    	function is_shipfree($total){
    		global $blog_id,$eshopoptions;
    		$shipfree = false;
    		$amt=$eshopoptions['discount_shipping'];
    		if(isset($_SESSION['eshop_discount'.$blog_id]) && eshop_discount_codes_check()){
    			$chkcode=valid_eshop_discount_code($_SESSION['eshop_discount'.$blog_id]);
    			if($chkcode && apply_eshop_discount_code('shipping'))
    				$shipfree = true;
    		}
    		if($amt!='' && $amt <= $total)
    			$shipfree = true;
    
    		return apply_filters('eshop_is_shipfree',$shipfree, $total);
    	}
    }

    and for some reason this one is niggling me – the filter name will not change, nor its placement – but I may change the code.

    daymobrew

    @daymobrew

    Perfect, thanks.
    Here is my working code:

    // Check shipping country for free shipping - 60 for Ireland (zone 1) and 100 for UK/Europe (zone 2).
    add_filter('eshop_is_shipfree', free_shipping_checks, 10, 2);
    
    function free_shipping_checks($shipfree,$total) {
      global $wpdb;
    
      $country_code=$_POST['ship_country'];
      $country_table=$wpdb->prefix.'eshop_countries';
      $ship_zone = $wpdb->get_var("SELECT zone FROM $country_table WHERE code='$country_code' limit 1");
    
      // Ireland is zone 1.
      if (($ship_zone == 1) && ($total >= 60)) {
        return true;
      }
      // UK/Europe is zone 2.
      if (($ship_zone == 2) && ($total >= 100)) {
        return true;
      }
    
      return $shipfree;
    }

    Hi Daymobrew,

    how did you solve this?
    I need this for my client too.

    Where did you paste that code?
    I tried to paste it in cart-functions.php
    But my shopping cart went blank.

    Can you help me out?

    Regards

    elfin’s update to is_shipfree() goes into eshop/cart-functions.php.

    I put my code in the theme’s functions.php (though I should move it to a site plugin).

    Thanks for your quick reply!!!
    I will give it a try.

    Yes, a plugin would be nice. I think useful for lots of people.

    A “site plugin” is not a plugin is unlikely to be useful to others as it would generally contain snippets of code for use on a specific site.

    Putting my free shipping code into a plugin would definitely not be useful to others as the values are specific to my client’s site.

    Just put it in your theme’s functions.php. That is an appropriate location.

    Sorry for the confusion. I should not have mentioned “site plugin”

    No problem!
    Maybe i`m not that good in coding, but for me its not working.

    In functions.php i pasted this, just before the last ?> :

    if function free_shipping_checks($shipfree,$total) {
      global $wpdb;
    
      $country_code=$_POST['ship_country'];
      $country_table=$wpdb->prefix.'eshop_countries';
      $ship_zone = $wpdb->get_var("SELECT zone FROM $country_table WHERE code='$country_code' limit 1");
    
      // Netherlands is zone 1.
      if (($ship_zone == 1) && ($total >= 75)) {
        return true;
      }
      // UK/Europe is zone 2.
      /*if (($ship_zone == 2) && ($total >= 100)) {
        return true;
      }*/
    
      return $shipfree;
    }

    In cart-functions.php i pasted this:

    // Check shipping country for free shipping - 60 for Ireland (zone 1) and 100 for UK/Europe (zone 2).
    add_filter('eshop_is_shipfree', free_shipping_checks, 10, 2);
    
    if (!function_exists('is_shipfree')) {
    	function is_shipfree($total){
    		global $blog_id,$eshopoptions;
    		$shipfree = false;
    		$amt=$eshopoptions['discount_shipping'];
    		if(isset($_SESSION['eshop_discount'.$blog_id]) && eshop_discount_codes_check()){
    			$chkcode=valid_eshop_discount_code($_SESSION['eshop_discount'.$blog_id]);
    			if($chkcode && apply_eshop_discount_code('shipping'))
    				$shipfree = true;
    		}
    		if($amt!='' && $amt <= $total)
    			$shipfree = true;
    
    		return apply_filters('eshop_is_shipfree',$shipfree, $total);
    	}
    }

    Am i doing well?

Viewing 15 replies - 1 through 15 (of 26 total)
  • The topic ‘[Resolved] [Plugin: eShop] Offer free shipping by country/zone?’ is closed to new replies.