Jump to content

getProducts() in AddressCore override


Plexy89

Recommended Posts

Hi there,

 

I need a very product specific modification where a specific product ID can only be delivered to a specific range of postal codes.

 

I've managed to override AddressCore to specify the postal codes which should be allowed and that works fine, but I need to be able to get the list of products in the cart in there as well - as it's only one product that can only be delivered to a specific range of postal codes.

 

Here's my code this far.

 

Many thanks in advance!

class Address extends AddressCore
{
    protected 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'];
    }

    public static function getZoneById($id_address)
    {
        $postcode = self::getPostcodeByAddress($id_address);
$products = $params['cart']->getProducts(true);


        if (in_array($postcode, Array(51252)) ) //The ZIP codes which will be allowed. To do: Add in && Cart Product ID == XXX 
            return 1;
        else
        return 10; //This doesn't exist so it will block the customer from ordering if it doesn't have a ZIP in the above range.
    }
}
Link to comment
Share on other sites

Hello, can you tell what you trying to achieve. Do you want to get products depending on postal code and remove them ? Or you want to prevent from adding that product to cart ?

 

Hi there,

 

Sorry for not being clear enough - I'll try to explain better. :)

I want the product ID's from the cart and then check if it's a specific ID (e.g. ID 987).

 

IF ID 987 is in the cart and the customer has postal number 51252 he will be able to check out. (return 1)

IF ID 987 is in the cart and the customer has postal number 11111 he won't be able to check out (return 10)

IF ID 987 is NOT in the cart the customer will be able to check out no matter which postal number. (return 1)

 

I hope this code is self-explanatory, what I want to do with this code is to replace $ProductIDs with the customer's current cart content.

class Address extends AddressCore
{
    protected 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'];
    }

    public static function getZoneById($id_address)
    {
        $postcode = self::getPostcodeByAddress($id_address);
        
        $ProductIDs = array(1,23,111,95,987);

        if (in_array($postcode, Array(51252))) { return 1; }    
        else {    if(in_array(987, $ProductIDs)) { return 10; } else { return 1; }    }    
    }
}
Edited by Plexy89 (see edit history)
Link to comment
Share on other sites

Finally got it to work myself - sharing the code in case anyone (which I doubt :) ) need it.

 

Also created a new table (shop_postnummer) which holds the Zip numbers that should be allowed to order this specific product ID.

class Address extends AddressCore
{
    protected 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'];
    }

    
    public static function getZoneById($id_address)
    {
        $postcode = self::getPostcodeByAddress($id_address);
        global $cookie ;

        $db = new mysqli(_DB_SERVER_, _DB_USER_, _DB_PASSWD_, _DB_NAME_);
        $result = $db->query("SELECT b.id_product as id FROM "._DB_PREFIX_."cart a, "._DB_PREFIX_."cart_product b WHERE a.id_cart = " . (int) $cookie->id_cart . " AND  a.id_cart = b.id_cart");
        if($result){
            while ($row = $result->fetch_object()){
                $ProductIDsArray[] = $row->id;
            }
            $result->close();
        } else {
            $ProductIDsArray[] = NULL;
        }

        $mysqli = new mysqli(_DB_SERVER_, _DB_USER_, _DB_PASSWD_, _DB_NAME_);
        if ($result = $mysqli->query("SELECT * FROM "._DB_PREFIX_."postnummer where postnummer = '".$postcode."' limit 1 ")) {
            if($result->num_rows > 0) {
                $WithinRange = 1;
                } else {
                $WithinRange = 0;
            }
                
        }

        if ($WithinRange === 1) {
            return 1;
        }    
        else {    
            if(in_array(9, $ProductIDsArray)) {
            return 10;
            } else {
            return 1;
            }    
        }    

} }
Edited by Plexy89 (see edit history)
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...