Exemplo n.º 1
0
<?php

$rule = new Cart66ShippingRule();
$method = new Cart66ShippingMethod();
$rate = new Cart66ShippingRate();
$product = new Cart66Product();
$tab = 1;
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    if ($_POST['cart66-action'] == 'save rule') {
        $rule->setData($_POST['rule']);
        $rule->save();
        $rule->clear();
    } elseif ($_POST['cart66-action'] == 'save shipping validation') {
        Cart66Setting::setValue('require_shipping_validation', $_POST['require_shipping_validation']);
        Cart66Setting::setValue('require_shipping_validation_label', $_POST['require_shipping_validation_label']);
    } elseif ($_POST['cart66-action'] == 'save shipping method') {
        if (isset($_POST['shipping_method']['countries'])) {
            foreach ($_POST['shipping_method']['countries'] as $post_country) {
                $post_country = explode('~', $post_country);
                $post_countries[$post_country[0]] = $post_country[1];
            }
            $_POST['shipping_method']['countries'] = serialize($post_countries);
        }
        $method->setData($_POST['shipping_method']);
        $method->save();
        $method->clear();
    } elseif ($_POST['cart66-action'] == 'save product rate') {
        $rate->setData($_POST['rate']);
        $rate->save();
        $rate->clear();
    } elseif ($_POST['cart66-action'] == 'save local pickup info') {
 public function getShippingCost($methodId = null)
 {
     $setting = new Cart66Setting();
     $shipping = null;
     if (!$this->requireShipping()) {
         $shipping = 0;
     } elseif (Cart66Session::get('Cart66LiveRates') && get_class(Cart66Session::get('Cart66LiveRates')) == 'Cart66LiveRates' && Cart66Setting::getValue('use_live_rates')) {
         $liveRate = Cart66Session::get('Cart66LiveRates')->getSelected();
         if (is_numeric($liveRate->rate)) {
             $r = $liveRate->rate;
             return number_format($r, 2, '.', '');
         }
     } else {
         if ($methodId > 0) {
             $this->_shippingMethodId = $methodId;
         }
         if ($this->_shippingMethodId < 1) {
             $this->_setDefaultShippingMethodId();
         } else {
             // make sure shipping method exists otherwise reset to the default shipping method
             $method = new Cart66ShippingMethod();
             if (!$method->load($this->_shippingMethodId) || !empty($method->code)) {
                 Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Resetting the default shipping method id");
                 $this->_setDefaultShippingMethodId();
             }
         }
         $methodId = $this->_shippingMethodId;
         // Check for shipping rules first
         $shipping = 0;
         $isRuleSet = false;
         $rule = new Cart66ShippingRule();
         if (isset($methodId) && is_numeric($methodId)) {
             $rules = $rule->getModels("where shipping_method_id = {$methodId}", 'order by min_amount desc');
             if (count($rules)) {
                 $cartTotal = $this->getSubTotal();
                 foreach ($rules as $rule) {
                     if ($cartTotal > $rule->minAmount) {
                         $shipping = $rule->shippingCost;
                         $isRuleSet = true;
                         break;
                     }
                 }
             }
         }
         if (!$isRuleSet) {
             $product = new Cart66Product();
             $shipping = 0;
             $highestShipping = 0;
             $bundleShipping = 0;
             $highestId = 0;
             foreach ($this->_items as $item) {
                 $product->load($item->getProductId());
                 if ($highestId < 1) {
                     $highestId = $product->id;
                 }
                 if ($product->isShipped()) {
                     $shippingPrice = $product->getShippingPrice($methodId);
                     $bundleShippingPrice = $product->getBundleShippingPrice($methodId);
                     if ($shippingPrice > $highestShipping) {
                         $highestShipping = $shippingPrice;
                         $highestId = $product->id;
                     }
                     $bundleShipping += $bundleShippingPrice * $item->getQuantity();
                 }
             }
             if ($highestId > 0) {
                 $product->load($highestId);
                 $shippingPrice = $product->getShippingPrice($methodId);
                 $bundleShippingPrice = $product->getBundleShippingPrice($methodId);
                 $shipping = $shippingPrice + ($bundleShipping - $bundleShippingPrice);
             }
         }
     }
     $shipping = $shipping < 0 ? 0 : $shipping;
     return number_format($shipping, 2, '.', '');
 }