Ejemplo n.º 1
0
 public function pwa_order($data)
 {
     $prefix = _DB_PREFIX_;
     $pwa_order_status = $data['amznPmtsPaymentStatus'];
     $pwa_order_id = $data['amznPmtsOrderIds'];
     if (isset($data['refresh']) && $data['refresh'] != '') {
         $refresh = $data['refresh'];
     } else {
         $refresh = 'yes';
     }
     if (isset($data['CartId']) && $data['CartId'] > 0) {
         $CartId = $data['CartId'];
     } else {
         $CartId = 0;
     }
     $tablename = $prefix . 'pwa_orders';
     $sql = 'SELECT * from `' . $tablename . '` where `amazon_order_id` = "' . $pwa_order_id . '" ';
     $result = Db::getInstance()->ExecuteS($sql);
     if (empty($result)) {
         $tablename = $prefix . 'orders';
         $date = date('Y-m-d H:i:s');
         $sql = 'INSERT into `' . $tablename . '`  (`current_state` , `id_cart` ,  `payment` , `module` , `date_add` ) VALUES( 99, "' . $CartId . '", "Pay with Amazon", "pwapresta", "' . $date . '" )';
         Db::getInstance()->Execute($sql);
         $order_id = Db::getInstance()->Insert_ID();
         $tablename = $prefix . 'pwa_orders';
         $sql = 'INSERT into `' . $tablename . '`  (`prestashop_order_id` , `amazon_order_id` ) VALUES( "' . $order_id . '", "' . $pwa_order_id . '" )';
         Db::getInstance()->Execute($sql);
     } else {
         $order_id = $result[0]['prestashop_order_id'];
     }
     $products = $this->context->cart->getProducts();
     foreach ($products as $product) {
         if (Configuration::get('PS_TAX_ADDRESS_TYPE') == 'id_address_invoice') {
             $address_id = (int) $this->id_address_invoice;
         } else {
             $address_id = (int) $product['id_address_delivery'];
         }
         // Get delivery address of the product from the cart
         if (!Address::addressExists($address_id)) {
             $address_id = null;
         }
         $virtual_context = Context::getContext()->cloneContext();
         $virtual_context->cart = $this->context->cart;
         $product_price = Product::getPriceStatic((int) $product['id_product'], false, (int) $product['id_product_attribute'], 2, null, false, true, $product['quantity'], false, (int) $this->context->cart->id_customer ? (int) $this->context->cart->id_customer : null, (int) $this->context->cart->id, (int) $address_id ? (int) $address_id : null, $null, true, true, $virtual_context);
         $tablename = $prefix . 'pwa_order_products';
         $sql = 'INSERT into `' . $tablename . '`  (`id_cart` , `id_product` , `id_product_attribute` , `quantity` , `amount` , `amount_excl` , `sku` , `title` ) VALUES( "' . $this->context->cart->id . '", "' . $product["id_product"] . '",  "' . $product["id_product_attribute"] . '",  "' . $product["quantity"] . '", "' . $product["price_wt"] . '", "' . $product_price . '", "' . $product["reference"] . '", "' . $product["name"] . '" )';
         Db::getInstance()->Execute($sql);
         //$this->context->cart->deleteProduct($product["id_product"]);
     }
     $this->context->smarty->assign(array('order_id' => $order_id, 'pwa_order_id' => $pwa_order_id, 'pwa_order_status' => $pwa_order_status, 'refresh' => $refresh));
     $this->setTemplate('pwa_order.tpl');
 }
Ejemplo n.º 2
0
 public function getPackageShippingCost($id_carrier = null, $use_tax = true, Country $default_country = null, $product_list = null, $id_zone = null)
 {
     if (!Configuration::get('LEGAL_SHIPTAXMETH')) {
         return parent::getPackageShippingCost($id_carrier, $use_tax, $default_country, $product_list, $id_zone);
     }
     if ($this->isVirtualCart()) {
         return 0;
     }
     if (!$default_country) {
         $default_country = Context::getContext()->country;
     }
     if (!is_null($product_list)) {
         foreach ($product_list as $key => $value) {
             if ($value['is_virtual'] == 1) {
                 unset($product_list[$key]);
             }
         }
     }
     $complete_product_list = $this->getProducts();
     if (is_null($product_list)) {
         $products = $complete_product_list;
     } else {
         $products = $product_list;
     }
     if (Configuration::get('PS_TAX_ADDRESS_TYPE') == 'id_address_invoice') {
         $address_id = (int) $this->id_address_invoice;
     } elseif (count($product_list)) {
         $prod = current($product_list);
         $address_id = (int) $prod['id_address_delivery'];
     } else {
         $address_id = null;
     }
     if (!Address::addressExists($address_id)) {
         $address_id = null;
     }
     $cache_id = 'getPackageShippingCost_' . (int) $this->id . '_' . (int) $address_id . '_' . (int) $id_carrier . '_' . (int) $use_tax . '_' . (int) $default_country->id;
     if ($products) {
         foreach ($products as $product) {
             $cache_id .= '_' . (int) $product['id_product'] . '_' . (int) $product['id_product_attribute'];
         }
     }
     if (Cache::isStored($cache_id)) {
         return Cache::retrieve($cache_id);
     }
     // Order total in default currency without fees
     $order_total = $this->getOrderTotal(true, Cart::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)) {
         Cache::store($cache_id, $shipping_cost);
         return $shipping_cost;
     }
     if (!isset($id_zone)) {
         // Get id zone
         if (!$this->isMultiAddressDelivery() && isset($this->id_address_delivery) && $this->id_address_delivery && Customer::customerHasAddress($this->id_customer, $this->id_address_delivery)) {
             $id_zone = Address::getZoneById((int) $this->id_address_delivery);
         } else {
             if (!Validate::isLoadedObject($default_country)) {
                 $default_country = new Country(Configuration::get('PS_COUNTRY_DEFAULT'), Configuration::get('PS_LANG_DEFAULT'));
             }
             $id_zone = (int) $default_country->id_zone;
         }
     }
     if ($id_carrier && !$this->isCarrierInRange((int) $id_carrier, (int) $id_zone)) {
         $id_carrier = '';
     }
     if (empty($id_carrier) && $this->isCarrierInRange((int) Configuration::get('PS_CARRIER_DEFAULT'), (int) $id_zone)) {
         $id_carrier = (int) Configuration::get('PS_CARRIER_DEFAULT');
     }
     $total_package_without_shipping_tax_inc = $this->getOrderTotal(true, Cart::BOTH_WITHOUT_SHIPPING, $product_list);
     if (empty($id_carrier)) {
         if ((int) $this->id_customer) {
             $customer = new Customer((int) $this->id_customer);
             $result = Carrier::getCarriers((int) Configuration::get('PS_LANG_DEFAULT'), true, false, (int) $id_zone, $customer->getGroups());
             unset($customer);
         } else {
             $result = Carrier::getCarriers((int) Configuration::get('PS_LANG_DEFAULT'), true, false, (int) $id_zone);
         }
         foreach ($result as $k => $row) {
             if ($row['id_carrier'] == Configuration::get('PS_CARRIER_DEFAULT')) {
                 continue;
             }
             if (!isset(self::$_carriers[$row['id_carrier']])) {
                 self::$_carriers[$row['id_carrier']] = new Carrier((int) $row['id_carrier']);
             }
             $carrier = self::$_carriers[$row['id_carrier']];
             // Get only carriers that are compliant with shipping method
             if ($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT && $carrier->getMaxDeliveryPriceByWeight((int) $id_zone) === false || $carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_PRICE && $carrier->getMaxDeliveryPriceByPrice((int) $id_zone) === false) {
                 unset($result[$k]);
                 continue;
             }
             // If out-of-range behavior carrier is set on "Desactivate carrier"
             if ($row['range_behavior']) {
                 $check_delivery_price_by_weight = Carrier::checkDeliveryPriceByWeight($row['id_carrier'], $this->getTotalWeight(), (int) $id_zone);
                 $total_order = $total_package_without_shipping_tax_inc;
                 $check_delivery_price_by_price = Carrier::checkDeliveryPriceByPrice($row['id_carrier'], $total_order, (int) $id_zone, (int) $this->id_currency);
                 // Get only carriers that have a range compatible with cart
                 if ($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT && !$check_delivery_price_by_weight || $carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_PRICE && !$check_delivery_price_by_price) {
                     unset($result[$k]);
                     continue;
                 }
             }
             if ($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT) {
                 $shipping = $carrier->getDeliveryPriceByWeight($this->getTotalWeight($product_list), (int) $id_zone);
             } else {
                 $shipping = $carrier->getDeliveryPriceByPrice($order_total, (int) $id_zone, (int) $this->id_currency);
             }
             if (!isset($min_shipping_price)) {
                 $min_shipping_price = $shipping;
             }
             if ($shipping <= $min_shipping_price) {
                 $id_carrier = (int) $row['id_carrier'];
                 $min_shipping_price = $shipping;
             }
         }
     }
     if (empty($id_carrier)) {
         $id_carrier = Configuration::get('PS_CARRIER_DEFAULT');
     }
     if (!isset(self::$_carriers[$id_carrier])) {
         self::$_carriers[$id_carrier] = new Carrier((int) $id_carrier, Configuration::get('PS_LANG_DEFAULT'));
     }
     $carrier = self::$_carriers[$id_carrier];
     // No valid Carrier or $id_carrier <= 0 ?
     if (!Validate::isLoadedObject($carrier)) {
         Cache::store($cache_id, 0);
         return 0;
     }
     if (!$carrier->active) {
         Cache::store($cache_id, $shipping_cost);
         return $shipping_cost;
     }
     // Free fees if free carrier
     if ($carrier->is_free == 1) {
         Cache::store($cache_id, 0);
         return 0;
     }
     // Select carrier tax
     if ($use_tax && !Tax::excludeTaxeOption()) {
         $address = Address::initialize((int) $address_id);
         $carrier_tax = $carrier->getTaxesRate($address);
     }
     $configuration = Configuration::getMultiple(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 = Tools::convertPrice((double) $configuration['PS_SHIPPING_FREE_PRICE'], Currency::getCurrencyInstance((int) $this->id_currency));
     }
     $orderTotalwithDiscounts = $this->getOrderTotal(true, Cart::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) {
         Cache::store($cache_id, $shipping_cost);
         return $shipping_cost;
     }
     // Get shipping cost using correct method
     if ($carrier->range_behavior) {
         if (!isset($id_zone)) {
             // Get id zone
             if (isset($this->id_address_delivery) && $this->id_address_delivery && Customer::customerHasAddress($this->id_customer, $this->id_address_delivery)) {
                 $id_zone = Address::getZoneById((int) $this->id_address_delivery);
             } else {
                 $id_zone = (int) $default_country->id_zone;
             }
         }
         if ($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT && !Carrier::checkDeliveryPriceByWeight($carrier->id, $this->getTotalWeight(), (int) $id_zone) || $carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_PRICE && !Carrier::checkDeliveryPriceByPrice($carrier->id, $total_package_without_shipping_tax_inc, $id_zone, (int) $this->id_currency)) {
             $shipping_cost += 0;
         } else {
             if ($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT) {
                 $shipping_cost += $carrier->getDeliveryPriceByWeight($this->getTotalWeight($product_list), $id_zone);
             } else {
                 // by price
                 $shipping_cost += $carrier->getDeliveryPriceByPrice($order_total, $id_zone, (int) $this->id_currency);
             }
         }
     } else {
         if ($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT) {
             $shipping_cost += $carrier->getDeliveryPriceByWeight($this->getTotalWeight($product_list), $id_zone);
         } else {
             $shipping_cost += $carrier->getDeliveryPriceByPrice($order_total, $id_zone, (int) $this->id_currency);
         }
     }
     // 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 = Tools::convertPrice($shipping_cost, Currency::getCurrencyInstance((int) $this->id_currency));
     //get external shipping cost from module
     if ($carrier->shipping_external) {
         $module_name = $carrier->external_module_name;
         $module = Module::getInstanceByName($module_name);
         if (Validate::isLoadedObject($module)) {
             if (array_key_exists('id_carrier', $module)) {
                 $module->id_carrier = $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) {
                 Cache::store($cache_id, false);
                 return false;
             }
         } else {
             Cache::store($cache_id, false);
             return false;
         }
     }
     $shipping_cost = (double) Tools::ps_round((double) $shipping_cost, 2);
     Cache::store($cache_id, $shipping_cost);
     return $shipping_cost;
 }
