Jump to content

understanding simple Module php code


Recommended Posts

In my Prestashop Ecommerce website I have a module that was created specially to compare and set products price in my website,

I need some help understand some of his PHP code.

 

The followng code set the price of a product in my website after scanning and finding the minumum price in other website link providede at the product backend page:

 

**My Question is:** Where is the part of the code that scan the other website and get the minumum price in it?

 

I see that there are few code lines that supposed to get in the end the new price:

$prices = json_decode($matches[1], true);

$newPrice = (float) $prices['minPrice'];

 

But how does they get the answer?

 

 

Full code :

 

    <?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),

    );

    }

    }

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...