Jump to content
thirty bees forum
  • 0

Managing Carriers by city, getting costs


Wartin

Question

Hello. I'm trying to set carriers in TB 1.1.x. I can't find a module for TB nor PS that manages shipment costs in Argentina.

I did find an SDK (Andreani's) that can make queries to ask for the price, based on postal code. Also, I found a module for Magento and an old version of MercadoPago's module that had shipping management (but doesn't work anymore, and had payment too).

Two questions:

1) is there a shipping module structure that I can use to adapt the queries and create a module for using this API? I managed to print in product's page the shipping price that is given to me in JSON format (using Andreani's API), but that's not enough.

2) is it posible to set carriers based on just one City? locally we want to send the packages without using mail, as there are cheap services we prefer. I read about how to make it by countries (making a new zone and moving the country and it's states in)

 

Any help will be really appreciated.

Thank you in advance.

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Well, I could solve the second question, about using one specific carrier for one (or more) cities (in fact postal codes).

I followed this post: https://www.prestashop.com/forums/topic/160695-solved-carrier-by-postcode-the-easy-way/

but didn't work, so I'll write my steps. It's my first override, if there is something wrong just tell me, please.


1) Go to Localization -> Zone. Add a new one. In my case I used the name of my city. It could be anything. Remember the Zone ID of this zone, we'll need it.

2) Go to Transport -> Carriers. Create a new carrier, and select just the zone we've just created. Put some price and weight ranges. Select 'most expensive' in out-of-range (It doesn't work if you don't, I don't know why)

3) Copy "classes/Address.php" to "override/classes/Address.php".

4) In line 37 change:  class AddressCore extends ObjectModel
to:
    class Address extends AddressCore

Add a } in line 353 (at the very end of the function getZoneById()

delete lines 39-309

Line 322, function getZoneById($idAddress), we'll add some lines at start. 2450 is the Postal Code of the city you want to use with a specific Carrier (change it with yours). As it's an array, it could be more than one postal code (just separate them with commas). where it says '9', write your new Zone ID

    public static function getZoneById($idAddress)
    {
        $postcode=self::getPostcodeByAddress($idAddress);

        if(in_array($postcode,array(2450))){
            return 9;
        }
        else{


Now go just before the function getZoneByAddress, and add this function:

/**
* Return postcode of address
*
* @param $id_address Address id
* @return integer postcode
*/

public static function getPostcodeByAddress($id_address){

        $row = Db::getInstance()->getRow('
        SELECT `postcode`
        FROM '._DB_PREFIX_.'address a
        WHERE a.`id_address` = '.(int)($id_address));

        return $row['postcode'];
}


Ready. As we have changed some code, we need to make TB read the changes. Delete /cache/class_index.php

Go and try to checkout, it should show the carrier you made specially for our new zone.

 

Address.php

  • Like 1
Link to comment
Share on other sites

  • 0
5 hours ago, Wartin said:

It's my first override, if there is something wrong just tell me, please.

Good job for a first override. Keep them going 🙂

My insights:

  • Override should not contain anything that is not needed. You should delete all methods that you didn't modified, such as aliasExist or getAddressIdBySupplierId. 
  • When you override method, you should always try to call parent version. Sometimes it's not possible, but in your case it is. Your getZoneById should look something like this:
    public static function getZoneById($idAddress)
    {
	   
        $postcode=self::getPostcodeByAddress($idAddress);
        if (in_array($postcode,array(2450))) {
            return 9;
        }

        return parent::getZoneById($idAddress);
    }

Your new business logic at the very beginning of the method. If the postcode is 2450 then this method will return 9 (id of your new carrier). Otherwise, the original version of the method (from /classes/Address.php) will be called. Calling parent method is very important. It makes sure that your override will work correctly with future versions of thirtybees.

The result override might look like this:

class Address extends AddressCore
{

    /**
     * Return postcode of address
     *
     * @param int $id_address Address id
     * @return string postcode
     * @throws PrestaShopDatabaseException
     * @throws PrestaShopException
     */
    private static function getPostcodeByAddress($id_address)
    {
        $row = Db::getInstance()->getRow('
        SELECT `postcode`
        FROM '._DB_PREFIX_.'address a
        WHERE a.`id_address` = '.(int)($id_address));

        return $row['postcode'];
    }

    /**
     * Get zone id for a given address
     *
     * @param int $idAddress Address id for which we want to get zone id
     *
     * @return int Zone id
     *
     * @throws PrestaShopDatabaseException
     * @throws PrestaShopException
     * @since   1.0.0
     * @version 1.0.0 Initial version
     */
    public static function getZoneById($idAddress)
    {
        $postcode = static::getPostcodeByAddress($idAddress);
        if  (in_array($postcode,array(2450))) {
            return 9;
        }
        
        return parent::getZoneById($idAddress);
    }

  

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...