在prestashop中创建模块:error';找不到类模块';


creating a module in prestashop: error 'class Module cannot be found'

php4中有一个presta模块。它通过连接到prestashop的数据库并打印出xml来工作。当人们试图上传到他们的应用商店时,他们要求我把它做成php5,并遵循某些程序。

  • 简而言之,它只是一个完整的结构代码,带有1个方法,可以连接到prestashop数据库并打印出xml

所以问题是如何使它符合prestashop所要求的php5标准。当我尝试时,我得到了这个错误。"找不到类模块"

<?php
include('config/settings.inc.php');
$con = mysql_connect(_DB_SERVER_, _DB_USER_, _DB_PASSWD_);
mysql_select_db(_DB_NAME_);
$str = '_DB_PREFIX_';
$str1 = constant($str);
$sql = 'SELECT '.
$str1.'product_lang.name,'.
$str1.'product.id_product as id,'.
$str1.'product.quantity,'.
$str1.'product.price,'.
$str1.'product.weight,'.
$str1.'product_lang.description,'.
$str1.'product_lang.link_rewrite as url,'.
$str1.'category_lang.name as category,'.
$str1.'manufacturer.name as manufacturer,'.
$str1.'image.id_image '.
'FROM '.
$str1.'product_lang '.
'INNER JOIN ' .$str1.'product '.
'ON ('.$str1.'product_lang.id_product = '.$str1.'product.id_product) '.
'INNER JOIN ' .$str1.'image '.
'ON ('.$str1.'image.id_product = '.$str1.'product.id_product) '.
'INNER JOIN ' .$str1.'manufacturer '.
'ON ('.$str1.'manufacturer.id_manufacturer = '.$str1.'product.id_manufacturer) '.
'INNER JOIN ' .$str1.'category_lang '.
'ON ('.$str1.'category_lang.id_category = '.$str1.'product.id_category_default) '.
//'WHERE '.$str1.'product_lang.id_lang = 1 AND '.$str1.'category_lang.id_lang = 1';
    'WHERE '.$str1.'product_lang.id_lang = 1';
    $result = mysql_query($sql);
     header("Content-Type: text/xml; charset=ISO-8859-1");
     $output = '<?xml version="1.0" encoding="utf-8"?>
     <products>';
     while($row = mysql_fetch_assoc($result)):
     //echo "<br><br><b>text:</b>".$text = addslashes($text);
     $text = str_replace(chr(145), "''", $text);
 $output .= '
<product>
    <id>'. $row['id'].'</id>
    <name><![CDATA['.$name.']]></name>
    <description><![CDATA['.$text.']]></description>
            <image><![CDATA['. 'http://'.$_SERVER['HTTP_HOST'].__PS_BASE_URI__.'img/p/'.$row['id'].'-'.$row['id_image'].'.jpg' .']]></image>
        <quantity><![CDATA['. $row['quantity'] .']]></quantity> 
        <price><![CDATA['. $row['price'] .']]></price>
        <weight>'. $row['weight'] .'</weight>
    <category><![CDATA['.$category.']]></category>
    <manufacturer><![CDATA['. $manufacturer.']]></manufacturer>
            <url><![CDATA['.'http://'.$_SERVER['HTTP_HOST'].'/product.php?id_product='.$row['id'].']]></url>
     </product>';
 endwhile;
 print $output .= '
 </products>';

这是我需要从prestashop 中遵循的编码过程

<?php
       //Your class must have the same name than this file.
      class module_name extends Module
       {
      public function __construct()
       {
    //Name of your module. It must have the same name than the class
    $this->name = 'module_name';
    //You must choose an existing tab amongst the ones that are available
    $this->tab = 'You choose';
    //The version of your module. Do not forget to increment the version for each modification
    $this->version = '1.0';
    //The constructor must be called after the name has been set, but before you try to use any functions like $this->l()
    parent::__construct();
    //Name displayed in the module list
    $this->displayName = $this->l('Display Name on Back Office');
    //Short description displayed in the module list
    $this->description = $this->l('Description On Back Office');    
}
//You must implement the following methods if your module need to create a table, add configuration variables, or hook itself somewhere.
//-------------------------------
public function install()
{
    return parent::install();
}
public function uninstall()
{
    return parent::install();
}
//-------------------------------
//Display Configuration page of your module.
public function getContent()
{
    return 'Hello World!';
}
   }
 ?>

不久前,我写了一系列关于为prestashop编写模块的文章,您可能会发现这些文章很有帮助:编写您自己的prestashop模块第1部分

这些描述了基本体系结构以及模块通常是如何实现的。

我有一种感觉,你希望这个脚本是独立的,而不是从后台管理屏幕中生成的xml?如果是这样的话,那么根据Prestashop的定义,它就不会被归类为"模块",这就是它被拒绝的原因。

无论您编写的是独立脚本还是模块,您还需要使用各种Prestashop API调用来检索产品信息,而不仅仅是在原始数据库表上执行sql。原因是原始sql不会考虑语言、税收或货币转换等因素。

如何从Prestashop模块中获取所有产品并打印有关它们的详细信息的示例如下:

global $cookie;
$products = Product::getProducts($cookie->id_lang, 0, NULL,'id_product', 'ASC');
foreach ($products AS product) {
  echo 'title: '. $product['name'];
  echo 'weight: '. $product['weight'];
  // .. etc. 
}

祝您的Prestashop编程一切顺利!

这里需要的是独立的代码。要做到这一点,您需要自己初始化prestashop。这可能是最可靠的方法。

if (!defined('_PS_VERSION_')) {
    require_once '../../config/config.inc.php';
    require_once '../../init.php';
}

这将允许您使用module类,以及您需要使用的prestashop框架中的任何其他类。