Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andreibabor/2ebfb18274cc738271e9e27ef0c73900 to your computer and use it in GitHub Desktop.
Save andreibabor/2ebfb18274cc738271e9e27ef0c73900 to your computer and use it in GitHub Desktop.
Shopify: BespokeApp Shipping Script - different shipping rates for wholesale accounts
/* This macro will be parsed as PHP code (see http://www.php.net)
The calculateshipping function is called every time a shipping calculation request is made by Shopify.
The function must return an array of available shipping options, otherwise no shipping options will be returned to your customers.
*/
function calculateshipping($DATA) {
/* do not edit above this line */
/*
NB: only select php functions are allowed. Use of anything else will return a syntax error when you try to save. Consult the user guide at http://www.parcelintelligence.com.au/cs/documentation/user_guide.pdf for more information
*/
//this holds the rates that will be returned to your customer
$_RATES = array();
$DATA = enrichCartDetails($DATA);
$wholesaleTags = array('Wholesale'); //list of customer tags considered wholesale, case sensitive
$isWholesale = false;
$cartTotal = 0;
foreach ($DATA['items'] as $item) {
$cartTotal += $item['quantity']*$item['price'];
}
if (isset($DATA['cart']['customer']['tags'])) {
foreach ($wholesaleTags as $tag) {
if (in_array($tag,$DATA['cart']['customer']['tags'])) {
$isWholesale = true;
}
}
}
if ($isWholesale) {
if ($cartTotal >= 50000 && $cartTotal <= 99900 ) {
$_RATES[] = array(
"service_name" => 'Wholesale Shipping',
"service_code" => 'Wholesale Shipping',
"total_price" => 2000,
"currency" => "USD",
);
}
if ($cartTotal >= 100000 && $cartTotal <= 499900 ) {
$_RATES[] = array(
"service_name" => 'Wholesale Shipping',
"service_code" => 'Wholesale Shipping',
"total_price" => 4000,
"currency" => "USD",
);
}
if ($cartTotal >= 500000 && $cartTotal <= 999900 ) {
$_RATES[] = array(
"service_name" => 'Wholesale Shipping',
"service_code" => 'Wholesale Shipping',
"total_price" => 6000,
"currency" => "USD",
);
}
if ($cartTotal >= 1000000){
$_RATES[] = array(
"service_name" => 'Wholesale free Shipping',
"service_code" => 'Wholesale Free Shipping',
"total_price" => 0,
"currency" => "USD",
);
}
}
if(!$isWholesale){
$totalWeightInKG = 0; //in KG
$totalWeightInLB = 0; //in LB
foreach ($DATA['items'] as $item) {
$totalWeightInKG += $item['quantity']*$item['grams']/1000;
$totalWeightInLB += $item['quantity']*round($item['grams']/1000*2.20462,2);
}
if ($totalWeightInLB <= 2.0 && $cartTotal <= 8000) {
$_RATES[] = array(
"service_name" => 'Standard Shipping',
"service_code" => 'Standard Shipping',
"total_price" => 1000,
"currency" => "USD",
);
}
if ($totalWeightInLB >= 2.1 && $cartTotal <= 8000 ) {
$_RATES[] = array(
"service_name" => 'Heavy Goods Shipping',
"service_code" => 'Heavy Goods Shipping',
"total_price" => 2000,
"currency" => "USD",
);
}
if ($cartTotal >= 8000){
$_RATES[] = array(
"service_name" => 'Free Standard Shipping',
"service_code" => 'Free Standard Shipping',
"total_price" => 0,
"currency" => "USD",
);
}
}
/*
this is what $DATA looks like, you can use any of the info in $DATA to generate your shipping rate
Array
(
[origin] => Array
(
[country] => AU
[postal_code] => 3000
[province] => VIC
[city] => melbourne
[name] =>
[address1] => 1 main street
[address2] =>
[address3] =>
[phone] =>
[fax] =>
[address_type] =>
[company_name] =>
)
[destination] => Array
(
[country] => AU
[postal_code] => 2000
[province] => NSW
[city] =>
[name] =>
[address1] =>
[address2] =>
[address3] =>
[phone] =>
[fax] =>
[address_type] =>
[company_name] =>
)
[items] => Array
(
[0] => Array
(
[name] => product10
[sku] => SKUP00310
[quantity] => 1
[grams] => 1000
[price] => 300
[vendor] => PRODUCT
[requires_shipping] => 1
[taxable] => 1
[fulfillment_service] => manual
[product_id] => 128436738
[variant_id] => 290813760
)
[1] => Array
(
[name] => product11
[sku] => SKUP0011
[quantity] => 1
[grams] => 1100
[price] => 300
[vendor] => PRODUCT
[requires_shipping] => 1
[taxable] => 1
[fulfillment_service] => manual
[product_id] => 128436744
[variant_id] => 290813772
)
)
[currency] => AUD
)
//this is how you insert a rate
$_RATES[] = array(
"service_name" => "Standard Shipping", //this is what the customer will see
"service_code" => "STANDARD_SHIPPING", //can be anything you like
"total_price" => 10000, //in cents
"currency" => "AUD",
);
*/
return $_RATES;
/* do not edit below this line */
}
@andreibabor
Copy link
Author

andreibabor commented May 10, 2019

shipping options
$20 $500 - $999
$40 $1,000 - $4,999
$60. $5,000 - $9,999
FREE $10,000+

Task:
For our retail customers we currently offer free shipping on orders over $80. We need to have different shipping rates for wholesale accounts. Ideally they are tiered (ie. $10 for orders under $500, $20 for orders between $500-1,000, etc.). Let me know if we need to hop on a call to discuss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment