php Prestashop-在哪里可以找到以下功能

时间:2017-06-12 13:45:15

标签: php module prestashop

在Prestashop电子商务网站中,我有一个模块,专门用于比较和设置我网站上的产品价格, 我需要一些帮助才能理解他的一些PHP代码。

以下代码在扫描后在我的网站上设置产品的价格,并在产品后端页面的其他网站链接中找到最低价格:

我的问题是:此代码如何运作?扫描其他网站并获得最低价格的代码部分在哪里?

<?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', 'uditools@gmail.com');
        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),
        );
    }
}

1 个答案:

答案 0 :(得分:0)

需要功能

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),
        );
    }