2017-06-12 2 views
0

In einer Prestashop E-Commerce-Website habe ich ein Modul, das speziell zum Vergleichen und Festlegen von Produkten Preis auf meiner Website, erstellt wurde Ich brauche etwas Hilfe zu verstehen, einige seiner PHP-Code.php Prestashop- wo finde ich die folgenden Funktionen

Der followng Code setzen Sie den Preis eines Produkts in meiner Website nach dem Scannen und der minumum Preis anderer Website-Link providede am Produkt Back-End-Seite zu finden:

Meine Frage ist: Wie funktioniert dieser Code funktioniert? Wo ist der Teil des Codes, der die andere Website scannt und den minimalen Preis enthält?

<?php 

class ProductCMP extends Module 
{ 
    public function __construct() 
    { 
     $this->name = 'productcmp'; 
     $this->tab = 'front_office_features'; 
     $this->version = '1.0.0'; 
     $this->author = 'biznesownia'; 
     parent::__construct(); 
     $this->displayName = $this->l('ProductCMP', FALSE, NULL); 
    } 

    public function install() 
    { 
     return parent::install() 
     and $this->registerHook('displayAdminProductsExtra') 
     and $this->registerHook('actionAdminProductsControllerSaveAfter'); 
    } 

    public function hookDisplayAdminProductsExtra($params) 
    { 
     $product = new Product((int) Tools::getValue('id_product'), false); 

     $tpl = $this->context->smarty->createTemplate(dirname(__FILE__).'/templates/tab.tpl', $this->context->smarty); 
     $tpl->assign('product', $product); 

     return $tpl->fetch(); 
    } 

    public function hookActionAdminProductsControllerSaveAfter($params) 
    { 
     # set data 
     $id_product = (int) Tools::getValue('id_product'); 
     $product = new Product($id_product, false, (int) $this->context->language->id); 

     $product->compare_active = (bool) Tools::getValue('compare_active', false); 
     $product->compare_link = (string) Tools::getValue('compare_link', ''); 
     $product->compare_price = (string) Tools::getValue('compare_price', 0); 

     return $product->save(); 
    } 

