Back to PhocacartShipping class

Method getPossibleShippingMethods

public
getPossibleShippingMethods
(mixed $amountNetto, mixed $amountBrutto, mixed $quantity, mixed $country, mixed $region, mixed $zip, mixed $weight, mixed $length, mixed $width, mixed $height, mixed $id = 0, mixed $selected = 0)

Method getPossibleShippingMethods - Source code

/*
 * Be aware:
 * if id > 0 ... it test the selected shipping method and return it if OK
 * if id = 0 ... it tests all shipping methods they meet the criteria and return all to list them (e.g. in checkout)
 * Always test for the id before using this function
 */
public function getPossibleShippingMethods($amountNetto, $amountBrutto, $quantity, $country, $region, $zip, $weight, $length, $width, $height, $id = 0, $selected = 0)
{
    $app = Factory::getApplication();
    $paramsC = PhocacartUtils::getComponentParameters();
    $shipping_amount_rule = $paramsC->get('shipping_amount_rule', 0);
    $user = PhocacartUser::getUser();
    $userLevels = implode(',', $user->getAuthorisedViewLevels());
    $userGroups = implode(',', PhocacartGroup::getGroupsById($user->id, 1, 1));
    $db = Factory::getDBO();
    $wheres = array();
    // ACCESS
    $wheres[] = " s.published = 1";
    $wheres[] = " s.access IN (" . $userLevels . ")";
    $wheres[] = " (ga.group_id IN (" . $userGroups . ") OR ga.group_id IS NULL)";
    if (!empty($this->type) && is_array($this->type)) {
        // if type is empty, then all types are asked
        $wheres[] = " s.type IN (" . implode(',', $this->type) . ')';
    }
    if ((int) $id > 0) {
        $wheres[] = 's.id = ' . (int) $id;
        $limit = ' LIMIT 1';
        //$group = '';
    } else {
        $limit = '';
    }
    $columns = 's.id, s.tax_id, s.cost, s.cost_additional, s.calculation_type, s.title, s.description, s.image, s.access, s.method, s.zip,' . ' s.active_amount, s.active_quantity, s.active_zone, s.active_country, s.active_region, s.active_zip,' . ' s.active_weight, s.active_size,' . ' s.lowest_amount, s.highest_amount, s.minimal_quantity, s.maximal_quantity, s.lowest_weight,' . ' s.highest_weight, s.default,' . ' s.minimal_length, s.minimal_width, s.minimal_height, s.maximal_length, s.maximal_width, s.maximal_height, s.params,' . ' t.id as taxid, t.title as taxtitle, t.tax_rate as taxrate, t.calculation_type as taxcalculationtype,' . ' GROUP_CONCAT(DISTINCT r.region_id) AS region,' . ' GROUP_CONCAT(DISTINCT c.country_id) AS country,' . ' GROUP_CONCAT(DISTINCT z.zone_id) AS zone';
    $groupsFull = 's.id, s.tax_id, s.cost, s.cost_additional, s.calculation_type, s.title, s.description, s.image, s.access, s.method, s.zip,' . ' s.active_amount, s.active_quantity, s.active_zone, s.active_country, s.active_region, s.active_zip,' . ' s.active_weight, s.active_size,' . ' s.lowest_amount, s.highest_amount, s.minimal_quantity, s.maximal_quantity, s.lowest_weight,' . ' s.minimal_length, s.minimal_width, s.minimal_height, s.maximal_length, s.maximal_width, s.maximal_height, s.params,' . ' s.highest_weight, s.default,' . ' t.id, t.title, t.tax_rate, t.calculation_type';
    $groupsFast = 's.id';
    $groups = PhocacartUtilsSettings::isFullGroupBy() ? $groupsFull : $groupsFast;
    $where = count($wheres) ? ' WHERE ' . implode(' AND ', $wheres) : '';
    $query = ' SELECT ' . $columns . ' FROM #__phocacart_shipping_methods AS s' . ' LEFT JOIN #__phocacart_shipping_method_regions AS r ON r.shipping_id = s.id' . ' LEFT JOIN #__phocacart_shipping_method_countries AS c ON c.shipping_id = s.id' . ' LEFT JOIN #__phocacart_shipping_method_zones AS z ON z.shipping_id = s.id' . ' LEFT JOIN #__phocacart_taxes AS t ON t.id = s.tax_id' . ' LEFT JOIN #__phocacart_item_groups AS ga ON s.id = ga.item_id AND ga.type = 7' . $where . ' GROUP BY ' . $groups . ' ORDER BY s.ordering' . $limit;
    PhocacartUtils::setConcatCharCount();
    $db->setQuery($query);
    $shippings = $db->loadObjectList();
    /*if (empty($shippings)) {
    			return false;
    		}*/
    if (!empty($shippings) && !isset($shippings[0]->id) || isset($shippings[0]->id) && (int) $shippings[0]->id < 1) {
        return false;
    }
    $i = 0;
    if (!empty($shippings)) {
        foreach ($shippings as $k => $v) {
            $v->active = 0;
            $v->selected = 0;
            $a = 0;
            $q = 0;
            $z = 0;
            $c = 0;
            $r = 0;
            $zi = 0;
            $w = 0;
            $s = 0;
            // Amount Rule
            if ($v->active_amount == 1) {
                if ($shipping_amount_rule == 0 || $shipping_amount_rule == 2) {
                    // No tax, brutto
                    if ($amountBrutto >= $v->lowest_amount && $amountBrutto <= $v->highest_amount) {
                        $a = 1;
                    }
                } else {
                    if ($shipping_amount_rule == 1) {
                        // Netto
                        if ($amountNetto >= $v->lowest_amount && $amountNetto <= $v->highest_amount) {
                            $a = 1;
                        }
                    }
                }
            } else {
                $a = 1;
            }
            // Quantity Rule
            if ($v->active_quantity == 1) {
                if ($quantity >= $v->minimal_quantity && $quantity <= $v->maximal_quantity) {
                    $q = 1;
                }
            } else {
                $q = 1;
            }
            // Zone Rule
            if ($v->active_zone == 1) {
                if (isset($v->zone) && $v->zone != '') {
                    $zones = explode(',', $v->zone);
                    if (PhocacartZone::isCountryOrRegionIncluded($zones, (int) $country, (int) $region)) {
                        $z = 1;
                    }
                }
            } else {
                $z = 1;
            }
            // Country Rule
            if ($v->active_country == 1) {
                if (isset($v->country) && $v->country != '') {
                    $countries = explode(',', $v->country);
                    if (in_array((int) $country, $countries)) {
                        $c = 1;
                    }
                }
            } else {
                $c = 1;
            }
            // Region Rule
            if ($v->active_region == 1) {
                if (isset($v->region) && $v->region != '') {
                    $regions = explode(',', $v->region);
                    if (in_array((int) $region, $regions)) {
                        $r = 1;
                    }
                }
            } else {
                $r = 1;
            }
            // ZIP Rule
            if ($v->active_zip == 1) {
                if (isset($v->zip) && $v->zip != '') {
                    $zips = array_map('trim', explode(',', $v->zip));
                    if (in_array((int) $zip, $zips)) {
                        $zi = 1;
                    }
                }
            } else {
                $zi = 1;
            }
            // Weight Rule
            if ($v->active_weight == 1) {
                if (($weight >= $v->lowest_weight || $weight == $v->lowest_weight) && ($weight <= $v->highest_weight || $weight == $v->highest_weight)) {
                    $w = 1;
                }
            } else {
                $w = 1;
            }
            // Size Rule
            if ($v->active_size == 1) {
                $sP = 0;
                if (($length >= $v->minimal_length || $length == $v->minimal_length) && ($length <= $v->maximal_length || $length == $v->maximal_length)) {
                    $sP++;
                }
                if (($width >= $v->minimal_width || $width == $v->minimal_width) && ($width <= $v->maximal_width || $width == $v->maximal_width)) {
                    $sP++;
                }
                if (($height >= $v->minimal_height || $height == $v->minimal_height) && ($height <= $v->maximal_height || $height == $v->maximal_height)) {
                    $sP++;
                }
                if ($sP == 3) {
                    $s = 1;
                }
            } else {
                $s = 1;
            }
            // No rule was set for shipping, it will be displayed at all events
            if ($v->active_amount == 0 && $v->active_quantity == 0 && $v->active_country == 0 && $v->active_region == 0 && $v->active_zip == 0 && $v->active_weight == 0) {
                $v->active = 1;
            }
            // if some of the rules is not valid, all the payment is NOT valid
            if ($a == 0 || $q == 0 || $z == 0 || $c == 0 || $r == 0 || $zi == 0 || $w == 0 || $s == 0) {
                $v->active = 0;
            } else {
                $v->active = 1;
            }
            if ($v->active == 0) {
                if (isset($shippings[$i])) {
                    unset($shippings[$i]);
                }
            } else {
                // Shipping is active but shipping method plugin can deactivate it
                $pluginShipping = PluginHelper::importPlugin('pcs');
                if ($pluginShipping) {
                    PluginHelper::importPlugin('pcs', htmlspecialchars(strip_tags($v->method)));
                    $eventData = array();
                    $active = true;
                    $eventData['pluginname'] = htmlspecialchars(strip_tags($v->method));
                    Factory::getApplication()->triggerEvent('onPCSbeforeShowPossibleShippingMethod', array(&$active, $v, $eventData));
                    if ($active == false) {
                        if (isset($shippings[$i])) {
                            unset($shippings[$i]);
                        }
                    }
                }
            }
            // Try to set default for frontend form
            // If user selected some shipping, such will be set as default
            // If not then the default will be set
            if ((int) $selected > 0) {
                if ((int) $v->id == (int) $selected) {
                    $v->selected = 1;
                }
            } else {
                $v->selected = $v->default;
            }
            $i++;
        }
    }
    return $shippings;
}