예제 #1
0
 public function initialize()
 {
     parent::initialize();
     $app = JFactory::getApplication();
     $context = JeproshopContext::getContext();
     // Get address ID
     $address_id = 0;
     $type = $app->input->get('type');
     if ($this->use_ajax && isset($type)) {
         if ($type == 'delivery' && isset($context->cart->address_delivery_id)) {
             $address_id = (int) $context->cart->address_delivery_id;
         } else {
             if ($type == 'invoice' && isset($context->cart->address_invoice_id) && $context->cart->id_address_invoice != $context->cart->address_delivery_id) {
                 $address_id = (int) $context->cart->address_invoice_id;
             }
         }
     } else {
         $address_id = (int) $app->input->get('address_id', 0);
     }
     // Initialize address
     if ($address_id) {
         $address = new JeproshopAddressModelAddress($address_id);
         $view = $app->input->get('view');
         $viewClass = $this->getView($view, JFactory::getDocument()->getType());
         if (JeproshopTools::isLoadedObject($address, 'address_id') && JeproshopCustomerModelCustomer::customerHasAddress($context->customer->customer_id, $address_id)) {
             $task = $app->input->get('task');
             if (isset($task) && $task == 'delete') {
                 if ($address->delete()) {
                     if ($context->cart->address_invoice_id == $address->address_id) {
                         unset($context->cart->address_invoice_id);
                     }
                     if ($context->cart->id_address_delivery == $address->address_id) {
                         unset($context->cart->id_address_delivery);
                         $context->cart->updateAddressId($address->address_id, (int) JeproshopAddressModelAddress::getCustomerFirstAddressId($context->customer->customer_id));
                     }
                     $app->redirect('index.php?option=com_jeproshop&view=address');
                 }
                 $this->has_errors = true;
                 JError::raiseError(500, 'This address cannot be deleted.');
             }
         } elseif ($this->use_ajax) {
             $app->close;
         } else {
             $app->redirect('index.php?option=com_jeproshop&view=address');
         }
     }
 }