    public function compare() 
    { 
     define('PRODUCT_COMPARE_EMAIL', '[email protected]'); 
     set_time_limit(60*60); 

     # get list 
     $products = Db::getInstance()->executeS(" 
      SELECT p.`id_product`, pl.`name` 
      FROM `"._DB_PREFIX_."product` p 
      LEFT JOIN `"._DB_PREFIX_."product_lang` pl ON (p.`id_product` = pl.`id_product` ".Shop::addSqlRestrictionOnLang('pl').") 
      WHERE 
      pl.`id_lang` = ".(int)$this->context->language->id." 
      AND p.`compare_active`=1 
      ORDER BY p.`id_product` 
     "); 

     # job 
     $report = array(); 
     foreach($products as $p) 
     { 
      try 
      { 
       $result = $this->_compareProduct((int) $p['id_product']); 
       if($result && $result['changed']) 
       { 
        $result['id_product'] = $p['id_product']; 
        $result['name'] = $p['name']; 
        $report[] = $result; 
       } 
      } 
      catch(Exception $e) 
      { 
       $report[] = array(
        'id_product' => $p['id_product'], 
        'name' =>  $p['name'], 
        'res' =>  false, 
        'msg' =>  'Exception occured', 
       ); 
      } 
     } 

     # if there is nothing to report 
     if(!$report) 
      return true; 

     # output 
     $output = ''; 
     foreach($report as $row) 
     { 
      $link = $this->context->link->getProductLink($row['id_product']); 
      $output .= $row['id_product'] .' - <a href="'.$link.'" target="_blank">'. $row['name'] .'</a> '. ($row['res'] ? 'Ok!' : 'Error!') .' : '. $row['msg'] ."\r\n"; 
     } 

     # send mail 
     $tpl_vars = array(
      '{report_txt}' => strip_tags($output), 
      '{report_html}' => nl2br($output), 
     ); 

     Mail::Send(
      $this->context->language->id, 
      'report', 
      'Price update report', 
      $tpl_vars, 
      PRODUCT_COMPARE_EMAIL, 
      '', 
      Configuration::get('PS_SHOP_EMAIL'), 
      Configuration::get('PS_SHOP_NAME'), 
      null, 
      null, 
      dirname(__FILE__).'/mails/', 
      null, 
      $this->context->shop->id 
     ); 

     return true; 
    } 

    public function compareTest($id_product) 
    { 
     $report = $this->_compareProduct($id_product); 
     var_dump($report); 
     die; 
    } 

    protected function _compareProduct($id_product) 
    { 
     # load product 
     $product = new Product($id_product, false, $this->context->language->id, $this->context->shop->id); 
     if(!Validate::isLoadedObject($product)) 
      return false; 

     if(!$product->compare_link) 
      return false; 

     $oldPrice = (float) $product->price; 




     //****** 
     # request 
     $data = file_get_contents($product->compare_link); 

     # analize price 
     if(!preg_match('!data-free-data=\'([^\']+)\'!i', $data, $matches)) 
     { 
      return array(
       'res' => false, 
       'msg' => 'Price not found!', 
      ); 
     } 
     //****** 





     $prices = json_decode($matches[1], true); 
     $newPrice = (float) $prices['minPrice']; 
     if(!$newPrice) 
     { 
      return array(
       'res' => false, 
       'msg' => 'Zero price!', 
      ); 
     } 

     # check change 
     $changed = $oldPrice != $newPrice; 
     if(!$changed) 
     { 
      return array(
       'res' => false, 
       'msg' => 'The price is the same.', 
      ); 
     } 

     $newPrice += (float) $product->compare_price; 

     # check change 
     $changed = $oldPrice != $newPrice; 
     if(!$changed) 
     { 
      return array(
       'res' => false, 
       'msg' => 'The price is the same.', 
      ); 
     } 

     # update 
     $product->price = $newPrice; 
     if(!$product->save()) 
     { 
      return array(
       'res' => false, 
       'msg' => 'Not saved!', 
      ); 
     } 

     return array(
      'res' => true, 
      'changed' => $changed, 
      'msg' => 'Updated! From: '.round($oldPrice, 2).' To: '.round($newPrice, 2), 
     ); 
    } 
} 

Antwort

0

Es werden benötigt Funktion

protected function _compareProduct($id_product) 
    { 
     # load product 
     $product = new Product($id_product, false, $this->context->language->id, $this->context->shop->id); 
     if(!Validate::isLoadedObject($product)) 
      return false; 

     if(!$product->compare_link) 
      return false; 

     $oldPrice = (float) $product->price; 




     //****** 
     # request 
     $data = file_get_contents($product->compare_link); 

     # analize price 
     if(!preg_match('!data-free-data=\'([^\']+)\'!i', $data, $matches)) 
     { 
      return array(
       'res' => false, 
       'msg' => 'Price not found!', 
      ); 
     } 
     //****** 





     $prices = json_decode($matches[1], true); 
     $newPrice = (float) $prices['minPrice']; 
     if(!$newPrice) 
     { 
      return array(
       'res' => false, 
       'msg' => 'Zero price!', 
      ); 
     } 

     # check change 
     $changed = $oldPrice != $newPrice; 
     if(!$changed) 
     { 
      return array(
       'res' => false, 
       'msg' => 'The price is the same.', 
      ); 
     } 

     $newPrice += (float) $product->compare_price; 

     # check change 
     $changed = $oldPrice != $newPrice; 
     if(!$changed) 
     { 
      return array(
       'res' => false, 
       'msg' => 'The price is the same.', 
      ); 
     } 

     # update 
     $product->price = $newPrice; 
     if(!$product->save()) 
     { 
      return array(
       'res' => false, 
       'msg' => 'Not saved!', 
      ); 
     } 

     return array(
      'res' => true, 
      'changed' => $changed, 
      'msg' => 'Updated! From: '.round($oldPrice, 2).' To: '.round($newPrice, 2), 
     ); 
    } 
+0

Was ?! können Sie erklären? –