Kryptronic Software Support Forum

You are viewing this forum as a guest. Login to an existing account, or create a new account, to reply to topics and to create new topics.

#1 12-21-2017 16:25:22

dskowron
Member
Registered: 11-26-2008
Posts: 127

Interesting Shipping Scenario - Array Help Needed

I need to find out if an order contains one of our new t-shirts or not. If it does, I need to adjust the shipping cost accordingly. I want to use the global "ecom.customship" value of "delitems" to count how many t-shirts (if any) are present in an order. The problem I am having is figuring out how to programatically refer to the elements within the "delitems" array in the context that it is a part of a larger array "ecom.customship".

In pseudocode:
     look at the first element's key (product ID) in the delitems array;
     is it the t-shirt?
        if not, go to the next array element and repeat;
        if it IS the t-shirt, read the quantity element;
     use that value in the calculation;

I am not very well versed in PHP so I need a little help in turning this pseudocode into proper syntax.

Last edited by dskowron (12-21-2017 16:29:14)

Offline

 

#2 12-22-2017 09:02:06

webmaster
Administrator
From: York, PA
Registered: 04-20-2001
Posts: 19512
Website

Re: Interesting Shipping Scenario - Array Help Needed

I'm not sure where you are doing this, but I'm assuming it's in a custom shipping script.  The code would look something like this assuming you have a list of t-shirt ids.

Code:

$info      = $this->globals('ecom.customship');
$tshirtids = array('ID1','ID2','ID3');
$tshirtqty = 0;

foreach ($info['delitems'] as $id => $qty) {if (in_array($id,$tshirtids)) {$tshirtqty += $qty}}

if (!(empty($tshirtqty))) {print 'There are ' . $tshirtqty . ' tshirts in this order.';}
else                      {print 'There are NO tshirts in this order.';}

Nick Hendler

Offline

 

#3 12-22-2017 09:09:46

dskowron
Member
Registered: 11-26-2008
Posts: 127

Re: Interesting Shipping Scenario - Array Help Needed

Thank you Nick. Yes, this is in a custom shipping script.

Offline

 

#4 12-22-2017 15:06:27

dskowron
Member
Registered: 11-26-2008
Posts: 127

Re: Interesting Shipping Scenario - Array Help Needed

With help from Nick, I have the perfect shipping script for my purposes. We have a very unique situation and needed custom shipping methods. Here it is - simple and fast.

<?php

/*
Custom shipping script which sets different shipping methods and amounts depending on the country and whether or not
the customer has a t-shirt (up to three) in their cart. If so, the t-shirt shipping amount will cover everything else
that could be in the cart along with it. Except in the US, two shirts can ship either priority or first-class. Once three shirts are
ordered, that order has to ship via Priority.

In the US, any shirt quantity trips the shipping amount over into the Priority price.

*/

$info = $this->globals('ecom.customship');

$method = 'First-Class Mail';
$method2 = 'Priority Mail Domestic';
$method3 = 'Priority Mail International';

//==================Thanks to Nick for the code in the following section.==============\\


$tshirtids = array('TSHIRT');
$tshirtqty = 0;

foreach ($info['delitems'] as $id => $qty) {
   
    if (in_array($id,$tshirtids)) $tshirtqty += $qty;
   
    }

//==============================================================================================\\

//world shipping

if (($info['country'] != 'United States') and ($info['country'] != 'Canada') and ($info['country'] != 'Mexico')) {

    $custom = array($method3 =>'35.00', $method => '14.00');

    if ($tshirtqty>0 && $tshirtqty<3) { $custom = array($method3 =>'35.00', $method => '23.00');
   
    }elseif ($tshirtqty==3){ $custom = array($method3 =>'35.00');
   

}


//==============================================================================================\\

//US shipping

}     elseif($info['country'] =='United States') {

        $custom = array($method2 =>'6.50', $method => '4.00');

        if ($tshirtqty>0){$custom = array($method2 =>'7.50');

}   

//==============================================================================================\\

//Canadian shipping

}    elseif($info['country'] =='Canada') {

        $custom = array($method3 =>'25.00', $method => '10.00');

    if ($tshirtqty>0 && $tshirtqty<3){
        $custom = array($method3 =>'25.00', $method => '16.00');

    }elseif ($tshirtqty==3){
        $custom = array($method3 =>'25.00');

}

//==============================================================================================\\

//Mexican shipping


}    elseif($info['country'] =='Mexico') {

        $custom = array($method3 =>'31.00', $method => '12.00');

        if ($tshirtqty>0 && $tshirtqty<3){
            $custom = array($method3 =>'31.00', $method => '22.00');

        }elseif ($tshirtqty==3){ $custom = array($method3 =>'31.00');
 

}

}

//==================== Put the Shipping Methods and Costs on the checkout form=====================\\

$this->globals('ecom.customship_response',$custom);



?>

Last edited by dskowron (12-22-2017 17:51:31)

Offline

 

Board footer