Ejemplo n.º 3
0
    public function getProducts($refresh = false, $id_product = false, $id_country = null)
    {
        if (!$this->id) {
            return array();
        }
        // Product cache must be strictly compared to NULL, or else an empty cart will add dozens of queries
        if ($this->_products !== null && !$refresh) {
            // Return product row with specified ID if it exists
            if (is_int($id_product)) {
                foreach ($this->_products as $product) {
                    if ($product['id_product'] == $id_product) {
                        return array($product);
                    }
                }
                return array();
            }
            return $this->_products;
        }
        // Build query
        $sql = new DbQuery();
        // Build SELECT
        $sql->select('cp.`id_product_attribute`, cp.`id_product`, cp.`quantity` AS cart_quantity, cp.id_shop, pl.`name`, p.`is_virtual`,
						pl.`description_short`, pl.`available_now`, pl.`available_later`, product_shop.`id_category_default`, p.`id_supplier`,
						p.`id_manufacturer`, product_shop.`on_sale`, product_shop.`ecotax`, product_shop.`additional_shipping_cost`,
						product_shop.`available_for_order`, product_shop.`price`, product_shop.`active`, product_shop.`unity`, product_shop.`unit_price_ratio`, 
						stock.`quantity` AS quantity_available, p.`width`, p.`height`, p.`depth`, stock.`out_of_stock`, p.`weight`,
						p.`date_add`, p.`date_upd`, IFNULL(stock.quantity, 0) as quantity, pl.`link_rewrite`, cl.`link_rewrite` AS category,
						CONCAT(LPAD(cp.`id_product`, 10, 0), LPAD(IFNULL(cp.`id_product_attribute`, 0), 10, 0), IFNULL(cp.`id_address_delivery`, 0)) AS unique_id, cp.id_address_delivery,
						product_shop.`wholesale_price`, product_shop.advanced_stock_management, ps.product_supplier_reference supplier_reference');
        // Build FROM
        $sql->from('cart_product', 'cp');
        // Build JOIN
        $sql->leftJoin('product', 'p', 'p.`id_product` = cp.`id_product`');
        $sql->innerJoin('product_shop', 'product_shop', '(product_shop.`id_shop` = cp.`id_shop` AND product_shop.`id_product` = p.`id_product`)');
        $sql->leftJoin('product_lang', 'pl', '
			p.`id_product` = pl.`id_product`
			AND pl.`id_lang` = ' . (int) $this->id_lang . Shop::addSqlRestrictionOnLang('pl', 'cp.id_shop'));
        $sql->leftJoin('category_lang', 'cl', '
			product_shop.`id_category_default` = cl.`id_category`
			AND cl.`id_lang` = ' . (int) $this->id_lang . Shop::addSqlRestrictionOnLang('cl', 'cp.id_shop'));
        $sql->leftJoin('product_supplier', 'ps', 'ps.`id_product` = cp.`id_product` AND ps.`id_product_attribute` = cp.`id_product_attribute` AND ps.`id_supplier` = p.`id_supplier`');
        // @todo test if everything is ok, then refactorise call of this method
        $sql->join(Product::sqlStock('cp', 'cp'));
        // Build WHERE clauses
        $sql->where('cp.`id_cart` = ' . (int) $this->id);
        if ($id_product) {
            $sql->where('cp.`id_product` = ' . (int) $id_product);
        }
        $sql->where('p.`id_product` IS NOT NULL');
        // Build GROUP BY
        $sql->groupBy('unique_id');
        // Build ORDER BY
        $sql->orderBy('p.`id_product`, cp.`id_product_attribute`, cp.`date_add` ASC');
        if (Customization::isFeatureActive()) {
            $sql->select('cu.`id_customization`, cu.`quantity` AS customization_quantity');
            $sql->leftJoin('customization', 'cu', 'p.`id_product` = cu.`id_product` AND cp.`id_product_attribute` = cu.`id_product_attribute` AND cu.`id_cart` = ' . (int) $this->id);
        } else {
            $sql->select('NULL AS customization_quantity, NULL AS id_customization');
        }
        if (Combination::isFeatureActive()) {
            $sql->select('
				product_attribute_shop.`price` AS price_attribute, product_attribute_shop.`ecotax` AS ecotax_attr,
				IF (IFNULL(pa.`reference`, \'\') = \'\', p.`reference`, pa.`reference`) AS reference,
				(p.`weight`+ pa.`weight`) weight_attribute,
				IF (IFNULL(pa.`ean13`, \'\') = \'\', p.`ean13`, pa.`ean13`) AS ean13,
				IF (IFNULL(pa.`upc`, \'\') = \'\', p.`upc`, pa.`upc`) AS upc,
				pai.`id_image` as pai_id_image, il.`legend` as pai_legend,
				IFNULL(product_attribute_shop.`minimal_quantity`, product_shop.`minimal_quantity`) as minimal_quantity
			');
            $sql->leftJoin('product_attribute', 'pa', 'pa.`id_product_attribute` = cp.`id_product_attribute`');
            $sql->leftJoin('product_attribute_shop', 'product_attribute_shop', '(product_attribute_shop.`id_shop` = cp.`id_shop` AND product_attribute_shop.`id_product_attribute` = pa.`id_product_attribute`)');
            $sql->leftJoin('product_attribute_image', 'pai', 'pai.`id_product_attribute` = pa.`id_product_attribute`');
            $sql->leftJoin('image_lang', 'il', 'il.`id_image` = pai.`id_image` AND il.`id_lang` = ' . (int) $this->id_lang);
        } else {
            $sql->select('p.`reference` AS reference, p.`ean13`,
				p.`upc` AS upc, product_shop.`minimal_quantity` AS minimal_quantity');
        }
        $result = Db::getInstance()->executeS($sql);
        // Reset the cache before the following return, or else an empty cart will add dozens of queries
        $products_ids = array();
        $pa_ids = array();
        if ($result) {
            foreach ($result as $row) {
                $products_ids[] = $row['id_product'];
                $pa_ids[] = $row['id_product_attribute'];
            }
        }
        // Thus you can avoid one query per product, because there will be only one query for all the products of the cart
        Product::cacheProductsFeatures($products_ids);
        Cart::cacheSomeAttributesLists($pa_ids, $this->id_lang);
        $this->_products = array();
        if (empty($result)) {
            return array();
        }
        $cart_shop_context = Context::getContext()->cloneContext();
        foreach ($result as &$row) {
            //                        $quantityDiscount = SpecificPrice::getQuantityDiscount((int)$row['id_product'], $row['id_shop'],
            //			(int)$cart->id_currency, (int)$this->vat_address->id_country,
            //			(int)$this->customer->id_default_group, (int)$row['cart_quantity'], false, null, null, $null, true, true, $this->context);
            //
            //                        echo '<pre>';
            //                        print_r($quantityDiscount);
            //                        echo '</pre>';
            if (isset($row['ecotax_attr']) && $row['ecotax_attr'] > 0) {
                $row['ecotax'] = (double) $row['ecotax_attr'];
            }
            $row['stock_quantity'] = (int) $row['quantity'];
            // for compatibility with 1.2 themes
            $row['quantity'] = (int) $row['cart_quantity'];
            if (isset($row['id_product_attribute']) && (int) $row['id_product_attribute'] && isset($row['weight_attribute'])) {
                $row['weight'] = (double) $row['weight_attribute'];
            }
            if (Configuration::get('PS_TAX_ADDRESS_TYPE') == 'id_address_invoice') {
                $address_id = (int) $this->id_address_invoice;
            } else {
                $address_id = (int) $row['id_address_delivery'];
            }
            if (!Address::addressExists($address_id)) {
                $address_id = null;
            }
            if ($cart_shop_context->shop->id != $row['id_shop']) {
                $cart_shop_context->shop = new Shop((int) $row['id_shop']);
            }
            if ($this->_taxCalculationMethod == PS_TAX_EXC) {
                $row['price'] = Product::getPriceStatic((int) $row['id_product'], false, isset($row['id_product_attribute']) ? (int) $row['id_product_attribute'] : null, 2, null, false, true, (int) $row['cart_quantity'], false, (int) $this->id_customer ? (int) $this->id_customer : null, (int) $this->id, (int) $address_id ? (int) $address_id : null, $specific_price_output, true, true, $cart_shop_context);
                // Here taxes are computed only once the quantity has been applied to the product price
                $row['price_wt'] = Product::getPriceStatic((int) $row['id_product'], true, isset($row['id_product_attribute']) ? (int) $row['id_product_attribute'] : null, 2, null, false, true, (int) $row['cart_quantity'], false, (int) $this->id_customer ? (int) $this->id_customer : null, (int) $this->id, (int) $address_id ? (int) $address_id : null, $null, true, true, $cart_shop_context);
                $tax_rate = Tax::getProductTaxRate((int) $row['id_product'], (int) $address_id);
                $row['total_wt'] = Tools::ps_round($row['price'] * (double) $row['cart_quantity'] * (1 + (double) $tax_rate / 100), 2);
                $row['total'] = $row['price'] * (int) $row['cart_quantity'];
            } else {
                $row['price'] = Product::getPriceStatic((int) $row['id_product'], false, (int) $row['id_product_attribute'], 2, null, false, true, $row['cart_quantity'], false, (int) $this->id_customer ? (int) $this->id_customer : null, (int) $this->id, (int) $address_id ? (int) $address_id : null, $specific_price_output, true, true, $cart_shop_context);
                $row['price_wt'] = Product::getPriceStatic((int) $row['id_product'], true, (int) $row['id_product_attribute'], 2, null, false, true, $row['cart_quantity'], false, (int) $this->id_customer ? (int) $this->id_customer : null, (int) $this->id, (int) $address_id ? (int) $address_id : null, $null, true, true, $cart_shop_context);
                // In case when you use QuantityDiscount, getPriceStatic() can be return more of 2 decimals
                $row['price_wt'] = Tools::ps_round($row['price_wt'], 2);
                $row['total_wt'] = $row['price_wt'] * (int) $row['cart_quantity'];
                $row['total'] = Tools::ps_round($row['price'] * (int) $row['cart_quantity'], 2);
                $row['description_short'] = Tools::nl2br($row['description_short']);
            }
            if (!isset($row['pai_id_image']) || $row['pai_id_image'] == 0) {
                $cache_id = 'Cart::getProducts_' . '-pai_id_image-' . (int) $row['id_product'] . '-' . (int) $this->id_lang . '-' . (int) $row['id_shop'];
                if (!Cache::isStored($cache_id)) {
                    $row2 = Db::getInstance()->getRow('
						SELECT image_shop.`id_image` id_image, il.`legend`
						FROM `' . _DB_PREFIX_ . 'image` i
						JOIN `' . _DB_PREFIX_ . 'image_shop` image_shop ON (i.id_image = image_shop.id_image AND image_shop.cover=1 AND image_shop.id_shop=' . (int) $row['id_shop'] . ')
						LEFT JOIN `' . _DB_PREFIX_ . 'image_lang` il ON (image_shop.`id_image` = il.`id_image` AND il.`id_lang` = ' . (int) $this->id_lang . ')
						WHERE i.`id_product` = ' . (int) $row['id_product'] . ' AND image_shop.`cover` = 1');
                    Cache::store($cache_id, $row2);
                }
                $row2 = Cache::retrieve($cache_id);
                if (!$row2) {
                    $row2 = array('id_image' => false, 'legend' => false);
                } else {
                    $row = array_merge($row, $row2);
                }
            } else {
                $row['id_image'] = $row['pai_id_image'];
                $row['legend'] = $row['pai_legend'];
            }
            $row['reduction_applies'] = $specific_price_output && (double) $specific_price_output['reduction'];
            $row['quantity_discount_applies'] = $specific_price_output && $row['cart_quantity'] >= (int) $specific_price_output['from_quantity'];
            $row['id_image'] = Product::defineProductImage($row, $this->id_lang);
            $row['allow_oosp'] = Product::isAvailableWhenOutOfStock($row['out_of_stock']);
            $row['features'] = Product::getFeaturesStatic((int) $row['id_product']);
            if (array_key_exists($row['id_product_attribute'] . '-' . $this->id_lang, self::$_attributesLists)) {
                $row = array_merge($row, self::$_attributesLists[$row['id_product_attribute'] . '-' . $this->id_lang]);
            }
            if (Context::getContext()->language->id != 1) {
                $row['name'] = Tools::rus2translit($row['name']);
            }
            $row = Product::getTaxesInformations($row, $cart_shop_context);
            $this->_products[] = $row;
        }
        return $this->_products;
    }
Ejemplo n.º 4
0
 protected function addressImportOne($info, $force_ids, $validateOnly = false)
 {
     AdminImportController::setDefaultValues($info);
     if ($force_ids && isset($info['id']) && (int) $info['id']) {
         $address = new Address((int) $info['id']);
     } else {
         if (array_key_exists('id', $info) && (int) $info['id'] && Address::addressExists((int) $info['id'])) {
             $address = new Address((int) $info['id']);
         } else {
             $address = new Address();
         }
     }
     AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $address);
     if (isset($address->country) && is_numeric($address->country)) {
         if (Country::getNameById(Configuration::get('PS_LANG_DEFAULT'), (int) $address->country)) {
             $address->id_country = (int) $address->country;
         }
     } elseif (isset($address->country) && is_string($address->country) && !empty($address->country)) {
         if ($id_country = Country::getIdByName(null, $address->country)) {
             $address->id_country = (int) $id_country;
         } else {
             $country = new Country();
             $country->active = 1;
             $country->name = AdminImportController::createMultiLangField($address->country);
             $country->id_zone = 0;
             // Default zone for country to create
             $country->iso_code = Tools::strtoupper(Tools::substr($address->country, 0, 2));
             // Default iso for country to create
             $country->contains_states = 0;
             // Default value for country to create
             $lang_field_error = $country->validateFieldsLang(UNFRIENDLY_ERROR, true);
             if (($field_error = $country->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $country->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && !$validateOnly && $country->add()) {
                 $address->id_country = (int) $country->id;
             } else {
                 if (!$validateOnly) {
                     $default_language_id = (int) Configuration::get('PS_LANG_DEFAULT');
                     $this->errors[] = sprintf($this->trans('%s cannot be saved', array(), 'Admin.Parameters.Notification'), $country->name[$default_language_id]);
                 }
                 if ($field_error !== true || isset($lang_field_error) && $lang_field_error !== true) {
                     $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
                 }
             }
         }
     }
     if (isset($address->state) && is_numeric($address->state)) {
         if (State::getNameById((int) $address->state)) {
             $address->id_state = (int) $address->state;
         }
     } elseif (isset($address->state) && is_string($address->state) && !empty($address->state)) {
         if ($id_state = State::getIdByName($address->state)) {
             $address->id_state = (int) $id_state;
         } else {
             $state = new State();
             $state->active = 1;
             $state->name = $address->state;
             $state->id_country = isset($country->id) ? (int) $country->id : 0;
             $state->id_zone = 0;
             // Default zone for state to create
             $state->iso_code = Tools::strtoupper(Tools::substr($address->state, 0, 2));
             // Default iso for state to create
             $state->tax_behavior = 0;
             if (($field_error = $state->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $state->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && !$validateOnly && $state->add()) {
                 $address->id_state = (int) $state->id;
             } else {
                 if (!$validateOnly) {
                     $this->errors[] = sprintf($this->trans('%s cannot be saved', array(), 'Admin.Parameters.Notification'), $state->name);
                 }
                 if ($field_error !== true || isset($lang_field_error) && $lang_field_error !== true) {
                     $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
                 }
             }
         }
     }
     if (isset($address->customer_email) && !empty($address->customer_email)) {
         if (Validate::isEmail($address->customer_email)) {
             // a customer could exists in different shop
             $customer_list = Customer::getCustomersByEmail($address->customer_email);
             if (count($customer_list) == 0) {
                 $this->errors[] = sprintf(Tools::displayError('%1$s does not exist in database %2$s (ID: %3$s), and therefore cannot be ' . ($validateOnly ? 'validated' : 'saved')), Db::getInstance()->getMsgError(), $address->customer_email, isset($info['id']) && !empty($info['id']) ? $info['id'] : 'null');
             }
         } else {
             $this->errors[] = sprintf($this->trans('"%s" is not a valid email address.', array(), 'Admin.Parameters.Notification'), $address->customer_email);
             return;
         }
     } elseif (isset($address->id_customer) && !empty($address->id_customer)) {
         if (Customer::customerIdExistsStatic((int) $address->id_customer)) {
             $customer = new Customer((int) $address->id_customer);
             // a customer could exists in different shop
             $customer_list = Customer::getCustomersByEmail($customer->email);
             if (count($customer_list) == 0) {
                 $this->errors[] = sprintf(Tools::displayError('%1$s does not exist in database %2$s (ID: %3$s), and therefore cannot be ' . ($validateOnly ? 'validated' : 'saved')), Db::getInstance()->getMsgError(), $customer->email, (int) $address->id_customer);
             }
         } else {
             $this->errors[] = sprintf(Tools::displayError('The customer ID #%d does not exist in the database, and therefore cannot be ' . ($validateOnly ? 'validated' : 'saved')), $address->id_customer);
         }
     } else {
         $customer_list = array();
         $address->id_customer = 0;
     }
     if (isset($address->manufacturer) && is_numeric($address->manufacturer) && Manufacturer::manufacturerExists((int) $address->manufacturer)) {
         $address->id_manufacturer = (int) $address->manufacturer;
     } elseif (isset($address->manufacturer) && is_string($address->manufacturer) && !empty($address->manufacturer)) {
         if ($manufacturerId = Manufacturer::getIdByName($address->manufacturer)) {
             $address->id_manufacturer = $manufacturerId;
         } else {
             $manufacturer = new Manufacturer();
             $manufacturer->name = $address->manufacturer;
             if (($field_error = $manufacturer->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $manufacturer->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && !$validateOnly && $manufacturer->add()) {
                 $address->id_manufacturer = (int) $manufacturer->id;
             } else {
                 if (!$validateOnly) {
                     $this->errors[] = Db::getInstance()->getMsgError() . ' ' . sprintf($this->trans('%1$s (ID: %2$s) cannot be saved', array(), 'Admin.Parameters.Notification'), $manufacturer->name, isset($manufacturer->id) && !empty($manufacturer->id) ? $manufacturer->id : 'null');
                 }
                 if ($field_error !== true || isset($lang_field_error) && $lang_field_error !== true) {
                     $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
                 }
             }
         }
     }
     if (isset($address->supplier) && is_numeric($address->supplier) && Supplier::supplierExists((int) $address->supplier)) {
         $address->id_supplier = (int) $address->supplier;
     } elseif (isset($address->supplier) && is_string($address->supplier) && !empty($address->supplier)) {
         if ($supplierId = Supplier::getIdByName($address->supplier)) {
             $address->id_supplier = $supplierId;
         } else {
             $supplier = new Supplier();
             $supplier->name = $address->supplier;
             if (($field_error = $supplier->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $supplier->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && !$validateOnly && $supplier->add()) {
                 $address->id_supplier = (int) $supplier->id;
             } else {
                 if (!$validateOnly) {
                     $this->errors[] = Db::getInstance()->getMsgError() . ' ' . sprintf($this->trans('%1$s (ID: %2$s) cannot be saved', array(), 'Admin.Parameters.Notification'), $supplier->name, isset($supplier->id) && !empty($supplier->id) ? $supplier->id : 'null');
                 }
                 if ($field_error !== true || isset($lang_field_error) && $lang_field_error !== true) {
                     $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
                 }
             }
         }
     }
     $res = false;
     if (($field_error = $address->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $address->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true) {
         $address->force_id = (bool) $force_ids;
         if (isset($customer_list) && count($customer_list) > 0) {
             $filter_list = array();
             foreach ($customer_list as $customer) {
                 if (in_array($customer['id_customer'], $filter_list)) {
                     continue;
                 }
                 $filter_list[] = $customer['id_customer'];
                 $address->id_customer = $customer['id_customer'];
             }
         }
         if ($address->id && $address->addressExists($address->id)) {
             $res = $validateOnly || $address->update();
         }
         if (!$res) {
             $res = $validateOnly || $address->add();
         }
     }
     if (!$res) {
         if (!$validateOnly) {
             $this->errors[] = sprintf($this->trans('%1$s (ID: %2$s) cannot be saved', array(), 'Admin.Parameters.Notification'), $info['alias'], isset($info['id']) && !empty($info['id']) ? $info['id'] : 'null');
         }
         if ($field_error !== true || isset($lang_field_error) && $lang_field_error !== true) {
             $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
         }
     }
 }
Ejemplo n.º 5
0
 /**
  * Check if an address exists depending on given $id_address
  * @param $id_address
  * @return bool
  */
 public function addressExists($id_address)
 {
     return Address::addressExists($id_address);
 }
Ejemplo n.º 6
0
 /**
  * Return package shipping cost
  *
  * @param integer $id_carrier Carrier ID (default : current carrier)
  * @param booleal $use_tax
  * @param Country $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($id_carrier = null, $use_tax = true, Country $default_country = null, $product_list = null)
 {
     if ($this->isVirtualCart()) {
         return 0;
     }
     if (!$default_country) {
         $default_country = Context::getContext()->country;
     }
     $complete_product_list = $this->getProducts();
     if (is_null($product_list)) {
         $products = $complete_product_list;
     } else {
         $products = $product_list;
     }
     if (Configuration::get('PS_TAX_ADDRESS_TYPE') == 'id_address_invoice') {
         $address_id = (int) $this->id_address_invoice;
     } elseif (count($product_list)) {
         $prod = current($product_list);
         $address_id = (int) $prod['id_address_delivery'];
     } else {
         $address_id = null;
     }
     if (!Address::addressExists($address_id)) {
         $address_id = null;
     }
     $cache_id = 'getPackageShippingCost_' . (int) $this->id . '_' . (int) $address_id . '_' . (int) $id_carrier . '_' . (int) $use_tax . '_' . (int) $default_country->id;
     if ($products) {
         foreach ($products as $product) {
             $cache_id .= '_' . (int) $product['id_product'] . '_' . (int) $product['id_product_attribute'];
         }
     }
     if (Cache::isStored($cache_id)) {
         return Cache::retrieve($cache_id);
     }
     // Order total in default currency without fees
     $order_total = $this->getOrderTotal(true, Cart::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)) {
         Cache::store($cache_id, $shipping_cost);
         return $shipping_cost;
     }
     // Get id zone
     if (!$this->isMultiAddressDelivery() && isset($this->id_address_delivery) && $this->id_address_delivery && Customer::customerHasAddress($this->id_customer, $this->id_address_delivery)) {
         $id_zone = Address::getZoneById((int) $this->id_address_delivery);
     } else {
         if (!Validate::isLoadedObject($default_country)) {
             $default_country = new Country(Configuration::get('PS_COUNTRY_DEFAULT'), Configuration::get('PS_LANG_DEFAULT'));
         }
         $id_zone = (int) $default_country->id_zone;
     }
     if ($id_carrier && !$this->isCarrierInRange((int) $id_carrier, (int) $id_zone)) {
         $id_carrier = '';
     }
     if (empty($id_carrier) && $this->isCarrierInRange((int) Configuration::get('PS_CARRIER_DEFAULT'), (int) $id_zone)) {
         $id_carrier = (int) Configuration::get('PS_CARRIER_DEFAULT');
     }
     if (empty($id_carrier)) {
         if ((int) $this->id_customer) {
             $customer = new Customer((int) $this->id_customer);
             $result = Carrier::getCarriers((int) Configuration::get('PS_LANG_DEFAULT'), true, false, (int) $id_zone, $customer->getGroups());
             unset($customer);
         } else {
             $result = Carrier::getCarriers((int) Configuration::get('PS_LANG_DEFAULT'), true, false, (int) $id_zone);
         }
         foreach ($result as $k => $row) {
             if ($row['id_carrier'] == Configuration::get('PS_CARRIER_DEFAULT')) {
                 continue;
             }
             if (!isset(self::$_carriers[$row['id_carrier']])) {
                 self::$_carriers[$row['id_carrier']] = new Carrier((int) $row['id_carrier']);
             }
             $carrier = self::$_carriers[$row['id_carrier']];
             // Get only carriers that are compliant with shipping method
             if ($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT && $carrier->getMaxDeliveryPriceByWeight((int) $id_zone) === false || $carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_PRICE && $carrier->getMaxDeliveryPriceByPrice((int) $id_zone) === false) {
                 unset($result[$k]);
                 continue;
             }
             // If out-of-range behavior carrier is set on "Desactivate carrier"
             if ($row['range_behavior']) {
                 $check_delivery_price_by_weight = Carrier::checkDeliveryPriceByWeight($row['id_carrier'], $this->getTotalWeight(), (int) $id_zone);
                 $total_order = $this->getOrderTotal(true, Cart::BOTH_WITHOUT_SHIPPING, $product_list);
                 $check_delivery_price_by_price = Carrier::checkDeliveryPriceByPrice($row['id_carrier'], $total_order, (int) $id_zone, (int) $this->id_currency);
                 // Get only carriers that have a range compatible with cart
                 if ($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT && !$check_delivery_price_by_weight || $carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_PRICE && !$check_delivery_price_by_price) {
                     unset($result[$k]);
                     continue;
                 }
             }
             if ($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT) {
                 $shipping = $carrier->getDeliveryPriceByWeight($this->getTotalWeight($product_list), (int) $id_zone);
             } else {
                 $shipping = $carrier->getDeliveryPriceByPrice($order_total, (int) $id_zone, (int) $this->id_currency);
             }
             if (!isset($min_shipping_price)) {
                 $min_shipping_price = $shipping;
             }
             if ($shipping <= $min_shipping_price) {
                 $id_carrier = (int) $row['id_carrier'];
                 $min_shipping_price = $shipping;
             }
         }
     }
     if (empty($id_carrier)) {
         $id_carrier = Configuration::get('PS_CARRIER_DEFAULT');
     }
     if (!isset(self::$_carriers[$id_carrier])) {
         self::$_carriers[$id_carrier] = new Carrier((int) $id_carrier, Configuration::get('PS_LANG_DEFAULT'));
     }
     $carrier = self::$_carriers[$id_carrier];
     if (!Validate::isLoadedObject($carrier)) {
         die(Tools::displayError('Fatal error: "no default carrier"'));
     }
     if (!$carrier->active) {
         Cache::store($cache_id, $shipping_cost);
         return $shipping_cost;
     }
     // Free fees if free carrier
     if ($carrier->is_free == 1) {
         Cache::store($cache_id, 0);
         return 0;
     }
     // Select carrier tax
     if ($use_tax && !Tax::excludeTaxeOption()) {
         $address = Address::initialize((int) $address_id);
         $carrier_tax = $carrier->getTaxesRate($address);
     }
     $configuration = Configuration::getMultiple(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 = Tools::convertPrice((double) $configuration['PS_SHIPPING_FREE_PRICE'], Currency::getCurrencyInstance((int) $this->id_currency));
     }
     $orderTotalwithDiscounts = $this->getOrderTotal(true, Cart::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) {
         Cache::store($cache_id, $shipping_cost);
         return $shipping_cost;
     }
     // Get shipping cost using correct method
     if ($carrier->range_behavior) {
         // Get id zone
         if (isset($this->id_address_delivery) && $this->id_address_delivery && Customer::customerHasAddress($this->id_customer, $this->id_address_delivery)) {
             $id_zone = Address::getZoneById((int) $this->id_address_delivery);
         } else {
             $id_zone = (int) $default_country->id_zone;
         }
         $check_delivery_price_by_weight = Carrier::checkDeliveryPriceByWeight((int) $carrier->id, $this->getTotalWeight(), (int) $id_zone);
         // Code Review V&V TO FINISH
         $check_delivery_price_by_price = Carrier::checkDeliveryPriceByPrice($carrier->id, $this->getOrderTotal(true, Cart::BOTH_WITHOUT_SHIPPING, $product_list), $id_zone, (int) $this->id_currency);
         if ($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT && !$check_delivery_price_by_weight || $carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_PRICE && !$check_delivery_price_by_price) {
             $shipping_cost += 0;
         } else {
             if ($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT) {
                 $shipping_cost += $carrier->getDeliveryPriceByWeight($this->getTotalWeight($product_list), $id_zone);
             } else {
                 // by price
                 $shipping_cost += $carrier->getDeliveryPriceByPrice($order_total, $id_zone, (int) $this->id_currency);
             }
         }
     } else {
         if ($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT) {
             $shipping_cost += $carrier->getDeliveryPriceByWeight($this->getTotalWeight($product_list), $id_zone);
         } else {
             $shipping_cost += $carrier->getDeliveryPriceByPrice($order_total, $id_zone, (int) $this->id_currency);
         }
     }
     // 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 = Tools::convertPrice($shipping_cost, Currency::getCurrencyInstance((int) $this->id_currency));
     //get external shipping cost from module
     if ($carrier->shipping_external) {
         $module_name = $carrier->external_module_name;
         $module = Module::getInstanceByName($module_name);
         if (Validate::isLoadedObject($module)) {
             if (array_key_exists('id_carrier', $module)) {
                 $module->id_carrier = $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) {
                 Cache::store($cache_id, false);
                 return false;
             }
         } else {
             Cache::store($cache_id, false);
             return false;
         }
     }
     // Apply tax
     if ($use_tax && isset($carrier_tax)) {
         $shipping_cost *= 1 + $carrier_tax / 100;
     }
     // ###############################################################
     $num = $this->getTotalWeight($product_list);
     $countryAndState = Address::getCountryAndState((int) $this->id_address_delivery);
     $state = $countryAndState['id_state'];
     $pri = false;
     if ($pri) {
         echo "carrierid =>" . $id_carrier . "-";
         echo 'products => ' . $num . '-';
         echo 'state => ' . $state . '-';
     }
     //Ground
     //if( $id_carrier == 21 ){
     if ($id_carrier == 31) {
         //NO CA
         if ($state == 313) {
             if ($num == 1) {
                 $shipping_cost = 19.75;
             } elseif ($num == 2) {
                 $shipping_cost = 20.5;
             } elseif ($num == 3) {
                 $shipping_cost = 22.0;
             } elseif ($num == 4) {
                 $shipping_cost = 24.5;
             } elseif ($num == 5) {
                 $shipping_cost = 25.75;
             } elseif ($num == 6) {
                 $shipping_cost = 27.0;
             } elseif ($num == 7) {
                 $shipping_cost = 30.5;
             } elseif ($num == 8) {
                 $shipping_cost = 31.5;
             } elseif ($num == 9) {
                 $shipping_cost = 32.25;
             } elseif ($num == 10) {
                 $shipping_cost = 33.5;
             } elseif ($num == 11) {
                 $shipping_cost = 34.25;
             } else {
                 $shipping_cost = 34.5;
             }
         } elseif ($state == 5 || $state == 28) {
             if ($num == 1) {
                 $shipping_cost = 19.75;
             } elseif ($num == 2) {
                 $shipping_cost = 20.5;
             } elseif ($num == 3) {
                 $shipping_cost = 22.25;
             } elseif ($num == 4) {
                 $shipping_cost = 25.25;
             } elseif ($num == 5) {
                 $shipping_cost = 26.5;
             } elseif ($num == 6) {
                 $shipping_cost = 28.0;
             } elseif ($num == 7) {
                 $shipping_cost = 32.5;
             } elseif ($num == 8) {
                 $shipping_cost = 33.25;
             } elseif ($num == 9) {
                 $shipping_cost = 34.5;
             } elseif ($num == 10) {
                 $shipping_cost = 35.25;
             } elseif ($num == 11) {
                 $shipping_cost = 36.75;
             } else {
                 $shipping_cost = 38.0;
             }
         } elseif ($state == 44 or $state == 37 or $state == 3 or $state == 47) {
             if ($num == 1) {
                 $shipping_cost = 22.75;
             } elseif ($num == 2) {
                 $shipping_cost = 23.75;
             } elseif ($num == 3) {
                 $shipping_cost = 25.0;
             } elseif ($num == 4) {
                 $shipping_cost = 25.75;
             } elseif ($num == 5) {
                 $shipping_cost = 27.5;
             } elseif ($num == 6) {
                 $shipping_cost = 28.25;
             } elseif ($num == 7) {
                 $shipping_cost = 33.25;
             } elseif ($num == 8) {
                 $shipping_cost = 34.5;
             } elseif ($num == 9) {
                 $shipping_cost = 35.5;
             } elseif ($num == 10) {
                 $shipping_cost = 36.75;
             } elseif ($num == 11) {
                 $shipping_cost = 38.0;
             } else {
                 $shipping_cost = 39.5;
             }
         } elseif ($state == 12 || $state == 26 || $state == 6 || $state == 31) {
             if ($num == 1) {
                 $shipping_cost = 23.0;
             } elseif ($num == 2) {
                 $shipping_cost = 24.5;
             } elseif ($num == 3) {
                 $shipping_cost = 25.75;
             } elseif ($num == 4) {
                 $shipping_cost = 26.5;
             } elseif ($num == 5) {
                 $shipping_cost = 28.0;
             } elseif ($num == 6) {
                 $shipping_cost = 29.5;
             } elseif ($num == 7) {
                 $shipping_cost = 34.75;
             } elseif ($num == 8) {
                 $shipping_cost = 36.75;
             } elseif ($num == 9) {
                 $shipping_cost = 38.25;
             } elseif ($num == 10) {
                 $shipping_cost = 39.5;
             } elseif ($num == 11) {
                 $shipping_cost = 41.25;
             } else {
                 $shipping_cost = 42.5;
             }
         } elseif ($state == 34 || $state == 41 || $state == 27 || $state == 16 || $state == 36 || $state == 50) {
             if ($num == 1) {
                 $shipping_cost = 24.0;
             } elseif ($num == 2) {
                 $shipping_cost = 24.75;
             } elseif ($num == 3) {
                 $shipping_cost = 26.25;
             } elseif ($num == 4) {
                 $shipping_cost = 27.75;
             } elseif ($num == 5) {
                 $shipping_cost = 30.25;
             } elseif ($num == 6) {
                 $shipping_cost = 33.25;
             } elseif ($num == 7) {
                 $shipping_cost = 38.5;
             } elseif ($num == 8) {
                 $shipping_cost = 41.25;
             } elseif ($num == 9) {
                 $shipping_cost = 44.0;
             } elseif ($num == 10) {
                 $shipping_cost = 45.75;
             } elseif ($num == 11) {
                 $shipping_cost = 47.75;
             } else {
                 $shipping_cost = 49.5;
             }
         } elseif ($state == 23 || $state == 15 || $state == 25 || $state == 4 || $state == 18 || $state == 24 || $state == 49) {
             if ($num == 1) {
                 $shipping_cost = 24.0;
             } elseif ($num == 2) {
                 $shipping_cost = 25.25;
             } elseif ($num == 3) {
                 $shipping_cost = 27.5;
             } elseif ($num == 4) {
                 $shipping_cost = 29.75;
             } elseif ($num == 5) {
                 $shipping_cost = 32.75;
             } elseif ($num == 6) {
                 $shipping_cost = 36.25;
             } elseif ($num == 7) {
                 $shipping_cost = 42.25;
             } elseif ($num == 8) {
                 $shipping_cost = 44.75;
             } elseif ($num == 9) {
                 $shipping_cost = 47.0;
             } elseif ($num == 10) {
                 $shipping_cost = 49.75;
             } elseif ($num == 11) {
                 $shipping_cost = 52.0;
             } else {
                 $shipping_cost = 54.0;
             }
         } elseif ($state == 2 || $state == 11) {
             if ($num == 1) {
                 $shipping_cost = 46.0;
             } elseif ($num == 2) {
                 $shipping_cost = 56.25;
             } elseif ($num == 3) {
                 $shipping_cost = 64.75;
             } elseif ($num == 4) {
                 $shipping_cost = 74.0;
             } elseif ($num == 5) {
                 $shipping_cost = 77.25;
             } elseif ($num == 6) {
                 $shipping_cost = 85.0;
             } elseif ($num == 7) {
                 $shipping_cost = 91.5;
             } elseif ($num == 8) {
                 $shipping_cost = 98.0;
             } elseif ($num == 9) {
                 $shipping_cost = 105.25;
             } elseif ($num == 10) {
                 $shipping_cost = 111.5;
             } elseif ($num == 11) {
                 $shipping_cost = 117.5;
             } else {
                 $shipping_cost = 124.25;
             }
         } else {
             if ($num == 1) {
                 $shipping_cost = 24.75;
             } elseif ($num == 2) {
                 $shipping_cost = 26.25;
             } elseif ($num == 3) {
                 $shipping_cost = 28.75;
             } elseif ($num == 4) {
                 $shipping_cost = 30.25;
             } elseif ($num == 5) {
                 $shipping_cost = 35.0;
             } elseif ($num == 6) {
                 $shipping_cost = 39.0;
             } elseif ($num == 7) {
                 $shipping_cost = 45.5;
             } elseif ($num == 8) {
                 $shipping_cost = 48.75;
             } elseif ($num == 9) {
                 $shipping_cost = 51.75;
             } elseif ($num == 10) {
                 $shipping_cost = 54.75;
             } elseif ($num == 11) {
                 $shipping_cost = 58.0;
             } else {
                 $shipping_cost = 60.5;
             }
         }
     } elseif ($id_carrier == 30) {
         //NO CA - 302
         if ($state == 313) {
             if ($num == 1) {
                 $shipping_cost = 24.0;
             } elseif ($num == 2) {
                 $shipping_cost = 24.75;
             } elseif ($num == 3) {
                 $shipping_cost = 25.25;
             } elseif ($num == 4) {
                 $shipping_cost = 26.25;
             } elseif ($num == 5) {
                 $shipping_cost = 27.75;
             } elseif ($num == 6) {
                 $shipping_cost = 29.75;
             } elseif ($num == 7) {
                 $shipping_cost = 32.25;
             } elseif ($num == 8) {
                 $shipping_cost = 34.5;
             } elseif ($num == 9) {
                 $shipping_cost = 36.25;
             } elseif ($num == 10) {
                 $shipping_cost = 38.0;
             } elseif ($num == 11) {
                 $shipping_cost = 40.0;
             } else {
                 $shipping_cost = 42.0;
             }
         } elseif ($state == 5 || $state == 28) {
             if ($num == 1) {
                 $shipping_cost = 24.0;
             } elseif ($num == 2) {
                 $shipping_cost = 24.75;
             } elseif ($num == 3) {
                 $shipping_cost = 25.75;
             } elseif ($num == 4) {
                 $shipping_cost = 27.75;
             } elseif ($num == 5) {
                 $shipping_cost = 30.25;
             } elseif ($num == 6) {
                 $shipping_cost = 33.5;
             } elseif ($num == 7) {
                 $shipping_cost = 38.0;
             } elseif ($num == 8) {
                 $shipping_cost = 38.5;
             } elseif ($num == 9) {
                 $shipping_cost = 40.0;
             } elseif ($num == 10) {
                 $shipping_cost = 41.75;
             } elseif ($num == 11) {
                 $shipping_cost = 45.5;
             } else {
                 $shipping_cost = 47.75;
             }
         } elseif ($state == 44 or $state == 37 or $state == 3 or $state == 47) {
             if ($num == 1) {
                 $shipping_cost = 24.75;
             } elseif ($num == 2) {
                 $shipping_cost = 25.75;
             } elseif ($num == 3) {
                 $shipping_cost = 27.0;
             } elseif ($num == 4) {
                 $shipping_cost = 30.5;
             } elseif ($num == 5) {
                 $shipping_cost = 34.0;
             } elseif ($num == 6) {
                 $shipping_cost = 36.75;
             } elseif ($num == 7) {
                 $shipping_cost = 40.25;
             } elseif ($num == 8) {
                 $shipping_cost = 42.25;
             } elseif ($num == 9) {
                 $shipping_cost = 45.25;
             } elseif ($num == 10) {
                 $shipping_cost = 47.75;
             } elseif ($num == 11) {
                 $shipping_cost = 50.75;
             } else {
                 $shipping_cost = 53.5;
             }
         } elseif ($state == 12 || $state == 26 || $state == 6 || $state == 31) {
             if ($num == 1) {
                 $shipping_cost = 24.75;
             } elseif ($num == 2) {
                 $shipping_cost = 27.75;
             } elseif ($num == 3) {
                 $shipping_cost = 29.5;
             } elseif ($num == 4) {
                 $shipping_cost = 33.5;
             } elseif ($num == 5) {
                 $shipping_cost = 38.0;
             } elseif ($num == 6) {
                 $shipping_cost = 40.75;
             } elseif ($num == 7) {
                 $shipping_cost = 45.25;
             } elseif ($num == 8) {
                 $shipping_cost = 48.0;
             } elseif ($num == 9) {
                 $shipping_cost = 52.25;
             } elseif ($num == 10) {
                 $shipping_cost = 54.25;
             } elseif ($num == 11) {
                 $shipping_cost = 57.25;
             } else {
                 $shipping_cost = 62.25;
             }
         } elseif ($state == 34 || $state == 41 || $state == 27 || $state == 16 || $state == 36 || $state == 50) {
             if ($num == 1) {
                 $shipping_cost = 25.5;
             } elseif ($num == 2) {
                 $shipping_cost = 30.5;
             } elseif ($num == 3) {
                 $shipping_cost = 36.0;
             } elseif ($num == 4) {
                 $shipping_cost = 40.25;
             } elseif ($num == 5) {
                 $shipping_cost = 47.75;
             } elseif ($num == 6) {
                 $shipping_cost = 51.25;
             } elseif ($num == 7) {
                 $shipping_cost = 56.25;
             } elseif ($num == 8) {
                 $shipping_cost = 60.25;
             } elseif ($num == 9) {
                 $shipping_cost = 65.0;
             } elseif ($num == 10) {
                 $shipping_cost = 69.5;
             } elseif ($num == 11) {
                 $shipping_cost = 74.25;
             } else {
                 $shipping_cost = 78.5;
             }
         } elseif ($state == 23 || $state == 15 || $state == 25 || $state == 4 || $state == 18 || $state == 24 || $state == 49) {
             if ($num == 1) {
                 $shipping_cost = 31.5;
             } elseif ($num == 2) {
                 $shipping_cost = 33.25;
             } elseif ($num == 3) {
                 $shipping_cost = 38.5;
             } elseif ($num == 4) {
                 $shipping_cost = 43.75;
             } elseif ($num == 5) {
                 $shipping_cost = 49.5;
             } elseif ($num == 6) {
                 $shipping_cost = 54.75;
             } elseif ($num == 7) {
                 $shipping_cost = 62.25;
             } elseif ($num == 8) {
                 $shipping_cost = 67.0;
             } elseif ($num == 9) {
                 $shipping_cost = 72.0;
             } elseif ($num == 10) {
                 $shipping_cost = 77.25;
             } elseif ($num == 11) {
                 $shipping_cost = 81.5;
             } else {
                 $shipping_cost = 86.5;
             }
         } elseif ($state == 2 || $state == 11) {
             if ($num == 1) {
                 $shipping_cost = -1;
             } elseif ($num == 2) {
                 $shipping_cost = -1;
             } elseif ($num == 3) {
                 $shipping_cost = -1;
             } elseif ($num == 4) {
                 $shipping_cost = -1;
             } elseif ($num == 5) {
                 $shipping_cost = -1;
             } elseif ($num == 6) {
                 $shipping_cost = -1;
             } elseif ($num == 7) {
                 $shipping_cost = -1;
             } elseif ($num == 8) {
                 $shipping_cost = -1;
             } elseif ($num == 9) {
                 $shipping_cost = -1;
             } elseif ($num == 10) {
                 $shipping_cost = -1;
             } elseif ($num == 11) {
                 $shipping_cost = -1;
             } else {
                 $shipping_cost = -1;
             }
         } else {
             if ($num == 1) {
                 $shipping_cost = 28.25;
             } elseif ($num == 2) {
                 $shipping_cost = 34.25;
             } elseif ($num == 3) {
                 $shipping_cost = 41.0;
             } elseif ($num == 4) {
                 $shipping_cost = 48.25;
             } elseif ($num == 5) {
                 $shipping_cost = 54.0;
             } elseif ($num == 6) {
                 $shipping_cost = 59.0;
             } elseif ($num == 7) {
                 $shipping_cost = 67.0;
             } elseif ($num == 8) {
                 $shipping_cost = 73.75;
             } elseif ($num == 9) {
                 $shipping_cost = 77.5;
             } elseif ($num == 10) {
                 $shipping_cost = 83.25;
             } elseif ($num == 11) {
                 $shipping_cost = 88.5;
             } else {
                 $shipping_cost = 93.0;
             }
         }
     } elseif ($id_carrier == 32) {
         //NO CA - 202
         if ($state == 313) {
             if ($num == 1) {
                 $shipping_cost = 23.75;
             } elseif ($num == 2) {
                 $shipping_cost = 23.75;
             } elseif ($num == 3) {
                 $shipping_cost = 26.0;
             } elseif ($num == 4) {
                 $shipping_cost = 27.5;
             } elseif ($num == 5) {
                 $shipping_cost = 29.25;
             } elseif ($num == 6) {
                 $shipping_cost = 30.75;
             } elseif ($num == 7) {
                 $shipping_cost = 34.75;
             } elseif ($num == 8) {
                 $shipping_cost = 37.5;
             } elseif ($num == 9) {
                 $shipping_cost = 40.0;
             } elseif ($num == 10) {
                 $shipping_cost = 40.75;
             } elseif ($num == 11) {
                 $shipping_cost = 43.25;
             } else {
                 $shipping_cost = 45.5;
             }
         } elseif ($state == 5 || $state == 28) {
             if ($num == 1) {
                 $shipping_cost = 23.75;
             } elseif ($num == 2) {
                 $shipping_cost = 24.25;
             } elseif ($num == 3) {
                 $shipping_cost = 28.0;
             } elseif ($num == 4) {
                 $shipping_cost = 29.25;
             } elseif ($num == 5) {
                 $shipping_cost = 31.75;
             } elseif ($num == 6) {
                 $shipping_cost = 34.75;
             } elseif ($num == 7) {
                 $shipping_cost = 38.75;
             } elseif ($num == 8) {
                 $shipping_cost = 41.25;
             } elseif ($num == 9) {
                 $shipping_cost = 43.75;
             } elseif ($num == 10) {
                 $shipping_cost = 46.25;
             } elseif ($num == 11) {
                 $shipping_cost = 48.25;
             } else {
                 $shipping_cost = 50.5;
             }
         } elseif ($state == 44 or $state == 37 or $state == 3 or $state == 47) {
             if ($num == 1) {
                 $shipping_cost = 23.75;
             } elseif ($num == 2) {
                 $shipping_cost = 25.5;
             } elseif ($num == 3) {
                 $shipping_cost = 30.75;
             } elseif ($num == 4) {
                 $shipping_cost = 33.0;
             } elseif ($num == 5) {
                 $shipping_cost = 36.25;
             } elseif ($num == 6) {
                 $shipping_cost = 40.5;
             } elseif ($num == 7) {
                 $shipping_cost = 44.75;
             } elseif ($num == 8) {
                 $shipping_cost = 46.5;
             } elseif ($num == 9) {
                 $shipping_cost = 50.25;
             } elseif ($num == 10) {
                 $shipping_cost = 52.5;
             } elseif ($num == 11) {
                 $shipping_cost = 55.25;
             } else {
                 $shipping_cost = 59.0;
             }
         } elseif ($state == 12 || $state == 26 || $state == 6 || $state == 31) {
             if ($num == 1) {
                 $shipping_cost = 27.25;
             } elseif ($num == 2) {
                 $shipping_cost = 30.75;
             } elseif ($num == 3) {
                 $shipping_cost = 36.0;
             } elseif ($num == 4) {
                 $shipping_cost = 41.0;
             } elseif ($num == 5) {
                 $shipping_cost = 46.0;
             } elseif ($num == 6) {
                 $shipping_cost = 50.5;
             } elseif ($num == 7) {
                 $shipping_cost = 57.25;
             } elseif ($num == 8) {
                 $shipping_cost = 61.75;
             } elseif ($num == 9) {
                 $shipping_cost = 65.0;
             } elseif ($num == 10) {
                 $shipping_cost = 68.75;
             } elseif ($num == 11) {
                 $shipping_cost = 72.25;
             } else {
                 $shipping_cost = 76.75;
             }
         } elseif ($state == 34 || $state == 41 || $state == 27 || $state == 16 || $state == 36 || $state == 50) {
             if ($num == 1) {
                 $shipping_cost = 29.25;
             } elseif ($num == 2) {
                 $shipping_cost = 38.75;
             } elseif ($num == 3) {
                 $shipping_cost = 46.5;
             } elseif ($num == 4) {
                 $shipping_cost = 54.75;
             } elseif ($num == 5) {
                 $shipping_cost = 62.75;
             } elseif ($num == 6) {
                 $shipping_cost = 69.75;
             } elseif ($num == 7) {
                 $shipping_cost = 77.75;
             } elseif ($num == 8) {
                 $shipping_cost = 84.25;
             } elseif ($num == 9) {
                 $shipping_cost = 90.75;
             } elseif ($num == 10) {
                 $shipping_cost = 96.75;
             } elseif ($num == 11) {
                 $shipping_cost = 105.0;
             } else {
                 $shipping_cost = 111.25;
             }
         } elseif ($state == 23 || $state == 15 || $state == 25 || $state == 4 || $state == 18 || $state == 24 || $state == 49) {
             if ($num == 1) {
                 $shipping_cost = 32.0;
             } elseif ($num == 2) {
                 $shipping_cost = 40.25;
             } elseif ($num == 3) {
                 $shipping_cost = 49.75;
             } elseif ($num == 4) {
                 $shipping_cost = 58.0;
             } elseif ($num == 5) {
                 $shipping_cost = 65.5;
             } elseif ($num == 6) {
                 $shipping_cost = 74.25;
             } elseif ($num == 7) {
                 $shipping_cost = 82.5;
             } elseif ($num == 8) {
                 $shipping_cost = 89.5;
             } elseif ($num == 9) {
                 $shipping_cost = 96.5;
             } elseif ($num == 10) {
                 $shipping_cost = 102.5;
             } elseif ($num == 11) {
                 $shipping_cost = 109.25;
             } else {
                 $shipping_cost = 116.75;
             }
         } elseif ($state == 2 || $state == 11) {
             if ($num == 1) {
                 $shipping_cost = -1;
             } elseif ($num == 2) {
                 $shipping_cost = -1;
             } elseif ($num == 3) {
                 $shipping_cost = -1;
             } elseif ($num == 4) {
                 $shipping_cost = -1;
             } elseif ($num == 5) {
                 $shipping_cost = -1;
             } elseif ($num == 6) {
                 $shipping_cost = -1;
             } elseif ($num == 7) {
                 $shipping_cost = -1;
             } elseif ($num == 8) {
                 $shipping_cost = -1;
             } elseif ($num == 9) {
                 $shipping_cost = -1;
             } elseif ($num == 10) {
                 $shipping_cost = -1;
             } elseif ($num == 11) {
                 $shipping_cost = -1;
             } else {
                 $shipping_cost = -1;
             }
         } else {
             if ($num == 1) {
                 $shipping_cost = 34.75;
             } elseif ($num == 2) {
                 $shipping_cost = 41.0;
             } elseif ($num == 3) {
                 $shipping_cost = 50.25;
             } elseif ($num == 4) {
                 $shipping_cost = 59.75;
             } elseif ($num == 5) {
                 $shipping_cost = 66.5;
             } elseif ($num == 6) {
                 $shipping_cost = 75.25;
             } elseif ($num == 7) {
                 $shipping_cost = 84.5;
             } elseif ($num == 8) {
                 $shipping_cost = 92.25;
             } elseif ($num == 9) {
                 $shipping_cost = 98.5;
             } elseif ($num == 10) {
                 $shipping_cost = 105.5;
             } elseif ($num == 11) {
                 $shipping_cost = 112.0;
             } else {
                 $shipping_cost = 119.25;
             }
         }
     } elseif ($id_carrier == 29) {
         //NO CA - 102
         if ($state == 313) {
             if ($num == 1) {
                 $shipping_cost = 27.25;
             } elseif ($num == 2) {
                 $shipping_cost = 29.0;
             } elseif ($num == 3) {
                 $shipping_cost = 32.75;
             } elseif ($num == 4) {
                 $shipping_cost = 36.25;
             } elseif ($num == 5) {
                 $shipping_cost = 38.75;
             } elseif ($num == 6) {
                 $shipping_cost = 40.75;
             } elseif ($num == 7) {
                 $shipping_cost = 45.25;
             } elseif ($num == 8) {
                 $shipping_cost = 48.25;
             } elseif ($num == 9) {
                 $shipping_cost = 50.5;
             } elseif ($num == 10) {
                 $shipping_cost = 54.75;
             } elseif ($num == 11) {
                 $shipping_cost = 56.25;
             } else {
                 $shipping_cost = 60.5;
             }
         } elseif ($state == 5 || $state == 28) {
             if ($num == 1) {
                 $shipping_cost = 31.75;
             } elseif ($num == 2) {
                 $shipping_cost = 37.5;
             } elseif ($num == 3) {
                 $shipping_cost = 39.5;
             } elseif ($num == 4) {
                 $shipping_cost = 44.25;
             } elseif ($num == 5) {
                 $shipping_cost = 48.0;
             } elseif ($num == 6) {
                 $shipping_cost = 52.75;
             } elseif ($num == 7) {
                 $shipping_cost = 56.75;
             } elseif ($num == 8) {
                 $shipping_cost = 59.5;
             } elseif ($num == 9) {
                 $shipping_cost = 65.0;
             } elseif ($num == 10) {
                 $shipping_cost = 67.5;
             } elseif ($num == 11) {
                 $shipping_cost = 71.25;
             } else {
                 $shipping_cost = 76.5;
             }
         } elseif ($state == 44 or $state == 37 or $state == 3 or $state == 47) {
             if ($num == 1) {
                 $shipping_cost = 38.75;
             } elseif ($num == 2) {
                 $shipping_cost = 47.75;
             } elseif ($num == 3) {
                 $shipping_cost = 57.75;
             } elseif ($num == 4) {
                 $shipping_cost = 64.0;
             } elseif ($num == 5) {
                 $shipping_cost = 71.25;
             } elseif ($num == 6) {
                 $shipping_cost = 80.75;
             } elseif ($num == 7) {
                 $shipping_cost = 85.75;
             } elseif ($num == 8) {
                 $shipping_cost = 92.5;
             } elseif ($num == 9) {
                 $shipping_cost = 100.75;
             } elseif ($num == 10) {
                 $shipping_cost = 105.25;
             } elseif ($num == 11) {
                 $shipping_cost = 111.5;
             } else {
                 $shipping_cost = 117.75;
             }
         } elseif ($state == 12 || $state == 26 || $state == 6 || $state == 31) {
             if ($num == 1) {
                 $shipping_cost = 41.25;
             } elseif ($num == 2) {
                 $shipping_cost = 50.75;
             } elseif ($num == 3) {
                 $shipping_cost = 61.75;
             } elseif ($num == 4) {
                 $shipping_cost = 70.75;
             } elseif ($num == 5) {
                 $shipping_cost = 78.75;
             } elseif ($num == 6) {
                 $shipping_cost = 86.75;
             } elseif ($num == 7) {
                 $shipping_cost = 91.5;
             } elseif ($num == 8) {
                 $shipping_cost = 99.0;
             } elseif ($num == 9) {
                 $shipping_cost = 105.5;
             } elseif ($num == 10) {
                 $shipping_cost = 113.0;
             } elseif ($num == 11) {
                 $shipping_cost = 119.75;
             } else {
                 $shipping_cost = 126.5;
             }
         } elseif ($state == 34 || $state == 41 || $state == 27 || $state == 16 || $state == 36 || $state == 50) {
             if ($num == 1) {
                 $shipping_cost = 46.75;
             } elseif ($num == 2) {
                 $shipping_cost = 53.75;
             } elseif ($num == 3) {
                 $shipping_cost = 67.25;
             } elseif ($num == 4) {
                 $shipping_cost = 77.0;
             } elseif ($num == 5) {
                 $shipping_cost = 83.75;
             } elseif ($num == 6) {
                 $shipping_cost = 93.0;
             } elseif ($num == 7) {
                 $shipping_cost = 100.25;
             } elseif ($num == 8) {
                 $shipping_cost = 103.0;
             } elseif ($num == 9) {
                 $shipping_cost = 112.25;
             } elseif ($num == 10) {
                 $shipping_cost = 120.5;
             } elseif ($num == 11) {
                 $shipping_cost = 126.25;
             } else {
                 $shipping_cost = 132.0;
             }
         } elseif ($state == 23 || $state == 15 || $state == 25 || $state == 4 || $state == 18 || $state == 24 || $state == 49) {
             if ($num == 1) {
                 $shipping_cost = 49.25;
             } elseif ($num == 2) {
                 $shipping_cost = 56.25;
             } elseif ($num == 3) {
                 $shipping_cost = 69.0;
             } elseif ($num == 4) {
                 $shipping_cost = 79.25;
             } elseif ($num == 5) {
                 $shipping_cost = 88.25;
             } elseif ($num == 6) {
                 $shipping_cost = 95.75;
             } elseif ($num == 7) {
                 $shipping_cost = 101.5;
             } elseif ($num == 8) {
                 $shipping_cost = 109.25;
             } elseif ($num == 9) {
                 $shipping_cost = 116.0;
             } elseif ($num == 10) {
                 $shipping_cost = 123.25;
             } elseif ($num == 11) {
                 $shipping_cost = 130.0;
             } else {
                 $shipping_cost = 138.75;
             }
         } elseif ($state == 2 || $state == 11) {
             if ($num == 1) {
                 $shipping_cost = 53.75;
             } elseif ($num == 2) {
                 $shipping_cost = 67.5;
             } elseif ($num == 3) {
                 $shipping_cost = 78.75;
             } elseif ($num == 4) {
                 $shipping_cost = 85.5;
             } elseif ($num == 5) {
                 $shipping_cost = 96.75;
             } elseif ($num == 6) {
                 $shipping_cost = 101.5;
             } elseif ($num == 7) {
                 $shipping_cost = 116.25;
             } elseif ($num == 8) {
                 $shipping_cost = 130.0;
             } elseif ($num == 9) {
                 $shipping_cost = 142.5;
             } elseif ($num == 10) {
                 $shipping_cost = 152.5;
             } elseif ($num == 11) {
                 $shipping_cost = 159.75;
             } else {
                 $shipping_cost = 168.25;
             }
         } else {
             if ($num == 1) {
                 $shipping_cost = 50.75;
             } elseif ($num == 2) {
                 $shipping_cost = 60.5;
             } elseif ($num == 3) {
                 $shipping_cost = 70.5;
             } elseif ($num == 4) {
                 $shipping_cost = 81.0;
             } elseif ($num == 5) {
                 $shipping_cost = 90.25;
             } elseif ($num == 6) {
                 $shipping_cost = 98.5;
             } elseif ($num == 7) {
                 $shipping_cost = 104.5;
             } elseif ($num == 8) {
                 $shipping_cost = 113.0;
             } elseif ($num == 9) {
                 $shipping_cost = 119.25;
             } elseif ($num == 10) {
                 $shipping_cost = 127.0;
             } elseif ($num == 11) {
                 $shipping_cost = 133.75;
             } else {
                 $shipping_cost = 139.75;
             }
         }
     } elseif ($id_carrier == 16) {
         //NO CA
         if ($state == 313) {
             if ($num == 1) {
                 $shipping_cost = 11.0;
             } elseif ($num == 2) {
                 $shipping_cost = 12.0;
             } elseif ($num == 3) {
                 $shipping_cost = 12.75;
             } elseif ($num == 4) {
                 $shipping_cost = 14.75;
             } elseif ($num == 5) {
                 $shipping_cost = 15.75;
             } elseif ($num == 6) {
                 $shipping_cost = 16.0;
             } elseif ($num == 7) {
                 $shipping_cost = 19.0;
             } elseif ($num == 8) {
                 $shipping_cost = 19.25;
             } elseif ($num == 9) {
                 $shipping_cost = 20.0;
             } elseif ($num == 10) {
                 $shipping_cost = 20.75;
             } elseif ($num == 11) {
                 $shipping_cost = 22.0;
             } else {
                 $shipping_cost = 22.25;
             }
         } elseif ($state == 314) {
             if ($num == 1) {
                 $shipping_cost = 11.75;
             } elseif ($num == 2) {
                 $shipping_cost = 12.5;
             } elseif ($num == 3) {
                 $shipping_cost = 13.75;
             } elseif ($num == 4) {
                 $shipping_cost = 14.75;
             } elseif ($num == 5) {
                 $shipping_cost = 16.0;
             } elseif ($num == 6) {
                 $shipping_cost = 16.75;
             } elseif ($num == 7) {
                 $shipping_cost = 20.0;
             } elseif ($num == 8) {
                 $shipping_cost = 20.75;
             } elseif ($num == 9) {
                 $shipping_cost = 22.0;
             } elseif ($num == 10) {
                 $shipping_cost = 22.75;
             } elseif ($num == 11) {
                 $shipping_cost = 24.0;
             } else {
                 $shipping_cost = 24.0;
             }
         } elseif ($state == 315) {
             if ($num == 1) {
                 $shipping_cost = 12.25;
             } elseif ($num == 2) {
                 $shipping_cost = 13.75;
             } elseif ($num == 3) {
                 $shipping_cost = 14.5;
             } elseif ($num == 4) {
                 $shipping_cost = 15.75;
             } elseif ($num == 5) {
                 $shipping_cost = 16.5;
             } elseif ($num == 6) {
                 $shipping_cost = 17.0;
             } elseif ($num == 7) {
                 $shipping_cost = 20.5;
             } elseif ($num == 8) {
                 $shipping_cost = 22.0;
             } elseif ($num == 9) {
                 $shipping_cost = 22.75;
             } elseif ($num == 10) {
                 $shipping_cost = 24.0;
             } elseif ($num == 11) {
                 $shipping_cost = 25.25;
             } else {
                 $shipping_cost = 26.0;
             }
         } elseif ($state == 3 || $state == 28) {
             if ($num == 1) {
                 $shipping_cost = 12.25;
             } elseif ($num == 2) {
                 $shipping_cost = 14.0;
             } elseif ($num == 3) {
                 $shipping_cost = 14.75;
             } elseif ($num == 4) {
                 $shipping_cost = 16.25;
             } elseif ($num == 5) {
                 $shipping_cost = 17.75;
             } elseif ($num == 6) {
                 $shipping_cost = 18.75;
             } elseif ($num == 7) {
                 $shipping_cost = 22.5;
             } elseif ($num == 8) {
                 $shipping_cost = 24.0;
             } elseif ($num == 9) {
                 $shipping_cost = 25.25;
             } elseif ($num == 10) {
                 $shipping_cost = 26.75;
             } elseif ($num == 11) {
                 $shipping_cost = 28.25;
             } else {
                 $shipping_cost = 29.0;
             }
         } else {
             if ($num == 1) {
                 $shipping_cost = -1;
             } elseif ($num == 2) {
                 $shipping_cost = -1;
             } elseif ($num == 3) {
                 $shipping_cost = -1;
             } elseif ($num == 4) {
                 $shipping_cost = -1;
             } elseif ($num == 5) {
                 $shipping_cost = -1;
             } elseif ($num == 6) {
                 $shipping_cost = -1;
             } elseif ($num == 7) {
                 $shipping_cost = -1;
             } elseif ($num == 8) {
                 $shipping_cost = -1;
             } elseif ($num == 9) {
                 $shipping_cost = -1;
             } elseif ($num == 10) {
                 $shipping_cost = -1;
             } elseif ($num == 11) {
                 $shipping_cost = -1;
             } else {
                 $shipping_cost = -1;
             }
         }
     }
     //###############################################################
     $shipping_cost = (double) Tools::ps_round((double) $shipping_cost, 2);
     Cache::store($cache_id, $shipping_cost);
     return $shipping_cost;
 }
Ejemplo n.º 7
0
    /**
     * Return cart products
     *
     * @result array Products
     */
    public function getProducts($refresh = false, $id_product = false, $id_country = null)
    {
        if (!$this->id) {
            return array();
        }
        // Product cache must be strictly compared to NULL, or else an empty cart will add dozens of queries
        if ($this->_products !== null && !$refresh) {
            // Return product row with specified ID if it exists
            if (is_int($id_product)) {
                foreach ($this->_products as $product) {
                    if ($product['id_product'] == $id_product) {
                        return array($product);
                    }
                }
                return array();
            }
            return $this->_products;
        }
        // Build query
        $sql = new DbQuery();
        // Build SELECT
        $sql->select('cp.`id_product_attribute`, cp.`id_product`, cp.`quantity` AS cart_quantity, cp.id_shop, pl.`name`, p.`is_virtual`,
						pl.`description_short`, pl.`available_now`, pl.`available_later`, product_shop.`id_category_default`, p.`id_supplier`,
						p.`id_manufacturer`, product_shop.`on_sale`, product_shop.`ecotax`, product_shop.`additional_shipping_cost`,
						product_shop.`available_for_order`, product_shop.`price`, product_shop.`active`, product_shop.`unity`, product_shop.`unit_price_ratio`,
						stock.`quantity` AS quantity_available, p.`width`, p.`height`, p.`depth`, stock.`out_of_stock`, p.`weight`,
						p.`date_add`, p.`date_upd`, IFNULL(stock.quantity, 0) as quantity, pl.`link_rewrite`, cl.`link_rewrite` AS category,
						CONCAT(LPAD(cp.`id_product`, 10, 0), LPAD(IFNULL(cp.`id_product_attribute`, 0), 10, 0), IFNULL(cp.`id_address_delivery`, 0)) AS unique_id, cp.id_address_delivery,
						product_shop.advanced_stock_management, ps.product_supplier_reference supplier_reference, IFNULL(sp.`reduction_type`, 0) AS reduction_type');
        // Build FROM
        $sql->from('cart_product', 'cp');
        // Build JOIN
        $sql->leftJoin('product', 'p', 'p.`id_product` = cp.`id_product`');
        $sql->innerJoin('product_shop', 'product_shop', '(product_shop.`id_shop` = cp.`id_shop` AND product_shop.`id_product` = p.`id_product`)');
        $sql->leftJoin('product_lang', 'pl', '
			p.`id_product` = pl.`id_product`
			AND pl.`id_lang` = ' . (int) $this->id_lang . Shop::addSqlRestrictionOnLang('pl', 'cp.id_shop'));
        $sql->leftJoin('category_lang', 'cl', '
			product_shop.`id_category_default` = cl.`id_category`
			AND cl.`id_lang` = ' . (int) $this->id_lang . Shop::addSqlRestrictionOnLang('cl', 'cp.id_shop'));
        $sql->leftJoin('product_supplier', 'ps', 'ps.`id_product` = cp.`id_product` AND ps.`id_product_attribute` = cp.`id_product_attribute` AND ps.`id_supplier` = p.`id_supplier`');
        $sql->leftJoin('specific_price', 'sp', 'sp.`id_product` = cp.`id_product`');
        // AND 'sp.`id_shop` = cp.`id_shop`
        // @todo test if everything is ok, then refactorise call of this method
        $sql->join(Product::sqlStock('cp', 'cp'));
        // Build WHERE clauses
        $sql->where('cp.`id_cart` = ' . (int) $this->id);
        if ($id_product) {
            $sql->where('cp.`id_product` = ' . (int) $id_product);
        }
        $sql->where('p.`id_product` IS NOT NULL');
        // Build GROUP BY
        $sql->groupBy('unique_id');
        // Build ORDER BY
        $sql->orderBy('cp.`date_add`, p.`id_product`, cp.`id_product_attribute` ASC');
        if (Customization::isFeatureActive()) {
            $sql->select('cu.`id_customization`, cu.`quantity` AS customization_quantity');
            $sql->leftJoin('customization', 'cu', 'p.`id_product` = cu.`id_product` AND cp.`id_product_attribute` = cu.`id_product_attribute` AND cu.`id_cart` = ' . (int) $this->id);
        } else {
            $sql->select('NULL AS customization_quantity, NULL AS id_customization');
        }
        if (Combination::isFeatureActive()) {
            $sql->select('
				product_attribute_shop.`price` AS price_attribute, product_attribute_shop.`ecotax` AS ecotax_attr,
				IF (IFNULL(pa.`reference`, \'\') = \'\', p.`reference`, pa.`reference`) AS reference,
				(p.`weight`+ pa.`weight`) weight_attribute,
				IF (IFNULL(pa.`ean13`, \'\') = \'\', p.`ean13`, pa.`ean13`) AS ean13,
				IF (IFNULL(pa.`upc`, \'\') = \'\', p.`upc`, pa.`upc`) AS upc,
				pai.`id_image` as pai_id_image, il.`legend` as pai_legend,
				IFNULL(product_attribute_shop.`minimal_quantity`, product_shop.`minimal_quantity`) as minimal_quantity,
				IF(product_attribute_shop.wholesale_price > 0,  product_attribute_shop.wholesale_price, product_shop.`wholesale_price`) wholesale_price
			');
            $sql->leftJoin('product_attribute', 'pa', 'pa.`id_product_attribute` = cp.`id_product_attribute`');
            $sql->leftJoin('product_attribute_shop', 'product_attribute_shop', '(product_attribute_shop.`id_shop` = cp.`id_shop` AND product_attribute_shop.`id_product_attribute` = pa.`id_product_attribute`)');
            $sql->leftJoin('product_attribute_image', 'pai', 'pai.`id_product_attribute` = pa.`id_product_attribute`');
            $sql->leftJoin('image_lang', 'il', 'il.`id_image` = pai.`id_image` AND il.`id_lang` = ' . (int) $this->id_lang);
        } else {
            $sql->select('p.`reference` AS reference, p.`ean13`,
				p.`upc` AS upc, product_shop.`minimal_quantity` AS minimal_quantity, product_shop.`wholesale_price` wholesale_price');
        }
        $result = Db::getInstance()->executeS($sql);
        // Reset the cache before the following return, or else an empty cart will add dozens of queries
        $products_ids = array();
        $pa_ids = array();
        if ($result) {
            foreach ($result as $row) {
                $products_ids[] = $row['id_product'];
                $pa_ids[] = $row['id_product_attribute'];
            }
        }
        // Thus you can avoid one query per product, because there will be only one query for all the products of the cart
        Product::cacheProductsFeatures($products_ids);
        Cart::cacheSomeAttributesLists($pa_ids, $this->id_lang);
        $this->_products = array();
        if (empty($result)) {
            return array();
        }
        $cart_shop_context = Context::getContext()->cloneContext();
        foreach ($result as &$row) {
            if (isset($row['ecotax_attr']) && $row['ecotax_attr'] > 0) {
                $row['ecotax'] = (double) $row['ecotax_attr'];
            }
            $row['stock_quantity'] = (int) $row['quantity'];
            // for compatibility with 1.2 themes
            $row['quantity'] = (int) $row['cart_quantity'];
            if (isset($row['id_product_attribute']) && (int) $row['id_product_attribute'] && isset($row['weight_attribute'])) {
                $row['weight'] = (double) $row['weight_attribute'];
            }
            if (Configuration::get('PS_TAX_ADDRESS_TYPE') == 'id_address_invoice') {
                $address_id = (int) $this->id_address_invoice;
            } else {
                $address_id = (int) $row['id_address_delivery'];
            }
            if (!Address::addressExists($address_id)) {
                $address_id = null;
            }
            if ($cart_shop_context->shop->id != $row['id_shop']) {
                $cart_shop_context->shop = new Shop((int) $row['id_shop']);
            }
            $address = Address::initialize($address_id, true);
            $id_tax_rules_group = Product::getIdTaxRulesGroupByIdProduct((int) $row['id_product'], $cart_shop_context);
            $tax_calculator = TaxManagerFactory::getManager($address, $id_tax_rules_group)->getTaxCalculator();
            $row['price'] = Product::getPriceStatic((int) $row['id_product'], false, isset($row['id_product_attribute']) ? (int) $row['id_product_attribute'] : null, 6, null, false, true, $row['cart_quantity'], false, (int) $this->id_customer ? (int) $this->id_customer : null, (int) $this->id, $address_id, $specific_price_output, false, true, $cart_shop_context);
            switch (Configuration::get('PS_ROUND_TYPE')) {
                case Order::ROUND_TOTAL:
                case Order::ROUND_LINE:
                    $row['total'] = Tools::ps_round($row['price'] * (int) $row['cart_quantity'], _PS_PRICE_COMPUTE_PRECISION_);
                    $row['total_wt'] = Tools::ps_round($tax_calculator->addTaxes($row['price']) * (int) $row['cart_quantity'], _PS_PRICE_COMPUTE_PRECISION_);
                    break;
                case Order::ROUND_ITEM:
                default:
                    $row['total'] = Tools::ps_round($row['price'], _PS_PRICE_COMPUTE_PRECISION_) * (int) $row['cart_quantity'];
                    $row['total_wt'] = Tools::ps_round($tax_calculator->addTaxes($row['price']), _PS_PRICE_COMPUTE_PRECISION_) * (int) $row['cart_quantity'];
                    break;
            }
            $row['price_wt'] = $tax_calculator->addTaxes($row['price']);
            $row['description_short'] = Tools::nl2br($row['description_short']);
            /**
             * ABU: correction bug
             * https://github.com/PrestaShop/PrestaShop/commit/bbc5591495b12021aa95421af1a0d27acd7a378e?diff=split
             */
            /*if (!isset($row['pai_id_image']) || $row['pai_id_image'] == 0)
            		{
            			$cache_id = 'Cart::getProducts_'.'-pai_id_image-'.(int)$row['id_product'].'-'.(int)$this->id_lang.'-'.(int)$row['id_shop'];
            			if (!Cache::isStored($cache_id))
            			{
            				$row2 = Db::getInstance()->getRow('
            					SELECT image_shop.`id_image` id_image, il.`legend`
            					FROM `'._DB_PREFIX_.'image` i
            					JOIN `'._DB_PREFIX_.'image_shop` image_shop ON (i.id_image = image_shop.id_image AND image_shop.cover=1 AND image_shop.id_shop='.(int)$row['id_shop'].')
            					LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (image_shop.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$this->id_lang.')
            					WHERE i.`id_product` = '.(int)$row['id_product'].' AND image_shop.`cover` = 1'
            				);
            				Cache::store($cache_id, $row2);
            			}
            			$row2 = Cache::retrieve($cache_id);
            			if (!$row2)
            				$row2 = array('id_image' => false, 'legend' => false);
            			else
            				$row = array_merge($row, $row2);
            		}
            		else
            		{
            			$row['id_image'] = $row['pai_id_image'];
            			$row['legend'] = $row['pai_legend'];
            		}*/
            $cache_id = 'Cart::getProducts_' . '-pai_id_image-' . (int) $row['id_product'] . '-' . (int) $row['id_product_attribute'] . '-' . (int) $this->id_lang . '-' . (int) $row['id_shop'];
            if (!Cache::isStored($cache_id)) {
                $flag = (int) $row['id_product_attribute'] && (int) $row['pai_id_image'];
                $row2 = Db::getInstance()->getRow('
					SELECT image_shop.`id_image` id_image, il.`legend`
					FROM `' . _DB_PREFIX_ . 'image` i
					INNER JOIN `' . _DB_PREFIX_ . 'image_shop` image_shop
						ON (i.id_image = image_shop.id_image' . (!$flag ? ' AND image_shop.cover = 1' : '') . ' AND image_shop.id_shop = ' . (int) $row['id_shop'] . ')' . ($flag ? '
					INNER JOIN `' . _DB_PREFIX_ . 'product_attribute_image` pai
						ON (pai.`id_image` = i.`id_image` AND pai.`id_product_attribute` = ' . (int) $row['id_product_attribute'] . ')' : '') . '
					LEFT JOIN `' . _DB_PREFIX_ . 'image_lang` il
						ON (image_shop.`id_image` = il.`id_image` AND il.`id_lang` = ' . (int) $this->id_lang . ')
					WHERE i.`id_product` = ' . (int) $row['id_product'] . ($flag ? '
					ORDER BY i.`position` ASC' : ''));
                Cache::store($cache_id, $row2);
            }
            $row2 = Cache::retrieve($cache_id);
            if (!$row2) {
                $row2 = array('id_image' => false, 'legend' => false);
            }
            $row = array_merge($row, $row2);
            /**
             * /end bug correction
             */
            $row['reduction_applies'] = $specific_price_output && (double) $specific_price_output['reduction'];
            $row['quantity_discount_applies'] = $specific_price_output && $row['cart_quantity'] >= (int) $specific_price_output['from_quantity'];
            $row['id_image'] = Product::defineProductImage($row, $this->id_lang);
            $row['allow_oosp'] = Product::isAvailableWhenOutOfStock($row['out_of_stock']);
            $row['features'] = Product::getFeaturesStatic((int) $row['id_product']);
            if (array_key_exists($row['id_product_attribute'] . '-' . $this->id_lang, self::$_attributesLists)) {
                $row = array_merge($row, self::$_attributesLists[$row['id_product_attribute'] . '-' . $this->id_lang]);
            }
            $row = Product::getTaxesInformations($row, $cart_shop_context);
            $this->_products[] = $row;
        }
        return $this->_products;
    }
Ejemplo n.º 8
0
 public static function getTaxesAverageUsed($id_cart)
 {
     $cart = new Cart((int) $id_cart);
     if (!Validate::isLoadedObject($cart)) {
         die(Tools::displayError());
     }
     if (!Configuration::get('PS_TAX')) {
         return 0;
     }
     $products = $cart->getProducts();
     $total_products_moy = 0;
     $ratio_tax = 0;
     if (!count($products)) {
         return 0;
     }
     foreach ($products as $product) {
         if (Configuration::get('PS_TAX_ADDRESS_TYPE') == 'id_address_invoice') {
             $address_id = (int) $cart->id_address_invoice;
         } else {
             $address_id = (int) $product['id_address_delivery'];
         }
         // Get delivery address of the product from the cart
         if (!Address::addressExists($address_id)) {
             $address_id = null;
         }
         $total_products_moy += $product['total_wt'];
         $ratio_tax += $product['total_wt'] * Tax::getProductTaxRate((int) $product['id_product'], (int) $address_id);
     }
     if ($total_products_moy > 0) {
         return $ratio_tax / $total_products_moy;
     }
     return 0;
 }
Ejemplo n.º 9
0
 /**
  * Initiliaze an address corresponding to the specified id address or if empty to the
  * default shop configuration
  *
  * @param int $id_address
  * @param bool $with_geoloc
  * @return Address address
  *
  * @throws PrestaShopException
  */
 public static function initialize($id_address = null, $with_geoloc = false)
 {
     $context = Context::getContext();
     $exists = (int) $id_address && (bool) Address::addressExists($id_address);
     if ($exists) {
         $context_hash = (int) $id_address;
     } elseif ($with_geoloc && isset($context->customer->geoloc_id_country)) {
         $context_hash = md5((int) $context->customer->geoloc_id_country . '-' . (int) $context->customer->id_state . '-' . $context->customer->postcode);
     } else {
         $context_hash = md5((int) $context->country->id);
     }
     $cache_id = 'Address::initialize_' . $context_hash;
     if (!Cache::isStored($cache_id)) {
         // if an id_address has been specified retrieve the address
         if ($exists) {
             $address = new Address((int) $id_address);
             if (!Validate::isLoadedObject($address)) {
                 throw new PrestaShopException('Invalid address #' . (int) $id_address);
             }
         } elseif ($with_geoloc && isset($context->customer->geoloc_id_country)) {
             $address = new Address();
             $address->id_country = (int) $context->customer->geoloc_id_country;
             $address->id_state = (int) $context->customer->id_state;
             $address->postcode = $context->customer->postcode;
         } else {
             // set the default address
             $address = new Address();
             $address->id_country = (int) $context->country->id;
             $address->id_state = 0;
             $address->postcode = 0;
         }
         Cache::store($cache_id, $address);
         return $address;
     }
     return Cache::retrieve($cache_id);
 }
Ejemplo n.º 10
0
    /**
     * Return cart products
     *
     * @result array Products
     */
    public function getProducts($refresh = false, $id_product = false, $id_country = null)
    {
        if (!$this->id) {
            return array();
        }
        // Product cache must be strictly compared to NULL, or else an empty cart will add dozens of queries
        if ($this->_products !== null && !$refresh) {
            // Return product row with specified ID if it exists
            if (is_int($id_product)) {
                foreach ($this->_products as $product) {
                    if ($product['id_product'] == $id_product) {
                        return array($product);
                    }
                }
                return array();
            }
            return $this->_products;
        }
        // Build query
        $sql = new DbQuery();
        // Build SELECT
        $sql->select('cp.`id_product_attribute`, cp.`id_product`, cp.`quantity` AS cart_quantity, cp.id_shop, cp.delivery_date, cp.delivery_time_to, cp.delivery_time_from, pl.`name`, p.`is_virtual`,
						pl.`description_short`, pl.`available_now`, pl.`available_later`, product_shop.`id_category_default`, p.`id_supplier`,
						p.`id_manufacturer`, product_shop.`on_sale`, product_shop.`ecotax`, product_shop.`additional_shipping_cost`,
						product_shop.`available_for_order`, product_shop.`price`, product_shop.`active`, product_shop.`unity`, product_shop.`unit_price_ratio`,
						stock.`quantity` AS quantity_available, p.`width`, p.`height`, p.`depth`, stock.`out_of_stock`, p.`weight`,
						p.`date_add`, p.`date_upd`, IFNULL(stock.quantity, 0) as quantity, pl.`link_rewrite`, cl.`link_rewrite` AS category,
						CONCAT(LPAD(cp.`id_product`, 10, 0), LPAD(IFNULL(cp.`id_product_attribute`, 0), 10, 0), IFNULL(cp.`id_address_delivery`, 0)) AS unique_id, cp.id_address_delivery,
						product_shop.advanced_stock_management, ps.product_supplier_reference supplier_reference');
        // Build FROM
        $sql->from('cart_product', 'cp');
        // Build JOIN
        $sql->leftJoin('product', 'p', 'p.`id_product` = cp.`id_product`');
        $sql->innerJoin('product_shop', 'product_shop', '(product_shop.`id_shop` = cp.`id_shop` AND product_shop.`id_product` = p.`id_product`)');
        $sql->leftJoin('product_lang', 'pl', '
			p.`id_product` = pl.`id_product`
			AND pl.`id_lang` = ' . (int) $this->id_lang . Shop::addSqlRestrictionOnLang('pl', 'cp.id_shop'));
        $sql->leftJoin('category_lang', 'cl', '
			product_shop.`id_category_default` = cl.`id_category`
			AND cl.`id_lang` = ' . (int) $this->id_lang . Shop::addSqlRestrictionOnLang('cl', 'cp.id_shop'));
        $sql->leftJoin('product_supplier', 'ps', 'ps.`id_product` = cp.`id_product` AND ps.`id_product_attribute` = cp.`id_product_attribute` AND ps.`id_supplier` = p.`id_supplier`');
        // @todo test if everything is ok, then refactorise call of this method
        $sql->join(Product::sqlStock('cp', 'cp'));
        // Build WHERE clauses
        $sql->where('cp.`id_cart` = ' . (int) $this->id);
        if ($id_product) {
            $sql->where('cp.`id_product` = ' . (int) $id_product);
        }
        $sql->where('p.`id_product` IS NOT NULL');
        // Build ORDER BY
        $sql->orderBy('cp.`date_add`, cp.`id_product`, cp.`id_product_attribute` ASC');
        if (Customization::isFeatureActive()) {
            $sql->select('cu.`id_customization`, cu.`quantity` AS customization_quantity');
            $sql->leftJoin('customization', 'cu', 'p.`id_product` = cu.`id_product` AND cp.`id_product_attribute` = cu.`id_product_attribute` AND cu.`id_cart` = ' . (int) $this->id);
            $sql->groupBy('cp.`id_product_attribute`, cp.`id_product`, cp.`id_shop`');
        } else {
            $sql->select('NULL AS customization_quantity, NULL AS id_customization');
        }
        if (Combination::isFeatureActive()) {
            $sql->select('
				product_attribute_shop.`price` AS price_attribute, product_attribute_shop.`ecotax` AS ecotax_attr,
				IF (IFNULL(pa.`reference`, \'\') = \'\', p.`reference`, pa.`reference`) AS reference,
				(p.`weight`+ pa.`weight`) weight_attribute,
				IF (IFNULL(pa.`ean13`, \'\') = \'\', p.`ean13`, pa.`ean13`) AS ean13,
				IF (IFNULL(pa.`upc`, \'\') = \'\', p.`upc`, pa.`upc`) AS upc,
				IFNULL(product_attribute_shop.`minimal_quantity`, product_shop.`minimal_quantity`) as minimal_quantity,
				IF(product_attribute_shop.wholesale_price > 0,  product_attribute_shop.wholesale_price, product_shop.`wholesale_price`) wholesale_price
			');
            $sql->leftJoin('product_attribute', 'pa', 'pa.`id_product_attribute` = cp.`id_product_attribute`');
            $sql->leftJoin('product_attribute_shop', 'product_attribute_shop', '(product_attribute_shop.`id_shop` = cp.`id_shop` AND product_attribute_shop.`id_product_attribute` = pa.`id_product_attribute`)');
        } else {
            $sql->select('p.`reference` AS reference, p.`ean13`,
				p.`upc` AS upc, product_shop.`minimal_quantity` AS minimal_quantity, product_shop.`wholesale_price` wholesale_price');
        }
        $sql->select('image_shop.`id_image` id_image, il.`legend`');
        $sql->leftJoin('image_shop', 'image_shop', 'image_shop.`id_product` = p.`id_product` AND image_shop.cover=1 AND image_shop.id_shop=' . (int) $this->id_shop);
        $sql->leftJoin('image_lang', 'il', 'il.`id_image` = image_shop.`id_image` AND il.`id_lang` = ' . (int) $this->id_lang);
        $result = Db::getInstance()->executeS($sql);
        // Reset the cache before the following return, or else an empty cart will add dozens of queries
        $products_ids = array();
        $pa_ids = array();
        if ($result) {
            foreach ($result as $key => $row) {
                $products_ids[] = $row['id_product'];
                $pa_ids[] = $row['id_product_attribute'];
                $specific_price = SpecificPrice::getSpecificPrice($row['id_product'], $this->id_shop, $this->id_currency, $id_country, $this->id_shop_group, $row['cart_quantity'], $row['id_product_attribute'], $this->id_customer, $this->id);
                if ($specific_price) {
                    $reduction_type_row = array('reduction_type' => $specific_price['reduction_type']);
                } else {
                    $reduction_type_row = array('reduction_type' => 0);
                }
                $result[$key] = array_merge($row, $reduction_type_row);
            }
        }
        // Thus you can avoid one query per product, because there will be only one query for all the products of the cart
        Product::cacheProductsFeatures($products_ids);
        Cart::cacheSomeAttributesLists($pa_ids, $this->id_lang);
        $this->_products = array();
        if (empty($result)) {
            return array();
        }
        $ecotax_rate = (double) Tax::getProductEcotaxRate($this->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
        $apply_eco_tax = Product::$_taxCalculationMethod == PS_TAX_INC && (int) Configuration::get('PS_TAX');
        $cart_shop_context = Context::getContext()->cloneContext();
        foreach ($result as &$row) {
            if (isset($row['ecotax_attr']) && $row['ecotax_attr'] > 0) {
                $row['ecotax'] = (double) $row['ecotax_attr'];
            }
            $row['stock_quantity'] = (int) $row['quantity'];
            // for compatibility with 1.2 themes
            $row['quantity'] = (int) $row['cart_quantity'];
            if (isset($row['id_product_attribute']) && (int) $row['id_product_attribute'] && isset($row['weight_attribute'])) {
                $row['weight'] = (double) $row['weight_attribute'];
            }
            if (Configuration::get('PS_TAX_ADDRESS_TYPE') == 'id_address_invoice') {
                $address_id = (int) $this->id_address_invoice;
            } else {
                $address_id = (int) $row['id_address_delivery'];
            }
            if (!Address::addressExists($address_id)) {
                $address_id = null;
            }
            if ($cart_shop_context->shop->id != $row['id_shop']) {
                $cart_shop_context->shop = new Shop((int) $row['id_shop']);
            }
            $row['shop_name'] = $cart_shop_context->shop->name;
            $address = Address::initialize($address_id, true);
            $id_tax_rules_group = Product::getIdTaxRulesGroupByIdProduct((int) $row['id_product'], $cart_shop_context);
            $tax_calculator = TaxManagerFactory::getManager($address, $id_tax_rules_group)->getTaxCalculator();
            $row['price_without_reduction'] = Product::getPriceStatic((int) $row['id_product'], true, !empty($row['delivery_date']) && $row['delivery_date'] != '0000-00-00' ? 0 : (isset($row['id_product_attribute']) ? (int) $row['id_product_attribute'] : null), 6, null, false, false, $row['cart_quantity'], false, (int) $this->id_customer ? (int) $this->id_customer : null, (int) $this->id, $address_id, $specific_price_output, true, true, $cart_shop_context, true, $row['delivery_date'], $row['delivery_time_from'], $row['delivery_time_to']);
            $row['price_with_reduction'] = Product::getPriceStatic((int) $row['id_product'], true, !empty($row['delivery_date']) && $row['delivery_date'] != '0000-00-00' ? 0 : (isset($row['id_product_attribute']) ? (int) $row['id_product_attribute'] : null), 6, null, false, true, $row['cart_quantity'], false, (int) $this->id_customer ? (int) $this->id_customer : null, (int) $this->id, $address_id, $specific_price_output, true, true, $cart_shop_context, true, $row['delivery_date'], $row['delivery_time_from'], $row['delivery_time_to']);
            $row['price'] = $row['price_with_reduction_without_tax'] = Product::getPriceStatic((int) $row['id_product'], false, !empty($row['delivery_date']) && $row['delivery_date'] != '0000-00-00' ? 0 : (isset($row['id_product_attribute']) ? (int) $row['id_product_attribute'] : null), 6, null, false, true, $row['cart_quantity'], false, (int) $this->id_customer ? (int) $this->id_customer : null, (int) $this->id, $address_id, $specific_price_output, true, true, $cart_shop_context, true, $row['delivery_date'], $row['delivery_time_from'], $row['delivery_time_to']);
            switch (Configuration::get('PS_ROUND_TYPE')) {
                case Order::ROUND_TOTAL:
                    $row['total'] = $row['price_with_reduction_without_tax'] * (int) $row['cart_quantity'];
                    $row['total_wt'] = $row['price_with_reduction'] * (int) $row['cart_quantity'];
                    break;
                case Order::ROUND_LINE:
                    $row['total'] = Tools::ps_round($row['price_with_reduction_without_tax'] * (int) $row['cart_quantity'], _PS_PRICE_COMPUTE_PRECISION_);
                    $row['total_wt'] = Tools::ps_round($row['price_with_reduction'] * (int) $row['cart_quantity'], _PS_PRICE_COMPUTE_PRECISION_);
                    break;
                case Order::ROUND_ITEM:
                default:
                    $row['total'] = Tools::ps_round($row['price_with_reduction_without_tax'], _PS_PRICE_COMPUTE_PRECISION_) * (int) $row['cart_quantity'];
                    $row['total_wt'] = Tools::ps_round($row['price_with_reduction'], _PS_PRICE_COMPUTE_PRECISION_) * (int) $row['cart_quantity'];
                    break;
            }
            $row['price_wt'] = $row['price_with_reduction'];
            $row['description_short'] = Tools::nl2br($row['description_short']);
            // check if a image associated with the attribute exists
            if ($row['id_product_attribute']) {
                $row2 = Image::getBestImageAttribute($row['id_shop'], $this->id_lang, $row['id_product'], $row['id_product_attribute']);
                if ($row2) {
                    $row = array_merge($row, $row2);
                }
            }
            $row['reduction_applies'] = $specific_price_output && (double) $specific_price_output['reduction'];
            $row['quantity_discount_applies'] = $specific_price_output && $row['cart_quantity'] >= (int) $specific_price_output['from_quantity'];
            $row['id_image'] = Product::defineProductImage($row, $this->id_lang);
            $row['allow_oosp'] = Product::isAvailableWhenOutOfStock($row['out_of_stock']);
            $row['features'] = Product::getFeaturesStatic((int) $row['id_product']);
            if (array_key_exists($row['id_product_attribute'] . '-' . $this->id_lang, self::$_attributesLists)) {
                $row = array_merge($row, self::$_attributesLists[$row['id_product_attribute'] . '-' . $this->id_lang]);
            }
            $row = Product::getTaxesInformations($row, $cart_shop_context);
            $this->_products[] = $row;
        }
        return $this->_products;
    }
Ejemplo n.º 11
0
    /**
     * Return cart products
     *
     * @result array Products
     * @param bool $refresh
     * @param bool $product_id
     * @param null $country_id
     * @return array|null
     */
    public function getProducts($refresh = false, $product_id = false, $country_id = null)
    {
        if (!$this->cart_id) {
            return array();
        }
        // Product cache must be strictly compared to NULL, or else an empty cart will add dozens of queries
        if ($this->_products !== null && !$refresh) {
            // Return product row with specified ID if it exists
            if (is_int($product_id)) {
                foreach ($this->_products as $product) {
                    if ($product->product_id == $product_id) {
                        return array($product);
                    }
                }
                return array();
            }
            return $this->_products;
        }
        // Build query
        $db = JFactory::getDBO();
        $select = "";
        $left_join = "";
        if (JeproshopCustomization::isFeaturePublished()) {
            $select .= ", customization." . $db->quoteName('customization_id') . ", customization." . $db->quoteName('quantity') . " AS customization_quantity";
            $left_join .= " LEFT JOIN " . $db->quoteName('#__jeproshop_customization') . " AS customization ON product." . $db->quoteName('product_id') . " = ";
            $left_join .= "customization." . $db->quoteName('product_id') . " AND cart_product." . $db->quoteName('product_attribute_id') . " = customization.";
            $left_join .= $db->quoteName('product_attribute_id') . " AND customization." . $db->quoteNam('cart_id') . " = " . (int) $this->cart_id;
        } else {
            $select .= ", NULL AS customization_quantity, NULL AS customization_id";
        }
        if (JeproshopCombinationModelCombination::isFeaturePublished()) {
            $select .= ", product_attribute_shop." . $db->quoteName('price') . " AS price_attribute, product_attribute_shop." . $db->quoteName('ecotax') . " AS ecotax_attr, IF (IFNULL(product_attribute." . $db->quoteName('reference');
            $select .= ", '') = '', product." . $db->quoteName('reference') . ", product_attribute." . $db->quoteName('reference') . ") AS reference, (product." . $db->quoteName('weight') . "+ product_attribute." . $db->quoteName('weight');
            $select .= ") weight_attribute, IF (IFNULL(product_attribute." . $db->quoteName('ean13') . ", '') = '', product." . $db->quoteName('ean13') . ", product_attribute." . $db->quoteName('ean13') . ") AS ean13, IF (IFNULL(product_attribute.";
            $select .= $db->quoteName('upc') . ", '') = '', product." . $db->quoteName('upc') . ", product_attribute." . $db->quoteName('upc') . ") AS upc, product_attribute_image." . $db->quoteName('image_id') . " AS product_attribute_image_image_id,";
            $select .= " image_lang." . $db->quoteName('legend') . " AS product_attribute_image_legend, IFNULL(product_attribute_shop." . $db->quoteName('minimal_quantity') . ", product_shop." . $db->quoteName('minimal_quantity') . ")) as minimal_quantity ";
            $left_join .= $db->quoteName('#__jeproshop_product_attribute') . " AS product_attribute ON product_attribute." . $db->quoteName('product_attribute_id') . " = cart_product." . $db->quoteName('product_attribute_id') . " LEFT JOIN ";
            $left_join .= $db->quoteName('#__jeproshop_product_attribute_shop') . " AS product_attribute_shop ON (product_attribute_shop." . $db->quoteName('shop_id') . " = cart_product." . $db->quoteName('shop_id') . " AND product_attribute_shop.";
            $left_join .= $db->quoteName('product_attribute_id') . " = product_attribute." . $db->quoteName('product_attribute_id') . ") LEFT JOIN " . $db->quoteName('#__jeproshop_product_attribute_image') . " AS product_attribute_image ON product_attribute_image.";
            $left_join .= $db->quoteName('product_attribute_id') . " = product_attribute." . $db->quoteName('product_attribute_id') . " LEFT JOIN " . $db->quoteName('#__jeproshop_image_lang') . " AS image_lang ON image_lang." . $db->quoteName('image_id');
            $left_join .= " = product_attribute_image." . $db->quoteName('image_id') . " AND image_lang." . $db->quoteName('lang_id') . " = " . (int) $this->lang_id;
        } else {
            $select .= ", product." . $db->quoteName('reference') . " AS reference, product." . $db->quoteName('ean13') . ", product." . $db->quoteName('upc') . " AS upc, product_shop." . $db->quoteName('minimal_quantity') . " AS minimal_quantity";
        }
        $query = "SELECT cart_product." . $db->quoteName('product_attribute_id') . ", cart_product." . $db->quoteName('product_id') . ", cart_product." . $db->quoteName('quantity');
        $query .= " AS cart_quantity, cart_product.shop_id, product_lang." . $db->quoteName('name') . ", product." . $db->quoteName('is_virtual') . ", product_lang." . $db->quoteName('short_description');
        $query .= ", product_lang." . $db->quoteName('available_now') . ", product_lang." . $db->quoteName('available_later') . ", product_shop." . $db->quoteName('default_category_id') . ", product.";
        $query .= $db->quoteName('supplier_id') . ", product." . $db->quoteName('manufacturer_id') . ", product_shop." . $db->quoteName('on_sale') . ", product_shop." . $db->quoteName('ecotax');
        $query .= ", product_shop." . $db->quoteName('additional_shipping_cost') . ", product_shop." . $db->quoteName('available_for_order') . ", product_shop." . $db->quoteName('price') . ", product_shop.";
        $query .= $db->quoteName('published') . ", product_shop." . $db->quoteName('unity') . ", product_shop." . $db->quoteName('unit_price_ratio') . ", stock." . $db->quoteName('quantity');
        $query .= " AS quantity_available, product." . $db->quoteName('width') . ", product." . $db->quoteName('height') . ", product." . $db->quoteName('depth') . ", stock." . $db->quoteName('out_of_stock');
        $query .= ", product." . $db->quoteName('weight') . ", product." . $db->quoteName('date_add') . ", product." . $db->quoteName('date_upd') . ", IFNULL(stock.quantity, 0) as quantity, product_lang.";
        $query .= $db->quoteName('link_rewrite') . ", category_lang." . $db->quoteName('link_rewrite') . " AS category, CONCAT(LPAD(cart_product." . $db->quoteName('product_id') . ", 10, 0), LPAD(IFNULL(cart_product.";
        $query .= $db->quoteName('product_attribute_id') . ", 0), 10, 0), IFNULL(cart_product." . $db->quoteName('address_delivery_id') . ", 0)) AS unique_id, cart_product.address_delivery_id, product_shop.";
        $query .= $db->quoteName('wholesale_price') . ", product_shop.advanced_stock_management, product_suppliers.product_supplier_reference supplier_reference, IFNULL(specific_price." . $db->quoteName('reduction_type');
        $query .= ", 0) AS reduction_type " . $select . " FROM " . $db->quoteName('#__jeproshop_cart_product') . " AS cart_product LEFT JOIN " . $db->quoteName('#__jeproshop_product') . " AS product ON product." . $db->quoteName('_product_id');
        $query .= " = cart_product." . $db->quoteName('product_id`') . " LEFT JOIN " . $db->quoteName('#__jeproshop_product_shop') . " AS product_shop ON (product_shop." . $db->quoteName('shop_id') . " = cart_product.";
        $query .= $db->quoteName('shop_id') . " AND product_shop." . $db->quoteName('product_id') . " = product." . $db->quoteName('product_id') . ") LEFT JOIN " . $db->quoteName('#__jeproshop_product_lang');
        $query .= " AS product_lang ON (product." . $db->quoteName('product_id') . " = product_lang." . $db->quoteName('product_id') . " AND product_lang." . $db->quoteName('lang_id') . " = " . (int) $this->lang_id;
        $query .= JeproshopShopModelShop::addSqlRestrictionOnLang('product_lang', 'cart_product.' . $db->quoteName('shop_id')) . "é LEFT JOIN " . $db->quoteName('#__jeproshop_category_lang') . " AS category_lang";
        $query .= " product_shop." . $db->quoteName('default_category_id') . " = category_lang." . $db->quoteName('category_id') . " AND category_lang." . $db->quoteName('lang_id') . " = " . (int) $this->lang_id;
        $query .= JeproshopShopModelShop::addSqlRestrictionOnLang('category_lang', 'cart_product.' . $db->quoteName('shop_id')) . "LEFT JOIN " . $db->quoteName('#__jeproshop_product_supplier') . " AS product_supplier ";
        $query .= " ON (product_supplier." . $db->quoteName('product_id') . " = cart_product." . $db->quoteName('product_id') . " AND product_supplier." . $db->quoteName('product_attribute_id') . " = cart_product.";
        $query .= $db->quoteName('product_attribute_id') . " AND product_supplier." . $db->quoteName('supplier_id') . " = product." . $db->quoteName('supplier_id') . ") LEFT JOIN " . $db->quoteName('#__jeproshop_specific_price');
        $query .= " AS specific_price ON specific_price." . $db->quoteName('product_id') . " = cart_product." . $db->quoteName('product_id') . " JOIN " . JeproshopProductModelProduct::sqlStock('cart_product') . $left_join . " WHERE ";
        $query .= " cart_product." . $db->quopteName('cart_id') . "= " . (int) $this->cart_id . ($product_id ? " AND cart_product." . $db->quoteName('product_id') . " = " . (int) $product_id : "") . " AND product." . $db->quoteName('product_id');
        $query .= " IS NOT NULL GROUP BY unique_id ORDER BY cart_product." . $db->quoteName('date_add') . ", product." . $db->quoteName('product_id') . ", cart_product." . $db->quoteName('product_attribute_id') . " ASC";
        $db->setQuery($query);
        $result = $db->loadObjectList();
        // Reset the cache before the following return, or else an empty cart will add dozens of queries
        $products_ids = array();
        $product_attribute_ids = array();
        if ($result) {
            foreach ($result as $row) {
                $products_ids[] = $row->product_id;
                $product_attribute_ids[] = $row->product_attribute_id;
            }
        }
        // Thus you can avoid one query per product, because there will be only one query for all the products of the cart
        JeproshopProductModelProduct::cacheProductsFeatures($products_ids);
        JeproshopCartModelCart::cacheSomeAttributesLists($product_attribute_ids, $this->lang_id);
        $this->_products = array();
        if (empty($result)) {
            return array();
        }
        $cart_shop_context = JeproshopContext::getContext()->cloneContext();
        foreach ($result as &$row) {
            if (isset($row->ecotax_attr) && $row->ecotax_attr > 0) {
                $row->ecotax = (double) $row->ecotax_attr;
            }
            $row->stock_quantity = (int) $row->quantity;
            // for compatibility with 1.2 themes
            $row->quantity = (int) $row->cart_quantity;
            if (isset($row->product_attribute_id) && (int) $row->product_attribute_id && isset($row->weight_attribute)) {
                $row->weight = (double) $row->weight_attribute;
            }
            if (JeproshopSettingModelSeting::getValue('PS_TAX_ADDRESS_TYPE') == 'address_invoice_id') {
                $address_id = (int) $this->address_invoice_id;
            } else {
                $address_id = (int) $row->address_delivery_id;
            }
            if (!Address::addressExists($address_id)) {
                $address_id = null;
            }
            if ($cart_shop_context->shop->shop_id != $row['id_shop']) {
                $cart_shop_context->shop = new Shop((int) $row['id_shop']);
            }
            $specific_price_output = null;
            $null = null;
            if ($this->_taxCalculationMethod == PS_TAX_EXC) {
                $row->price = JeproshopProductModelProduct::getStaticPrice((int) $row->product_id, false, isset($row->product_attribute_id) ? (int) $row->product_attribute_id : null, 2, null, false, true, (int) $row->cart_quantity, false, (int) $this->customer_id ? (int) $this->customer_id : null, (int) $this->cart_id, (int) $address_id ? (int) $address_id : null, $specific_price_output, true, true, $cart_shop_context);
                // Here taxes are computed only once the quantity has been applied to the product price
                $row->price_wt = JeproshopProductModelProduct::getStaticPrice((int) $row->product_id, true, isset($row->product_attribute_id) ? (int) $row->product_attribute_id : null, 2, null, false, true, (int) $row->cart_quantity, false, (int) $this->customer_id ? (int) $this->customer_id : null, (int) $this->cart_id, (int) $address_id ? (int) $address_id : null, $null, true, true, $cart_shop_context);
                $tax_rate = JeproshopTaxModelTax::getProductTaxRate((int) $row->product_id, (int) $address_id);
                $row->total_wt = JeproshopValidator::roundPrice($row->price * (double) $row->cart_quantity * (1 + (double) $tax_rate / 100), 2);
                $row->total = $row->price * (int) $row->cart_quantity;
            } else {
                $row->price = JreproshopProductModelProduct::getStaticPrice((int) $row->product_id, false, (int) $row->product_attribute_id, 2, null, false, true, $row->cart_quantity, false, (int) $this->customer_id ? (int) $this->customer_id : null, (int) $this->cart_id, (int) $address_id ? (int) $address_id : null, $specific_price_output, true, true, $cart_shop_context);
                $row->price_wt = JeproshopProductModelProduct::getStaticPrice((int) $row['id_product'], true, (int) $row['id_product_attribute'], 2, null, false, true, $row['cart_quantity'], false, (int) $this->customer_id ? (int) $this->customer_id : null, (int) $this->cart_id, (int) $address_id ? (int) $address_id : null, $null, true, true, $cart_shop_context);
                // In case when you use QuantityDiscount, getPriceStatic() can be return more of 2 decimals
                $row->price_wt = JeproshopValidator::roundPrice($row->price_wt, 2);
                $row->total_wt = $row->price_wt * (int) $row->cart_quantity;
                $row->total = JeproshopValidator::roundPrice($row['price'] * (int) $row['cart_quantity'], 2);
                $row->description_short = Tools::nl2br($row['description_short']);
            }
            if (!isset($row['pai_id_image']) || $row['pai_id_image'] == 0) {
                $cache_id = 'Cart::getProducts_' . '-pai_id_image-' . (int) $row['id_product'] . '-' . (int) $this->lang_id . '-' . (int) $row['id_shop'];
                if (!Cache::isStored($cache_id)) {
                    $row2 = Db::getInstance()->getRow('
						SELECT image_shop.`id_image` id_image, il.`legend`
						FROM `' . _DB_PREFIX_ . 'image` i
						JOIN `' . _DB_PREFIX_ . 'image_shop` image_shop ON (i.id_image = image_shop.id_image AND image_shop.cover=1 AND image_shop.id_shop=' . (int) $row['id_shop'] . ')
						LEFT JOIN `' . _DB_PREFIX_ . 'image_lang` il ON (image_shop.`id_image` = il.`id_image` AND il.`id_lang` = ' . (int) $this->lang_id . ')
						WHERE i.`id_product` = ' . (int) $row['id_product'] . ' AND image_shop.`cover` = 1');
                    Cache::store($cache_id, $row2);
                }
                $row2 = Cache::retrieve($cache_id);
                if (!$row2) {
                    $row2 = array('id_image' => false, 'legend' => false);
                } else {
                    $row = array_merge($row, $row2);
                }
            } else {
                $row->image_id = $row['pai_id_image'];
                $row->legend = $row['pai_legend'];
            }
            $row->reduction_applies = $specific_price_output && (double) $specific_price_output->reduction;
            $row->quantity_discount_applies = $specific_price_output && $row->cart_quantity >= (int) $specific_price_output->from_quantity;
            $row->image_id = JeproshopProductModelProduct::defineProductImage($row, $this->lang_id);
            $row->allow_oosp = JeproshopProductModelProduct::isAvailableWhenOutOfStock($row->out_of_stock);
            $row->features = JeproshopProductModelProduct::getStaticFeatures((int) $row->product_id);
            if (array_key_exists($row->product_attribute_id . '_' . $this->lang_id, self::$_attributesLists)) {
                $row = array_merge($row, self::$_attributesLists[$row->product_attribute_id . '_' . $this->lang_id]);
            }
            $row = JeproshopProductModelProduct::getTaxesInformations($row, $cart_shop_context);
            $this->_products[] = $row;
        }
        return $this->_products;
    }
Ejemplo n.º 12
0
 /**
  * Replace with your own cart here to try out
  * different promotions, tax, shipping, etc. 
  * 
  * @param merchantID
  * @param awsAccessKeyID
  */
 private function getCartXML($merchantID, $awsAccessKeyID)
 {
     $context = Context::getContext();
     if ($context->customer->isLogged()) {
         $cart_id = (int) $context->cart->id;
         $client_id = $context->customer->id . '/' . $cart_id;
     } else {
         $cart_id = (int) $context->cart->id;
         $client_id = '0/' . $cart_id;
     }
     $site = _PS_BASE_URL_ . __PS_BASE_URI__;
     $ReturnUrl = $site . 'module/pwapresta/pwaorder?action=pwa_order&amp;CartId=' . $cart_id;
     $doc = new DOMDocument('1.0');
     $doc->formatOutput = true;
     $root = $doc->createElement('Order');
     $attribute = $doc->createAttribute('xmlns');
     $attribute->value = 'http://payments.amazon.com/checkout/2009-05-15/';
     $root_attr = $root->appendChild($attribute);
     $root = $doc->appendChild($root);
     $ClientRequestId = $doc->createElement('ClientRequestId', $cart_id);
     $ClientRequestId = $root->appendChild($ClientRequestId);
     $Cart = $doc->createElement('Cart');
     $Cart = $root->appendChild($Cart);
     $Items = $doc->createElement('Items');
     $Items = $Cart->appendChild($Items);
     $CartPromotionId = $doc->createElement('CartPromotionId', 'Total_Discount');
     $CartPromotionId = $Cart->appendChild($CartPromotionId);
     $Promotions = $doc->createElement('Promotions');
     $Promotions = $root->appendChild($Promotions);
     $cart_rules = $context->cart->getCartRules();
     $total_product_with_tax = $context->cart->getOrderTotal(true, Cart::ONLY_PRODUCTS);
     $total_discount_at_amazon_with_tax = $context->cart->getOrderTotal(true, Cart::ONLY_DISCOUNTS);
     $total_wrapping_with_tax = $context->cart->getOrderTotal(true, Cart::ONLY_WRAPPING);
     global $currency;
     $currency_iso_code = $currency->iso_code;
     if ($total_discount_at_amazon_with_tax) {
         $Promotion = $doc->createElement('Promotion');
         $Promotion = $Promotions->appendChild($Promotion);
         $Promotion_pro_id = $doc->createElement('PromotionId', 'Total_Discount');
         $Promotion_pro_id = $Promotion->appendChild($Promotion_pro_id);
         $Promotion_pro_desc = $doc->createElement('Description', 'Total discount at cart level');
         $Promotion_pro_desc = $Promotion->appendChild($Promotion_pro_desc);
         $Promotion_pro_benf = $doc->createElement('Benefit');
         $Promotion_pro_benf = $Promotion->appendChild($Promotion_pro_benf);
         $Promotion_pro_benf_fad = $doc->createElement('FixedAmountDiscount');
         $Promotion_pro_benf_fad = $Promotion_pro_benf->appendChild($Promotion_pro_benf_fad);
         $Promotion_pro_benf_fad_amount = $doc->createElement('Amount', $total_discount_at_amazon_with_tax);
         $Promotion_pro_benf_fad_amount = $Promotion_pro_benf_fad->appendChild($Promotion_pro_benf_fad_amount);
         $Promotion_pro_benf_fad_currency = $doc->createElement('CurrencyCode', $currency_iso_code);
         $Promotion_pro_benf_fad_currency = $Promotion_pro_benf_fad->appendChild($Promotion_pro_benf_fad_currency);
     }
     $products = $context->cart->getProducts();
     foreach ($products as $key => $product) {
         $product_id = $product['id_product'];
         $sku = $product['reference'];
         $sku = substr($sku, 0, 40);
         $sku = $this->replace_char($sku);
         $sku = htmlentities($sku, ENT_QUOTES, 'UTF-8');
         $title = $product['name'];
         if (!$title) {
             $title = 'Title';
         }
         $title = substr($title, 0, 80);
         $title = $this->replace_char($title);
         $title = htmlentities($title, ENT_QUOTES, 'UTF-8');
         $description = $product['description_short'];
         $description = substr($description, 0, 1900);
         $description = $this->replace_char($description);
         $description = htmlentities($description, ENT_QUOTES, 'UTF-8');
         $quantity = $product['quantity'];
         $weight = $product['weight'];
         $weight_unit = Configuration::get('PS_WEIGHT_UNIT');
         if ($weight_unit != 'kg' && $weight > 0) {
             $weight = $weight / 1000;
             $weight_unit = 'kg';
         }
         if (Configuration::get('PS_TAX_ADDRESS_TYPE') == 'id_address_invoice') {
             $address_id = (int) $this->id_address_invoice;
         } else {
             $address_id = (int) $product['id_address_delivery'];
         }
         // Get delivery address of the product from the cart
         if (!Address::addressExists($address_id)) {
             $address_id = null;
         }
         $virtual_context = Context::getContext()->cloneContext();
         $virtual_context->cart = $context->cart;
         $product_price = Product::getPriceStatic((int) $product['id_product'], true, (int) $product['id_product_attribute'], 2, null, false, true, $product['cart_quantity'], false, (int) $context->cart->id_customer ? (int) $context->cart->id_customer : null, (int) $context->cart->id, (int) $address_id ? (int) $address_id : null, $null, true, true, $virtual_context);
         $product_price_exl_tax = Product::getPriceStatic((int) $product['id_product'], false, (int) $product['id_product_attribute'], 2, null, false, true, $product['cart_quantity'], false, (int) $context->cart->id_customer ? (int) $context->cart->id_customer : null, (int) $context->cart->id, (int) $address_id ? (int) $address_id : null, $null, true, true, $virtual_context);
         $Item = $doc->createElement('Item');
         $Item = $Items->appendChild($Item);
         $SKU = $doc->createElement('SKU', $sku);
         $SKU = $Item->appendChild($SKU);
         $MerchantId = $doc->createElement('MerchantId', $merchantID);
         $MerchantId = $Item->appendChild($MerchantId);
         $Title = $doc->createElement('Title', $title);
         $Title = $Item->appendChild($Title);
         $Description = $doc->createElement('Description', $description);
         $Description = $Item->appendChild($Description);
         $Price = $doc->createElement('Price');
         $Price = $Item->appendChild($Price);
         $Amount = $doc->createElement('Amount', $product_price);
         $Amount = $Price->appendChild($Amount);
         $CurrencyCode = $doc->createElement('CurrencyCode', $currency_iso_code);
         $CurrencyCode = $Price->appendChild($CurrencyCode);
         $Quantity = $doc->createElement('Quantity', $quantity);
         $Quantity = $Item->appendChild($Quantity);
         if ($weight) {
             $Weight = $doc->createElement('Weight');
             $Weight = $Item->appendChild($Weight);
             $Amount_wt = $doc->createElement('Amount', $weight);
             $Amount_wt = $Weight->appendChild($Amount_wt);
             $Wt_unit = $doc->createElement('Unit', $weight_unit);
             $Wt_unit = $Weight->appendChild($Wt_unit);
         }
         /*
               if($total_discount_at_amazon_with_tax)
               {  
                 $discount_code = "";
                 $product_discount_with_tax = 0;
                 foreach ($cart_rules as $key => $cart_rule) {
                   if($cart_rule['reduction_product'] == $product_id)
                   {
                     $discount_code .= $cart_rule['code'].',';
                     $product_discount_with_tax += $cart_rule['value_real'];
                   }
                   else if($cart_rule['reduction_product'] == 0){
                     $discount_code .= $cart_rule['code'].',';
                     $product_discount_with_tax += (($product_price * $quantity) / $total_product_with_tax) * $cart_rule['value_real'];
                   }
                 }
                 $product_discount_with_tax = round($product_discount_with_tax,2);
                 $this->create_promotion_xml_part($product_id,$discount_code,$product_discount_with_tax,$doc,$Item,$root,$Promotions,$currency_iso_code);
               }*/
         $variant_array = isset($product['attributes']) ? explode(',', $product['attributes']) : array();
         $ItemCustomData = $doc->createElement('ItemCustomData');
         $ItemCustomData = $Item->appendChild($ItemCustomData);
         $Item_product_id = $doc->createElement('Item_product_id', $product_id);
         $Item_product_id = $ItemCustomData->appendChild($Item_product_id);
         $Item_attr_product_id = $doc->createElement('Item_attr_product_id', $product['id_product_attribute']);
         $Item_attr_product_id = $ItemCustomData->appendChild($Item_attr_product_id);
         $Item_price_excl_tax = $doc->createElement('Item_price_excl_tax', $product_price_exl_tax);
         $Item_price_excl_tax = $ItemCustomData->appendChild($Item_price_excl_tax);
         foreach ($variant_array as $key => $value) {
             $value = explode(':', $value);
             $Item_attribute = $doc->createElement('Item_attribute');
             $Item_attribute = $ItemCustomData->appendChild($Item_attribute);
             $attr_name = substr($value[0], 0, 40);
             $attr_name = $this->replace_char($attr_name);
             $attr_name = htmlentities($attr_name, ENT_QUOTES, 'UTF-8');
             $Attribute_name = $doc->createElement('Attribute_name', $attr_name);
             $Attribute_name = $Item_attribute->appendChild($Attribute_name);
             $attr_val = substr($value[1], 0, 40);
             $attr_val = $this->replace_char($attr_val);
             $attr_val = htmlentities($attr_val, ENT_QUOTES, 'UTF-8');
             $Attribute_val = $doc->createElement('Attribute_val', $attr_val);
             $Attribute_val = $Item_attribute->appendChild($Attribute_val);
         }
     }
     if ($context->cart->gift) {
         $Item = $doc->createElement('Item');
         $Item = $Items->appendChild($Item);
         $SKU = $doc->createElement('SKU', 'wrapping_fee');
         $SKU = $Item->appendChild($SKU);
         $MerchantId = $doc->createElement('MerchantId', $merchantID);
         $MerchantId = $Item->appendChild($MerchantId);
         $Title = $doc->createElement('Title', 'Wrapping-fee');
         $Title = $Item->appendChild($Title);
         $Description = $doc->createElement('Description', $context->cart->gift_message);
         $Description = $Item->appendChild($Description);
         $Price = $doc->createElement('Price');
         $Price = $Item->appendChild($Price);
         $Amount = $doc->createElement('Amount', $total_wrapping_with_tax);
         $Amount = $Price->appendChild($Amount);
         $CurrencyCode = $doc->createElement('CurrencyCode', $currency_iso_code);
         $CurrencyCode = $Price->appendChild($CurrencyCode);
         $Quantity = $doc->createElement('Quantity', 1);
         $Quantity = $Item->appendChild($Quantity);
     }
     $ReturnUrl = $doc->createElement('ReturnUrl', $ReturnUrl);
     $ReturnUrl = $root->appendChild($ReturnUrl);
     return $doc->saveXML();
 }
Ejemplo n.º 13
0
 public function getOrderTotal($with_taxes = true, $type = Cart::BOTH, $products = null, $id_carrier = null, $use_cache = true)
 {
     static $address = null;
     if (!$this->id) {
         return 0;
     }
     $type = (int) $type;
     $array_type = array(Cart::ONLY_PRODUCTS, Cart::ONLY_DISCOUNTS, Cart::BOTH, Cart::BOTH_WITHOUT_SHIPPING, Cart::ONLY_SHIPPING, Cart::ONLY_WRAPPING, Cart::ONLY_PRODUCTS_WITHOUT_SHIPPING, Cart::ONLY_PHYSICAL_PRODUCTS_WITHOUT_SHIPPING);
     // Define virtual context to prevent case where the cart is not the in the global context
     $virtual_context = Context::getContext()->cloneContext();
     $virtual_context->cart = $this;
     if (!in_array($type, $array_type)) {
         die(Tools::displayError());
     }
     $with_shipping = in_array($type, array(Cart::BOTH, Cart::ONLY_SHIPPING));
     // if cart rules are not used
     if ($type == Cart::ONLY_DISCOUNTS && !CartRule::isFeatureActive()) {
         return 0;
     }
     // no shipping cost if is a cart with only virtuals products
     $virtual = $this->isVirtualCart();
     if ($virtual && $type == Cart::ONLY_SHIPPING) {
         return 0;
     }
     if ($virtual && $type == Cart::BOTH) {
         $type = Cart::BOTH_WITHOUT_SHIPPING;
     }
     if ($with_shipping || $type == Cart::ONLY_DISCOUNTS) {
         if (is_null($products) && is_null($id_carrier)) {
             $shipping_fees = $this->getTotalShippingCost(null, (bool) $with_taxes);
         } else {
             $shipping_fees = $this->getPackageShippingCost($id_carrier, (bool) $with_taxes, null, $products);
         }
     } else {
         $shipping_fees = 0;
     }
     if ($type == Cart::ONLY_SHIPPING) {
         return $shipping_fees;
     }
     if ($type == Cart::ONLY_PRODUCTS_WITHOUT_SHIPPING) {
         $type = Cart::ONLY_PRODUCTS;
     }
     $param_product = true;
     if (is_null($products)) {
         $param_product = false;
         $products = $this->getProducts();
     }
     if ($type == Cart::ONLY_PHYSICAL_PRODUCTS_WITHOUT_SHIPPING) {
         foreach ($products as $key => $product) {
             if ($product['is_virtual']) {
                 unset($products[$key]);
             }
         }
         $type = Cart::ONLY_PRODUCTS;
     }
     $order_total = 0;
     if (Tax::excludeTaxeOption()) {
         $with_taxes = false;
     }
     $products_total = array();
     $ecotax_total = 0;
     foreach ($products as $product) {
         if ($virtual_context->shop->id != $product['id_shop']) {
             $virtual_context->shop = new Shop((int) $product['id_shop']);
         }
         if (Configuration::get('PS_TAX_ADDRESS_TYPE') == 'id_address_invoice') {
             $id_address = (int) $this->id_address_invoice;
         } else {
             $id_address = (int) $product['id_address_delivery'];
         }
         // Get delivery address of the product from the cart
         if (!Address::addressExists($id_address)) {
             $id_address = null;
         }
         $null = null;
         $price = Product::getPriceStatic((int) $product['id_product'], false, (int) $product['id_product_attribute'], 6, null, false, true, array($product['cart_quantity'], $product['cart_quantity_fractional']), false, (int) $this->id_customer ? (int) $this->id_customer : null, (int) $this->id, $id_address, $null, false, true, $virtual_context);
         if (Configuration::get('PS_USE_ECOTAX')) {
             $ecotax = $product['ecotax'];
             if (isset($product['attribute_ecotax']) && $product['attribute_ecotax'] > 0) {
                 $ecotax = $product['attribute_ecotax'];
             }
         } else {
             $ecotax = 0;
         }
         $address = Address::initialize($id_address, true);
         if ($with_taxes) {
             $id_tax_rules_group = Product::getIdTaxRulesGroupByIdProduct((int) $product['id_product'], $virtual_context);
             $tax_calculator = TaxManagerFactory::getManager($address, $id_tax_rules_group)->getTaxCalculator();
             if ($ecotax) {
                 $ecotax_tax_calculator = TaxManagerFactory::getManager($address, (int) Configuration::get('PS_ECOTAX_TAX_RULES_GROUP_ID'))->getTaxCalculator();
             }
         } else {
             $id_tax_rules_group = 0;
         }
         if (in_array(Configuration::get('PS_ROUND_TYPE'), array(Order::ROUND_ITEM, Order::ROUND_LINE))) {
             if (!isset($products_total[$id_tax_rules_group])) {
                 $products_total[$id_tax_rules_group] = 0;
             }
         } else {
             if (!isset($products_total[$id_tax_rules_group . '_' . $id_address])) {
                 $products_total[$id_tax_rules_group . '_' . $id_address] = 0;
             }
         }
         switch (Configuration::get('PS_ROUND_TYPE')) {
             case Order::ROUND_TOTAL:
                 $products_total[$id_tax_rules_group . '_' . $id_address] += PP::calcPrice($price, $product['cart_quantity'], $product['cart_quantity_fractional'], (int) $product['id_product'], false);
                 $ppropertiessmartprice_hook3 = null;
                 if ($ecotax) {
                     $ecotax_total += PP::calcPrice($ecotax, $product['cart_quantity'], $product['cart_quantity_fractional'], null, false);
                 }
                 break;
             case Order::ROUND_LINE:
                 $product_price = PP::calcPrice($price, $product['cart_quantity'], $product['cart_quantity_fractional'], (int) $product['id_product'], false);
                 $ppropertiessmartprice_hook4 = null;
                 if ($with_taxes) {
                     $products_total[$id_tax_rules_group] += Tools::ps_round($product_price + $tax_calculator->getTaxesTotalAmount($product_price), _PS_PRICE_COMPUTE_PRECISION_);
                 } else {
                     $products_total[$id_tax_rules_group] += Tools::ps_round($product_price, _PS_PRICE_COMPUTE_PRECISION_);
                 }
                 if ($ecotax) {
                     $ecotax_price = PP::calcPrice($ecotax, $product['cart_quantity'], $product['cart_quantity_fractional'], null, false);
                     if ($with_taxes) {
                         $ecotax_total += Tools::ps_round($ecotax_price + $ecotax_tax_calculator->getTaxesTotalAmount($ecotax_price), _PS_PRICE_COMPUTE_PRECISION_);
                     } else {
                         $ecotax_total += Tools::ps_round($ecotax_price, _PS_PRICE_COMPUTE_PRECISION_);
                     }
                 }
                 break;
             case Order::ROUND_ITEM:
             default:
                 $product_price = $with_taxes ? $tax_calculator->addTaxes($price) : $price;
                 $products_total[$id_tax_rules_group] += PP::calcPrice($product_price, $product['cart_quantity'], $product['cart_quantity_fractional'], (int) $product['id_product'], Order::ROUND_ITEM);
                 $ppropertiessmartprice_hook5 = null;
                 if ($ecotax) {
                     $ecotax_price = $with_taxes ? $ecotax_tax_calculator->addTaxes($ecotax) : $ecotax;
                     $ecotax_total += PP::calcPrice($ecotax_price, $product['cart_quantity'], $product['cart_quantity_fractional'], null, Order::ROUND_ITEM);
                 }
                 break;
         }
     }
     foreach ($products_total as $key => $price) {
         if ($with_taxes && Configuration::get('PS_ROUND_TYPE') == Order::ROUND_TOTAL) {
             $tmp = explode('_', $key);
             $address = Address::initialize((int) $tmp[1], true);
             $tax_calculator = TaxManagerFactory::getManager($address, $tmp[0])->getTaxCalculator();
             $order_total += Tools::ps_round($price + $tax_calculator->getTaxesTotalAmount($price), _PS_PRICE_COMPUTE_PRECISION_);
         } else {
             $order_total += $price;
         }
     }
     if ($ecotax_total && $with_taxes && Configuration::get('PS_ROUND_TYPE') == Order::ROUND_TOTAL) {
         $ecotax_total = Tools::ps_round($ecotax_total, _PS_PRICE_COMPUTE_PRECISION_) + Tools::ps_round($ecotax_tax_calculator->getTaxesTotalAmount($ecotax_total), _PS_PRICE_COMPUTE_PRECISION_);
     }
     $order_total += $ecotax_total;
     $order_total_products = $order_total;
     if ($type == Cart::ONLY_DISCOUNTS) {
         $order_total = 0;
     }
     // Wrapping Fees
     $wrapping_fees = 0;
     if ($this->gift) {
         $wrapping_fees = Tools::convertPrice(Tools::ps_round($this->getGiftWrappingPrice($with_taxes), _PS_PRICE_COMPUTE_PRECISION_), Currency::getCurrencyInstance((int) $this->id_currency));
     }
     if ($type == Cart::ONLY_WRAPPING) {
         return $wrapping_fees;
     }
     $order_total_discount = 0;
     $order_shipping_discount = 0;
     if (!in_array($type, array(Cart::ONLY_SHIPPING, Cart::ONLY_PRODUCTS)) && CartRule::isFeatureActive()) {
         // First, retrieve the cart rules associated to this "getOrderTotal"
         if ($with_shipping || $type == Cart::ONLY_DISCOUNTS) {
             $cart_rules = $this->getCartRules(CartRule::FILTER_ACTION_ALL);
         } else {
             $cart_rules = $this->getCartRules(CartRule::FILTER_ACTION_REDUCTION);
             // Cart Rules array are merged manually in order to avoid doubles
             foreach ($this->getCartRules(CartRule::FILTER_ACTION_GIFT) as $tmp_cart_rule) {
                 $flag = false;
                 foreach ($cart_rules as $cart_rule) {
                     if ($tmp_cart_rule['id_cart_rule'] == $cart_rule['id_cart_rule']) {
                         $flag = true;
                     }
                 }
                 if (!$flag) {
                     $cart_rules[] = $tmp_cart_rule;
                 }
             }
         }
         $id_address_delivery = 0;
         if (isset($products[0])) {
             $id_address_delivery = is_null($products) ? $this->id_address_delivery : $products[0]['id_address_delivery'];
         }
         $package = array('id_carrier' => $id_carrier, 'id_address' => $id_address_delivery, 'products' => $products);
         // Then, calculate the contextual value for each one
         $flag = false;
         foreach ($cart_rules as $cart_rule) {
             // If the cart rule offers free shipping, add the shipping cost
             if (($with_shipping || $type == Cart::ONLY_DISCOUNTS) && $cart_rule['obj']->free_shipping && !$flag) {
                 $order_shipping_discount = (double) Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_SHIPPING, $param_product ? $package : null, $use_cache), _PS_PRICE_COMPUTE_PRECISION_);
                 $flag = true;
             }
             // If the cart rule is a free gift, then add the free gift value only if the gift is in this package
             if ((int) $cart_rule['obj']->gift_product) {
                 $in_order = false;
                 if (is_null($products)) {
                     $in_order = true;
                 } else {
                     foreach ($products as $product) {
                         if ($cart_rule['obj']->gift_product == $product['id_product'] && $cart_rule['obj']->gift_product_attribute == $product['id_product_attribute']) {
                             $in_order = true;
                         }
                     }
                 }
                 if ($in_order) {
                     $order_total_discount += $cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_GIFT, $package, $use_cache);
                 }
             }
             // If the cart rule offers a reduction, the amount is prorated (with the products in the package)
             if ($cart_rule['obj']->reduction_percent > 0 || $cart_rule['obj']->reduction_amount > 0) {
                 $order_total_discount += Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_REDUCTION, $package, $use_cache), _PS_PRICE_COMPUTE_PRECISION_);
             }
         }
         $order_total_discount = min(Tools::ps_round($order_total_discount, 2), (double) $order_total_products) + (double) $order_shipping_discount;
         $order_total -= $order_total_discount;
     }
     if ($type == Cart::BOTH) {
         $order_total += $shipping_fees + $wrapping_fees;
     }
     if ($order_total < 0 && $type != Cart::ONLY_DISCOUNTS) {
         return 0;
     }
     if ($type == Cart::ONLY_DISCOUNTS) {
         return $order_total_discount;
     }
     return Tools::ps_round((double) $order_total, _PS_PRICE_COMPUTE_PRECISION_);
 }
Ejemplo n.º 14
0
 /**
  * This function returns the total cart amount
  *
  * Possible values for $type:
  * Cart::ONLY_PRODUCTS
  * Cart::ONLY_DISCOUNTS
  * Cart::BOTH
  * Cart::BOTH_WITHOUT_SHIPPING
  * Cart::ONLY_SHIPPING
  * Cart::ONLY_WRAPPING
  * Cart::ONLY_PRODUCTS_WITHOUT_SHIPPING
  * Cart::ONLY_PHYSICAL_PRODUCTS_WITHOUT_SHIPPING
  *
  * @param boolean $withTaxes With or without taxes
  * @param integer $type Total type
  * @param boolean $use_cache Allow using cache of the method CartRule::getContextualValue
  * @return float Order total
  */
 public function getOrderTotal($with_taxes = true, $type = Cart::BOTH, $products = null, $id_carrier = null, $use_cache = true)
 {
     if (!$this->id) {
         return 0;
     }
     $type = (int) $type;
     $array_type = array(Cart::ONLY_PRODUCTS, Cart::ONLY_DISCOUNTS, Cart::BOTH, Cart::BOTH_WITHOUT_SHIPPING, Cart::ONLY_SHIPPING, Cart::ONLY_WRAPPING, Cart::ONLY_PRODUCTS_WITHOUT_SHIPPING, Cart::ONLY_PHYSICAL_PRODUCTS_WITHOUT_SHIPPING);
     // Define virtual context to prevent case where the cart is not the in the global context
     $virtual_context = Context::getContext()->cloneContext();
     $virtual_context->cart = $this;
     if (!in_array($type, $array_type)) {
         die(Tools::displayError());
     }
     $with_shipping = in_array($type, array(Cart::BOTH, Cart::ONLY_SHIPPING));
     // if cart rules are not used
     if ($type == Cart::ONLY_DISCOUNTS && !CartRule::isFeatureActive()) {
         return 0;
     }
     // no shipping cost if is a cart with only virtuals products
     $virtual = $this->isVirtualCart();
     if ($virtual && $type == Cart::ONLY_SHIPPING) {
         return 0;
     }
     if ($virtual && $type == Cart::BOTH) {
         $type = Cart::BOTH_WITHOUT_SHIPPING;
     }
     if ($with_shipping || $type == Cart::ONLY_DISCOUNTS) {
         if (is_null($products) && is_null($id_carrier)) {
             $shipping_fees = $this->getTotalShippingCost(null, (bool) $with_taxes);
         } else {
             $shipping_fees = $this->getPackageShippingCost($id_carrier, (bool) $with_taxes, null, $products);
         }
     } else {
         $shipping_fees = 0;
     }
     $shipping_fees = self::updateShippingCost($shipping_fees);
     if ($type == Cart::ONLY_SHIPPING) {
         $shipping_fees;
     }
     if ($type == Cart::ONLY_PRODUCTS_WITHOUT_SHIPPING) {
         $type = Cart::ONLY_PRODUCTS;
     }
     $param_product = true;
     if (is_null($products)) {
         $param_product = false;
         $products = $this->getProducts();
     }
     if ($type == Cart::ONLY_PHYSICAL_PRODUCTS_WITHOUT_SHIPPING) {
         foreach ($products as $key => $product) {
             if ($product['is_virtual']) {
                 unset($products[$key]);
             }
         }
         $type = Cart::ONLY_PRODUCTS;
     }
     $order_total = 0;
     if (Tax::excludeTaxeOption()) {
         $with_taxes = false;
     }
     foreach ($products as $product) {
         if ($virtual_context->shop->id != $product['id_shop']) {
             $virtual_context->shop = new Shop((int) $product['id_shop']);
         }
         if (Configuration::get('PS_TAX_ADDRESS_TYPE') == 'id_address_invoice') {
             $address_id = (int) $this->id_address_invoice;
         } else {
             $address_id = (int) $product['id_address_delivery'];
         }
         // Get delivery address of the product from the cart
         if (!Address::addressExists($address_id)) {
             $address_id = null;
         }
         if ($this->_taxCalculationMethod == PS_TAX_EXC) {
             // Here taxes are computed only once the quantity has been applied to the product price
             $price = Product::getPriceStatic((int) $product['id_product'], false, (int) $product['id_product_attribute'], 2, null, false, true, $product['cart_quantity'], false, (int) $this->id_customer ? (int) $this->id_customer : null, (int) $this->id, $address_id, $null, true, true, $virtual_context);
             $total_ecotax = $product['ecotax'] * (int) $product['cart_quantity'];
             $total_price = $price * (int) $product['cart_quantity'];
             if ($with_taxes) {
                 $product_tax_rate = (double) Tax::getProductTaxRate((int) $product['id_product'], (int) $address_id, $virtual_context);
                 $product_eco_tax_rate = Tax::getProductEcotaxRate((int) $address_id);
                 $total_price = ($total_price - $total_ecotax) * (1 + $product_tax_rate / 100);
                 $total_ecotax = $total_ecotax * (1 + $product_eco_tax_rate / 100);
                 $total_price = Tools::ps_round($total_price + $total_ecotax, 2);
             }
         } else {
             if ($with_taxes) {
                 $price = Product::getPriceStatic((int) $product['id_product'], true, (int) $product['id_product_attribute'], 2, null, false, true, $product['cart_quantity'], false, (int) $this->id_customer ? (int) $this->id_customer : null, (int) $this->id, (int) $address_id ? (int) $address_id : null, $null, true, true, $virtual_context);
             } else {
                 $price = Product::getPriceStatic((int) $product['id_product'], false, (int) $product['id_product_attribute'], 2, null, false, true, $product['cart_quantity'], false, (int) $this->id_customer ? (int) $this->id_customer : null, (int) $this->id, (int) $address_id ? (int) $address_id : null, $null, true, true, $virtual_context);
             }
             $total_price = Tools::ps_round($price * (int) $product['cart_quantity'], 2);
         }
         $order_total += $total_price;
     }
     $order_total_products = $order_total;
     if ($type == Cart::ONLY_DISCOUNTS) {
         $order_total = 0;
     }
     // Wrapping Fees
     $wrapping_fees = 0;
     if ($this->gift) {
         $wrapping_fees = Tools::convertPrice(Tools::ps_round($this->getGiftWrappingPrice($with_taxes), 2), Currency::getCurrencyInstance((int) $this->id_currency));
     }
     if ($type == Cart::ONLY_WRAPPING) {
         return $wrapping_fees;
     }
     $order_total_discount = 0;
     if (!in_array($type, array(Cart::ONLY_SHIPPING, Cart::ONLY_PRODUCTS)) && CartRule::isFeatureActive()) {
         // First, retrieve the cart rules associated to this "getOrderTotal"
         if ($with_shipping || $type == Cart::ONLY_DISCOUNTS) {
             $cart_rules = $this->getCartRules(CartRule::FILTER_ACTION_ALL);
         } else {
             $cart_rules = $this->getCartRules(CartRule::FILTER_ACTION_REDUCTION);
             // Cart Rules array are merged manually in order to avoid doubles
             foreach ($this->getCartRules(CartRule::FILTER_ACTION_GIFT) as $tmp_cart_rule) {
                 $flag = false;
                 foreach ($cart_rules as $cart_rule) {
                     if ($tmp_cart_rule['id_cart_rule'] == $cart_rule['id_cart_rule']) {
                         $flag = true;
                     }
                 }
                 if (!$flag) {
                     $cart_rules[] = $tmp_cart_rule;
                 }
             }
         }
         $id_address_delivery = 0;
         if (isset($products[0])) {
             $id_address_delivery = is_null($products) ? $this->id_address_delivery : $products[0]['id_address_delivery'];
         }
         $package = array('id_carrier' => $id_carrier, 'id_address' => $id_address_delivery, 'products' => $products);
         // Then, calculate the contextual value for each one
         foreach ($cart_rules as $cart_rule) {
             // If the cart rule offers free shipping, add the shipping cost
             if (($with_shipping || $type == Cart::ONLY_DISCOUNTS) && $cart_rule['obj']->free_shipping) {
                 $order_total_discount += Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_SHIPPING, $param_product ? $package : null, $use_cache), 2);
             }
             // If the cart rule is a free gift, then add the free gift value only if the gift is in this package
             if ((int) $cart_rule['obj']->gift_product) {
                 $in_order = false;
                 if (is_null($products)) {
                     $in_order = true;
                 } else {
                     foreach ($products as $product) {
                         if ($cart_rule['obj']->gift_product == $product['id_product'] && $cart_rule['obj']->gift_product_attribute == $product['id_product_attribute']) {
                             $in_order = true;
                         }
                     }
                 }
                 if ($in_order) {
                     $order_total_discount += $cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_GIFT, $package, $use_cache);
                 }
             }
             // If the cart rule offers a reduction, the amount is prorated (with the products in the package)
             if ($cart_rule['obj']->reduction_percent > 0 || $cart_rule['obj']->reduction_amount > 0) {
                 $order_total_discount += Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_REDUCTION, $package, $use_cache), 2);
             }
         }
         $order_total_discount = min(Tools::ps_round($order_total_discount, 2), $wrapping_fees + $order_total_products + $shipping_fees);
         $order_total -= $order_total_discount;
     }
     if ($type == Cart::BOTH) {
         $order_total += $shipping_fees + $wrapping_fees;
     }
     if ($order_total < 0 && $type != Cart::ONLY_DISCOUNTS) {
         return 0;
     }
     if ($type == Cart::ONLY_DISCOUNTS) {
         return $order_total_discount;
     }
     return Tools::ps_round((double) $order_total, 2);
 }