Example #1
0
 public function onAfterInitialise()
 {
     // If this is a local request and carta=update get the weight/costs and cartaupdate set the config
     if ($this->isLocal() && array_key_exists('cartaupdate', $_REQUEST)) {
         $this->updateWeightCosts();
     }
     // Set which cats allow carta registrada from the config form
     self::$bookCats = preg_split('/\\s*,\\s*/', $this->params->get("cartaCats"));
     // And the Carta registrada prices
     foreach (array(0, 50, 100, 150, 200, 250, 300, 350, 400, 450) as $d) {
         $e = $d ? $d : 20;
         self::$cartaPrices[$d] = str_replace(',', '.', $this->params->get("carta{$e}"));
         self::$cartaPricesMod[$d] = str_replace(',', '.', $this->params->get("cartam{$e}"));
     }
     // Install our extended shipping type if not already there
     // (should be done from onExtensionAfterInstall but can't get it to be called)
     // (or better, should be done from the xml with install/uninstall element, but couldn't get that to work either)
     $db = JFactory::getDbo();
     $tbl = '#__jshopping_shipping_ext_calc';
     $db->setQuery("SELECT 1 FROM `{$tbl}` WHERE `name`='Correios'");
     $row = $db->loadRow();
     if (!$row) {
         // Add the shipping type extension
         $query = "INSERT INTO `{$tbl}` " . "(`name`, `alias`, `description`, `params`, `shipping_method`, `published`, `ordering`) " . "VALUES( 'Correios', 'sm_correios', 'Correios', '', '', 1, 1 )";
         $db->setQuery($query);
         $db->query();
         // Add our freight cost cache table
         $tbl = '#__correios_cache';
         $query = "CREATE TABLE IF NOT EXISTS `{$tbl}` (\n\t\t\t\tid     INT UNSIGNED NOT NULL AUTO_INCREMENT,\n\t\t\t\tcep    INT UNSIGNED NOT NULL,\n\t\t\t\tweight INT UNSIGNED NOT NULL,\n\t\t\t\ttime   INT UNSIGNED NOT NULL,\n\t\t\t\tpac    DECIMAL(5,2) NOT NULL,\n\t\t\t\tsedex  DECIMAL(5,2) NOT NULL,\n\t\t\t\tPRIMARY KEY (id)\n\t\t\t)";
         $db->setQuery($query);
         $db->query();
         // Copy the sm_ligmincha_freight class into the proper place
         // (there's probably a proper way to do this from the xml file)
         $path = JPATH_ROOT . '/components/com_jshopping/shippings/sm_correios';
         $file = 'sm_correios.php';
         if (!is_dir($path)) {
             mkdir($path);
         }
         if (!is_link("{$path}/{$file}")) {
             symlink(__DIR__ . "/{$file}", "{$path}/{$file}");
         }
     }
 }
Example #2
0
 /**
  * This method is called for each of the shipping methods options in the checkout
  * (and also on subsequent pages for the chosen method)
  */
 function getPrices($cart, $params, $prices, &$shipping_ext_row, &$shipping_method_price)
 {
     $weight = $cart->getWeightProducts();
     $id = $shipping_method_price->shipping_method_id;
     $type = plgSystemCorreios::getShippingMethodName($id);
     // Redirect the page stright to payment methods if weight is zero
     if ($weight == 0) {
         static $done = false;
         if (!$done) {
             $done = true;
             $url = JSFactory::getModel('checkoutStep', 'jshop')->getCheckoutUrl('step4save');
             header("Location: {$url}?sh_pr_method_id={$id}");
         }
     }
     // Check if all products are in cats that allow carta registrada and that all are 500g or less
     plgSystemCorreios::$allbooks = true;
     foreach ($cart->products as $item) {
         if (!in_array($item['category_id'], plgSystemCorreios::$bookCats) || $item['weight'] > 0.5) {
             plgSystemCorreios::$allbooks = false;
         }
     }
     // If it's one of ours, calculate the price
     if ($type == 'PAC') {
         $prices['shipping'] = $this->getFreightPrice($weight, 1);
         $prices['package'] = 0;
     } elseif ($type == 'SEDEX') {
         $prices['shipping'] = $this->getFreightPrice($weight, 2);
         $prices['package'] = 0;
     } elseif (preg_match('/carta\\s*registrada/i', $type)) {
         $packages = plgSystemCorreios::makeCartaPackages($cart->products);
         $costs = preg_match('/módico/i', $type) ? plgSystemCorreios::$cartaPricesMod : plgSystemCorreios::$cartaPrices;
         $price = 0;
         foreach ($packages as $package) {
             $weight = $package[0];
             $i = 50 * (int) ($weight * 20);
             // price divisions are in multiples of 50 grams
             $price += $costs[$i];
         }
         $prices['shipping'] = $price;
         $prices['package'] = 0;
     }
     return $prices;
 }