г. Владимир, пр. Ленина 15а, офис 410

8 (499) 404-14-01
(телефон офиса)

8 (904) 259-97-29
(ватсапп, вайбер, телеграмм)

пн-пт: с 09.00-18.00
e-mail: info@pavelsonar.net

Недавно столкнулся с задачей печати почтовых бланков для джумшоппинг. Искал, гуглил, думал что лучше. В итоге потестировали с клиентом программу от https://pbrf.ru/ и http://beagler.ru . В итоге из-за определенных спецификаций нашей всеми любимой почты РФ пришлось искать другую альтернативу... и кстати более дешевую. Клиент посоветовал обратить внимание на "Купеческую книгу" (так как пользуется ей давно). Сама программа имеет возможность экспорта заказов через файл xml. Сам файл надо генерировать и сделано это было путем php и mySQL . Работа скрипта проста - запрос в базу - ответ в браузер. Сам скрипт прилагаю ниже:

 

  1. <?php
  2. $is_test = true;
  3. class export2XML {
  4.     private $_db;
  5.     private static $_dbhost = "localhost";
  6.     private static $_dbname = "Имя базы";
  7.     private static $_dbuser = "Имя пользователя";
  8.     private static $_dbpass = "Пароль";
  9.     private $_recordsLimit = 20;
  10.     private $prefix = 'префикс базы_';
  11.     private $countries_table = "jshopping_countries";
  12.     private $orders_table = "jshopping_orders";
  13.     private $ordered_products_table = "jshopping_order_item";
  14.     private $_is_test;
  15.     public function __construct($is_test = false) {
  16.         $this->_is_test = $is_test;
  17.         $this->_connect2DB();
  18.         $this->_showResult();
  19.     }
  20.     private function _connect2DB() {
  21.         $this->_setDBParams();
  22.         try {
  23.             $this->_db = new PDO('mysql:host=' . SELF::$_dbhost . ';dbname=' . SELF::$_dbname, SELF::$_dbuser, SELF::$_dbpass);
  24.         } catch (PDOException $e) {
  25.             print "Error!: " . $e->getMessage() . "<br/>";
  26.             die();
  27.         }
  28.     }
  29.     private function _setDBParams() {
  30.         $serverName = filter_input(INPUT_SERVER, 'SERVER_NAME');
  31.         if ($serverName == "vh73.spaceweb.ru") {
  32.             self::$_dbhost = "localhost";          
  33.             self::$_dbname = "kraspultru";
  34.             self::$_dbuser = "kraspultru";
  35.             self::$_dbpass = "29la7tw71";
  36.         }
  37.     }
  38.     private function _query($sql, $wheres = []) {
  39.         $sth = $this->_db->prepare($sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
  40.         $sth->execute($execParams);
  41.         return $sth->fetchAll(PDO::FETCH_CLASS);
  42.     }
  43.     private function _select($table, $wheres = []) {
  44.         $sql = 'SELECT * FROM ' . $this->prefix . $table;
  45.         $where = '';
  46.         $execParams = [];
  47.         foreach ($wheres as $key => $value) {
  48.             $where .= $key . ' = :' . $key . ' ';
  49.             $execParams[':' . $key] = $value;
  50.         }
  51.         if (!empty($where)) {
  52.             $sql .= ' WHERE ' . trim($where);
  53.         }            
  54.         $sth = $this->_db->prepare($sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
  55.         $sth->execute($execParams);
  56.         return $sth->fetchAll(PDO::FETCH_CLASS);
  57.     }
  58.     private function _getOrders() {
  59.         $sql =
  60.             'SELECT `ot`.*, `ct`.`name_ru-RU` as country_name FROM ' . $this->prefix . $this->orders_table . ' ot ' .
  61.                 'JOIN ' . $this->prefix . $this->countries_table . ' ct ON ct.country_id = ot.country ' .
  62.                 'WHERE `order_status` = 1';
  63.         if ($this->_is_test) {
  64.             $sql =
  65.             'SELECT * FROM (' .
  66.                 $sql . ' ORDER BY ot.order_id DESC LIMIT ' . $this->_recordsLimit .
  67.             ') sub ' .
  68.             'ORDER BY sub.order_id ASC';
  69.         }
  70.         $sth = $this->_db->prepare($sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
  71.         $sth->execute();
  72.         return $sth->fetchAll(PDO::FETCH_CLASS);
  73.      }
  74.     private function _escSpec($str) {
  75.         $rData = [
  76.             '&' => '&amp;',
  77.             '<' => '&lt;',
  78.             '>' => '&gt;',
  79.             '"' => '&quot;',
  80.             "'" =>'&apos;'
  81.         ];
  82.         return str_ireplace(array_keys($rData), array_values($rData), trim($str));
  83.     }
  84.     private function _showResult() {
  85.         header('Content-Type: text/xml');
  86.         $result =
  87.             '<?xml version="1.0" encoding="WINDOWS-1251"?>' . "\n" .
  88.                 '<xml_orders date="' . date("Y-m-d H:i") . '">' . "\n";
  89.         $orders = $this->_getOrders();
  90.         foreach ($orders as $row) {
  91.             $customer_name = trim($this->_escSpec($row->l_name). ' ' . $this->_escSpec($row->f_name). ' ' .$this->_escSpec($row->m_name));
  92.             $result .=
  93.                 '<order>' . "\n" .
  94.                     '<order_id>' . $this->_escSpec($row->order_id) . '</order_id>'. "\n" .
  95.                     '<order_date>' . $this->_escSpec($row->order_date) . '</order_date>' . "\n" .
  96.                     '<order_amount>' . $this->_escSpec($row->order_total) . '</order_amount>' . "\n" .
  97.                     '<customer_name>' . $customer_name . '</customer_name>' . "\n" .
  98.                     '<customer_email>' . $this->_escSpec($row->email) . '</customer_email>' . "\n" .
  99.                     '<customer_country>' . $this->_escSpec($row->country_name) . '</customer_country>' . "\n" .
  100.                     '<customer_region>' . $this->_escSpec($row->state) . '</customer_region>' ."\n" .
  101.                     '<customer_postalcode>' . $this->_escSpec($row->zip) . '</customer_postalcode>' . "\n" .
  102.                     '<customer_city>' . $this->_escSpec($row->city) . '</customer_city>'. "\n" .
  103. '<customer_address>ул. ' . $this->_escSpec($row->street) . ', д. ' . $this->_escSpec($row->home) . ', кв. ' . $this->_escSpec($row->apartment) . '</customer_address>' . "\n" .
  104.                     '<delivery_cost>' . $this->_escSpec($row->order_shipping) . '</delivery_cost>' . "\n" .
  105.                         '<products>' . "\n";
  106.             $products = $this->_select($this->ordered_products_table, ['order_id' => $this->_escSpec($row->order_id)]);
  107.             foreach ($products as $product) {
  108.                 $result .=
  109.                     '<product>' . "\n" .
  110.                         '<code>' . $this->_escSpec($product->order_item_id) . '</code>' . "\n" .
  111.                         '<name>' . $this->_escSpec($product->product_name) . '</name>' . "\n" .
  112.                         '<cost>' . $this->_escSpec($product->product_item_price) . '</cost>' . "\n" .
  113.                         '<count>' . $this->_escSpec($product->product_quantity) . '</count>' . "\n" .
  114.                     '</product>' . "\n";
  115.             }   
  116.             $result .= '</products>' . "\n" .
  117.                     '</order>' . "\n";
  118.         }
  119.         echo $result .= '</xml_orders>' . "\n";
  120.     }
  121.     private function dump($data, $leave = 0) {
  122.         echo '<pre>';
  123.         var_dump($data);
  124.         echo '</pre>';
  125.         if (!$leave) die;
  126.     }
  127. }
  128. new export2XML($is_test);?>

 

Рекомендую отличный хостинг