예제 #2
0
파일: cart.php 프로젝트: jeprodev/jeproshop
 /**
  * Return package shipping cost
  *
  * @param integer $carrier_id Carrier ID (default : current carrier)
  * @param boolean $use_tax
  * @param JeproshopCountryModelCountry $default_country
  * @param Array $product_list
  * @param array $product_list List of product concerned by the shipping. If null, all the product of the cart are used to calculate the shipping cost
  *
  * @return float Shipping total
  */
 public function getPackageShippingCost($carrier_id = null, $use_tax = true, JeproshopCountryModelCountry $default_country = null, $product_list = null, $zone_id = null)
 {
     if ($this->isVirtualCart()) {
         return 0;
     }
     if (!$default_country) {
         $default_country = JeproshopContext::getContext()->country;
     }
     $complete_product_list = $this->getProducts();
     if (is_null($product_list)) {
         $products = $complete_product_list;
     } else {
         $products = $product_list;
     }
     if (JeproshopSettingModelSetting::getValue('tax_address_type') == 'address_invoice_id') {
         $address_id = (int) $this->address_invoice_id;
     } elseif (count($product_list)) {
         $prod = current($product_list);
         $address_id = (int) $prod->address_delivery_id;
     } else {
         $address_id = null;
     }
     if (!JeproshopAddressModelAddress::addressExists($address_id)) {
         $address_id = null;
     }
     $cache_id = 'get_package_shipping_cost_' . (int) $this->cart_id . '_' . (int) $address_id . '_' . (int) $carrier_id . '_' . (int) $use_tax . '_' . (int) $default_country->country_id;
     if ($products) {
         foreach ($products as $product) {
             $cache_id .= '_' . (int) $product->product_id . '_' . (int) $product->product_attribute_id;
         }
     }
     if (JeproshopCache::isStored($cache_id)) {
         return JeproshopCache::retrieve($cache_id);
     }
     // Order total in default currency without fees
     $order_total = $this->getOrderTotal(true, JeproshopCartModelCart::ONLY_PHYSICAL_PRODUCTS_WITHOUT_SHIPPING, $product_list);
     // Start with shipping cost at 0
     $shipping_cost = 0;
     // If no product added, return 0
     if (!count($products)) {
         JeproshopCache::store($cache_id, $shipping_cost);
         return $shipping_cost;
     }
     if (!isset($zone_id)) {
         // Get id zone
         if (!$this->isMultiAddressDelivery() && isset($this->address_delivery_id) && $this->address_delivery_id && JeproshopCustomerModelCustomer::customerHasAddress($this->customer_id, $this->address_delivery_id)) {
             $zone_id = JeproshopAddressModelAddress::getZoneIdByAddressId((int) $this->address_delivery_id);
         } else {
             if (!JeproshopValidator::isLoadedObject($default_country, 'country_id')) {
                 $default_country = new Country(JeproshopSettingModelSeting::getValue('PS_COUNTRY_DEFAULT'), JeproshopSettingModelSeting::getValue('PS_LANG_DEFAULT'));
             }
             $zone_id = (int) $default_country->zone_id;
         }
     }
     if ($carrier_id && !$this->isCarrierInRange((int) $carrier_id, (int) $zone_id)) {
         $carrier_id = '';
     }
     if (empty($carrier_id) && $this->isCarrierInRange((int) JeproshopSettingModelSeting::getValue('default_carrier'), (int) $zone_id)) {
         $carrier_id = (int) JeproshopSettingModelSeting::getValue('default_carrier');
     }
     $total_package_without_shipping_tax_inc = $this->getOrderTotal(true, Cart::BOTH_WITHOUT_SHIPPING, $product_list);
     if (empty($carrier_id)) {
         if ((int) $this->customer_id) {
             $customer = new JeproshopCustomerModelCustomer((int) $this->customer_id);
             $result = JeproshopCarrierModelCarrier::getCarriers((int) JeproshopSettingModelSeting::getValue('default_lang'), true, false, (int) $zone_id, $customer->getGroups());
             unset($customer);
         } else {
             $result = JeproshopCarrierModelCarrier::getCarriers((int) JeproshopSettingModelSeting::getValue('default_lang'), true, false, (int) $zone_id);
         }
         foreach ($result as $k => $row) {
             if ($row->carrier_id == JeproshopSettingModelSeting::getValue('default_carrier')) {
                 continue;
             }
             if (!isset(self::$_carriers[$row->carrier_id])) {
                 self::$_carriers[$row->carrier_id] = new JeproshopCarrierModelCarrier((int) $row->carrier_id);
             }
             $carrier = self::$_carriers[$row->carrier_id];
             // Get only carriers that are compliant with shipping method
             if ($carrier->getShippingMethod() == JeproshopCarrierModelCarrier::WEIGHT_SHIPPING_METHOD && $carrier->getMaxDeliveryPriceByWeight((int) $zone_id) === false || $carrier->getShippingMethod() == JeproshopCarrierModelCarrier::PRICE_SHIPPING_METHOD && $carrier->getMaxDeliveryPriceByPrice((int) $zone_id) === false) {
                 unset($result[$k]);
                 continue;
             }
             // If out-of-range behavior carrier is set on "Deactivated carrier"
             if ($row->range_behavior) {
                 $check_delivery_price_by_weight = JeproshopCarrierModelCarrier::checkDeliveryPriceByWeight($row->carrier_id, $this->getTotalWeight(), (int) $zone_id);
                 $total_order = $total_package_without_shipping_tax_inc;
                 $check_delivery_price_by_price = JeproshopCarrierModelCarrier::checkDeliveryPriceByPrice($row->carrier_id, $total_order, (int) $zone_id, (int) $this->currency_id);
                 // Get only carriers that have a range compatible with cart
                 if ($carrier->getShippingMethod() == JeproshopCarrierModelCarrier::WEIGHT_SHIPPING_METHOD && !$check_delivery_price_by_weight || $carrier->getShippingMethod() == JeproshopCarrierModelCarrier::PRICE_SHIPPING_METHOD && !$check_delivery_price_by_price) {
                     unset($result[$k]);
                     continue;
                 }
             }
             if ($carrier->getShippingMethod() == JeproshopCarrierModelCarrier::WEIGHT_SHIPPING_METHOD) {
                 $shipping = $carrier->getDeliveryPriceByWeight($this->getTotalWeight($product_list), (int) $zone_id);
             } else {
                 $shipping = $carrier->getDeliveryPriceByPrice($order_total, (int) $zone_id, (int) $this->currency_id);
             }
             if (!isset($min_shipping_price)) {
                 $min_shipping_price = $shipping;
             }
             if ($shipping <= $min_shipping_price) {
                 $carrier_id = (int) $row->carrier_id;
                 $min_shipping_price = $shipping;
             }
         }
     }
     if (empty($carrier_id)) {
         $carrier_id = JeproshopSettingModelSeting::getValue('default_carrier');
     }
     if (!isset(self::$_carriers[$carrier_id])) {
         self::$_carriers[$carrier_id] = new JeproshopCarrierModelCarrier((int) $carrier_id, JeproshopSettingModelSeting::getValue('default_lang'));
     }
     $carrier = self::$_carriers[$carrier_id];
     // No valid Carrier or $carrier_id <= 0 ?
     if (!JeproshopValidator::isLoadedObject($carrier, 'carrier_id')) {
         JeproshopCache::store($cache_id, 0);
         return 0;
     }
     if (!$carrier->published) {
         JeproshopCache::store($cache_id, $shipping_cost);
         return $shipping_cost;
     }
     // Free fees if free carrier
     if ($carrier->is_free == 1) {
         JeproshopCache::store($cache_id, 0);
         return 0;
     }
     // Select carrier tax
     if ($use_tax && !JeproshopTaxModelTax::excludedTaxOption()) {
         $address = JeproshopAddressModelAddress::initialize((int) $address_id);
         $carrier_tax = $carrier->getTaxesRate($address);
     }
     $configuration = JeproshopSettingModelSeting::getValueMultiple(array('PS_SHIPPING_FREE_PRICE', 'PS_SHIPPING_HANDLING', 'PS_SHIPPING_METHOD', 'PS_SHIPPING_FREE_WEIGHT'));
     // Free fees
     $free_fees_price = 0;
     if (isset($configuration['PS_SHIPPING_FREE_PRICE'])) {
         $free_fees_price = JeproshopValidator::convertPrice((double) $configuration['PS_SHIPPING_FREE_PRICE'], JeproshopCurrencyModelCurrency::getCurrencyInstance((int) $this->currency_id));
     }
     $orderTotalWithDiscounts = $this->getOrderTotal(true, JeproshopCartModelCart::BOTH_WITHOUT_SHIPPING, null, null, false);
     if ($orderTotalWithDiscounts >= (double) $free_fees_price && (double) $free_fees_price > 0) {
         Cache::store($cache_id, $shipping_cost);
         return $shipping_cost;
     }
     if (isset($configuration['PS_SHIPPING_FREE_WEIGHT']) && $this->getTotalWeight() >= (double) $configuration['PS_SHIPPING_FREE_WEIGHT'] && (double) $configuration['PS_SHIPPING_FREE_WEIGHT'] > 0) {
         JeproshopCache::store($cache_id, $shipping_cost);
         return $shipping_cost;
     }
     // Get shipping cost using correct method
     if ($carrier->range_behavior) {
         if (!isset($zone_id)) {
             // Get id zone
             if (isset($this->address_delivery_id) && $this->address_delivery_id && JeproshopCustomerModelCustomer::customerHasAddress($this->customer_id, $this->address_delivery_id)) {
                 $zone_id = JeproshopAddressModelAddress::getZoneIdByAddressId((int) $this->address_delivery_id);
             } else {
                 $zone_id = (int) $default_country->zone_id;
             }
         }
         if ($carrier->getShippingMethod() == JeproshopCarrierModelCarrier::WEIGHT_SHIPPING_METHOD && !JeproshopCarrierModelCarrier::checkDeliveryPriceByWeight($carrier->id, $this->getTotalWeight(), (int) $zone_id) || $carrier->getShippingMethod() == JeproshopCarrierModelCarrier::PRICE_SHIPPING_METHOD && !JeproshopCarrierModelCarrier::checkDeliveryPriceByPrice($carrier->id, $total_package_without_shipping_tax_inc, $zone_id, (int) $this->currency_id)) {
             $shipping_cost += 0;
         } else {
             if ($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT) {
                 $shipping_cost += $carrier->getDeliveryPriceByWeight($this->getTotalWeight($product_list), $zone_id);
             } else {
                 // by price
                 $shipping_cost += $carrier->getDeliveryPriceByPrice($order_total, $zone_id, (int) $this->currency_id);
             }
         }
     } else {
         if ($carrier->getShippingMethod() == JeproshopCarrierModelCarrier::WEIGHT_SHIPPING_METHOD) {
             $shipping_cost += $carrier->getDeliveryPriceByWeight($this->getTotalWeight($product_list), $zone_id);
         } else {
             $shipping_cost += $carrier->getDeliveryPriceByPrice($order_total, $zone_id, (int) $this->currency_id);
         }
     }
     // Adding handling charges
     if (isset($configuration['PS_SHIPPING_HANDLING']) && $carrier->shipping_handling) {
         $shipping_cost += (double) $configuration['PS_SHIPPING_HANDLING'];
     }
     // Additional Shipping Cost per product
     foreach ($products as $product) {
         if (!$product->is_virtual) {
             $shipping_cost += $product->additional_shipping_cost * $product->cart_quantity;
         }
     }
     $shipping_cost = JeproshopValidator::convertPrice($shipping_cost, JeproshopCurrencyModelCurrency::getCurrencyInstance((int) $this->currency_id));
     /*/get external shipping cost from module
             if ($carrier->shipping_external)
             {
                 $module_name = $carrier->external_module_name;
                 $module = Module::getInstanceByName($module_name);
     
                 if (JeproshopValidator::isLoadedObject($module))
                 {
                     if (array_key_exists('carrier_id', $module))
                         $module->carrier_id = $carrier->id;
                     if ($carrier->need_range)
                         if (method_exists($module, 'getPackageShippingCost'))
                             $shipping_cost = $module->getPackageShippingCost($this, $shipping_cost, $products);
                         else
                             $shipping_cost = $module->getOrderShippingCost($this, $shipping_cost);
                     else
                         $shipping_cost = $module->getOrderShippingCostExternal($this);
     
                     // Check if carrier is available
                     if ($shipping_cost === false)
                     {
                         JeproshopCache::store($cache_id, false);
                         return false;
                     }
                 }
                 else
                 {
                     JeproshopCache::store($cache_id, false);
                     return false;
                 }
             } */
     // Apply tax
     if ($use_tax && isset($carrier_tax)) {
         $shipping_cost *= 1 + $carrier_tax / 100;
     }
     $shipping_cost = (double) JeproshopValidator::roundPrice((double) $shipping_cost, 2);
     JeproshopCache::store($cache_id, $shipping_cost);
     return $shipping_cost;
 }