opencart开发前台显示指定分类的产品模组(块)

通过清心醉

opencart开发前台显示指定分类的产品模组(块)

和开发支付方式一样,开发模块需要有对应的MVC+L参数,并且Language里必须有$_[‘header_title’]模块名.同时该模型名必须是唯一的.

opencart后台会循环读取/controller/module里的模块PHP文件代码,然后根据每个文件的$_[‘header_title’]获取模块的标题名称.

同时该文件的类名(比如作者现在开发显示分类的产品,类是:class ControllerModuleCategoryproduct extends Controller) .

类名是Categoryproduct,然后根据命名规则,查询extension表内的code有无对应的参数值,如果没有,用户可以安装,否则是添加更多.

我们来看看extension表内是如何区分的:

extension_id=>’27’,type=>’module’,code=’categoryproduct’;

//类型为module里的文件,参数是类名为categoryproduct的模块,安装后就可以看到

extension_id=>’28’,type=>’payment’,code=’paypal’;

//类型为payment支付扩展里的文件,参数是类名为paypal的模块.

好了,说了数据库表内的关系,来看看完整控制器吧(视图请自行编写)

class ControllerModuleCategoryproduct extends Controller {
private $error = array();

public function index() {
$this->load->language(‘module/categoryproduct’);

$this->document->setTitle($this->language->get(‘heading_title’));

$this->load->model(‘extension/module’);

if (($this->request->server[‘REQUEST_METHOD’] == ‘POST’) && $this->validate()) {
if (!isset($this->request->get[‘module_id’])) {
$this->model_extension_module->addModule(‘categoryproduct’, $this->request->post);
} else {
$this->model_extension_module->editModule($this->request->get[‘module_id’], $this->request->post);
}

$this->session->data[‘success’] = $this->language->get(‘text_success’);

$this->response->redirect($this->url->link(‘extension/module’, ‘token=’ . $this->session->data[‘token’], ‘SSL’));
}

$data[‘heading_title’] = $this->language->get(‘heading_title’);

$data[‘text_edit’] = $this->language->get(‘text_edit’);
$data[‘text_enabled’] = $this->language->get(‘text_enabled’);
$data[‘text_disabled’] = $this->language->get(‘text_disabled’);

$data[‘entry_name’] = $this->language->get(‘entry_name’);
$data[‘entry_product’] = $this->language->get(‘entry_product’);
$data[‘entry_limit’] = $this->language->get(‘entry_limit’);
$data[‘entry_width’] = $this->language->get(‘entry_width’);
$data[‘entry_height’] = $this->language->get(‘entry_height’);
$data[‘entry_status’] = $this->language->get(‘entry_status’);

$data[‘help_product’] = $this->language->get(‘help_product’);

$data[‘button_save’] = $this->language->get(‘button_save’);
$data[‘button_cancel’] = $this->language->get(‘button_cancel’);

if (isset($this->error[‘warning’])) {
$data[‘error_warning’] = $this->error[‘warning’];
} else {
$data[‘error_warning’] = ”;
}

if (isset($this->error[‘name’])) {
$data[‘error_name’] = $this->error[‘name’];
} else {
$data[‘error_name’] = ”;
}

if (isset($this->error[‘width’])) {
$data[‘error_width’] = $this->error[‘width’];
} else {
$data[‘error_width’] = ”;
}

if (isset($this->error[‘height’])) {
$data[‘error_height’] = $this->error[‘height’];
} else {
$data[‘error_height’] = ”;
}

$data[‘breadcrumbs’] = array();

$data[‘breadcrumbs’][] = array(
‘text’ => $this->language->get(‘text_home’),
‘href’ => $this->url->link(‘common/dashboard’, ‘token=’ . $this->session->data[‘token’], ‘SSL’)
);

$data[‘breadcrumbs’][] = array(
‘text’ => $this->language->get(‘text_module’),
‘href’ => $this->url->link(‘extension/module’, ‘token=’ . $this->session->data[‘token’], ‘SSL’)
);

if (!isset($this->request->get[‘module_id’])) {
$data[‘breadcrumbs’][] = array(
‘text’ => $this->language->get(‘heading_title’),
‘href’ => $this->url->link(‘module/categoryproduct’, ‘token=’ . $this->session->data[‘token’], ‘SSL’)
);
} else {
$data[‘breadcrumbs’][] = array(
‘text’ => $this->language->get(‘heading_title’),
‘href’ => $this->url->link(‘module/categoryproduct’, ‘token=’ . $this->session->data[‘token’] . ‘&module_id=’ . $this->request->get[‘module_id’], ‘SSL’)
);
}

if (!isset($this->request->get[‘module_id’])) {
$data[‘action’] = $this->url->link(‘module/categoryproduct’, ‘token=’ . $this->session->data[‘token’], ‘SSL’);
} else {
$data[‘action’] = $this->url->link(‘module/categoryproduct’, ‘token=’ . $this->session->data[‘token’] . ‘&module_id=’ . $this->request->get[‘module_id’], ‘SSL’);
}

$data[‘cancel’] = $this->url->link(‘extension/module’, ‘token=’ . $this->session->data[‘token’], ‘SSL’);

if (isset($this->request->get[‘module_id’]) && ($this->request->server[‘REQUEST_METHOD’] != ‘POST’)) {
$module_info = $this->model_extension_module->getModule($this->request->get[‘module_id’]);
}

$data[‘token’] = $this->session->data[‘token’];

if (isset($this->request->post[‘name’])) {
$data[‘name’] = $this->request->post[‘name’];
} elseif (!empty($module_info)) {
$data[‘name’] = $module_info[‘name’];
} else {
$data[‘name’] = ”;
}

$this->load->model(‘catalog/category’);  //使用分类

$data[‘categorys’] = array();
//定义分类数组

if (isset($this->request->post[‘category’]))
{
$data[‘categorys’] = $this->request->post[‘category’];
//如果有存在POST过来的category,直接视图上使用
}
else
{   //否则
$categorys = $this->model_catalog_category->getCategoryAll();
foreach ($categorys as $category)
{
$data[‘categorys’][] = array(
‘category_id’ => $category[‘category_id’],
‘name’       => $category[‘name’]
);
}
}
if (isset($this->request->post[‘limit’])) {
$data[‘limit’] = $this->request->post[‘limit’];
} elseif (!empty($module_info)) {
$data[‘limit’] = $module_info[‘limit’];
} else {
$data[‘limit’] = 8;
}

if (isset($this->request->post[‘width’])) {
$data[‘width’] = $this->request->post[‘width’];
} elseif (!empty($module_info)) {
$data[‘width’] = $module_info[‘width’];
} else {
$data[‘width’] = 200;
}

if (isset($this->request->post[‘height’])) {
$data[‘height’] = $this->request->post[‘height’];
} elseif (!empty($module_info)) {
$data[‘height’] = $module_info[‘height’];
} else {
$data[‘height’] = 300;
}

if (isset($this->request->post[‘status’])) {
$data[‘status’] = $this->request->post[‘status’];
} elseif (!empty($module_info)) {
$data[‘status’] = $module_info[‘status’];
} else {
$data[‘status’] = ”;
}

$data[‘header’] = $this->load->controller(‘common/header’);
$data[‘column_left’] = $this->load->controller(‘common/column_left’);
$data[‘footer’] = $this->load->controller(‘common/footer’);

$this->response->setOutput($this->load->view(‘module/categoryproduct.tpl’, $data));
}

protected function validate() {
if (!$this->user->hasPermission(‘modify’, ‘module/categoryproduct’)) {
$this->error[‘warning’] = $this->language->get(‘error_permission’);
}

if ((utf8_strlen($this->request->post[‘name’]) < 3) || (utf8_strlen($this->request->post[‘name’]) > 64)) {
$this->error[‘name’] = $this->language->get(‘error_name’);
}

if (!$this->request->post[‘width’]) {
$this->error[‘width’] = $this->language->get(‘error_width’);
}

if (!$this->request->post[‘height’]) {
$this->error[‘height’] = $this->language->get(‘error_height’);
}

return !$this->error;
}
}

OPENCART的模块如果有执行安装,数据不单只是追加到extension表.为什么呢?

因为opencart一个模块可以在多个地方使用.比如作者现在开发的主页显示指定分类的产品

可以选择分类ID为1,也可以添加为2,也可以添加为3等等.如果是同一个页面,一个位置,只是上下位不同,可以使用同一个视图类型.

来看看OPENCART是如何实现多元化的:

在module表内:

module_id=>模块ID

name=>模块名字(多元化的关键,一个模块派生出来的多个名字命名)

code=>模块参数(categoryproduct)

setting=>数值(JSON类型);

接着是前台获取模块参数的控制器,文中使用的视图请自行编写

class ControllerModuleCategoryproduct extends Controller
{
public function index($setting)
{
$this->load->language(‘module/categoryproduct’);
$data[‘heading_title’] = $this->language->get(‘categoryproduct’);
$this->load->model(‘catalog/category’);
$data[‘categoryname’]=$this->model_catalog_category->getCategoryname($setting[‘category’]);

$data[‘text_tax’] = $this->language->get(‘text_tax’);

$data[‘button_cart’] = $this->language->get(‘button_cart’);
$data[‘button_wishlist’] = $this->language->get(‘button_wishlist’);
$data[‘button_compare’] = $this->language->get(‘button_compare’);

if (!$setting[‘limit’]) {
$setting[‘limit’] = 4;
}  //如果没设置数量,那么数量为4

$this->load->model(‘catalog/product’);
$this->load->model(‘tool/image’);
//获取分类产品及图行工具模型
$category_id=$setting[‘category’]; //获取分类ID
$products_id=$this->model_catalog_product->getCategoryProducts($category_id);
$data[‘products’]=array();
for ($o=0;$o<count($products_id);$o++)
{
if ($o==$setting[‘limit’]) //判断循环提取的数量是否达到了要求
{
break;
}
$product_info = $this->model_catalog_product->getProduct($products_id[$o][‘product_id’]);
if ($product_info)
{
if ($product_info[‘image’])
{
$image = $this->model_tool_image->resize($product_info[‘image’], $setting[‘width’], $setting[‘height’]);
} else
{
$image = $this->model_tool_image->resize(‘placeholder.png’, $setting[‘width’], $setting[‘height’]);
}

if (($this->config->get(‘config_customer_price’) && $this->customer->isLogged()) || !$this->config->get(‘config_customer_price’)) {
$price = $this->currency->format($this->tax->calculate($product_info[‘price’], $product_info[‘tax_class_id’], $this->config->get(‘config_tax’)));
} else {
$price = false;
}

if ((float)$product_info[‘special’]) {
$special = $this->currency->format($this->tax->calculate($product_info[‘special’], $product_info[‘tax_class_id’], $this->config->get(‘config_tax’)));
} else {
$special = false;
}

if ($this->config->get(‘config_tax’)) {
$tax = $this->currency->format((float)$product_info[‘special’] ? $product_info[‘special’] : $product_info[‘price’]);
} else {
$tax = false;
}

if ($this->config->get(‘config_review_status’)) {
$rating = $product_info[‘rating’];
} else {
$rating = false;
}

$data[‘products’][$o] = array(
‘product_id’  => $product_info[‘product_id’],
‘thumb’       => $image,
‘name’        => $product_info[‘name’],
‘description’ => utf8_substr(strip_tags(html_entity_decode($product_info[‘description’], ENT_QUOTES, ‘UTF-8’)), 0, $this->config->get(‘config_product_description_length’)) . ‘..’,
‘price’       => $price,
‘special’     => $special,
‘tax’         => $tax,
‘rating’      => $rating,
‘href’        => $this->url->link(‘product/product’, ‘product_id=’ . $product_info[‘product_id’])
);
}
}
if ($data[‘products’])
{
if (file_exists(DIR_TEMPLATE . $this->config->get(‘config_template’) . ‘/template/module/categoryproduct.tpl’)) {
return $this->load->view($this->config->get(‘config_template’) . ‘/template/module/categoryproduct.tpl’, $data);
} else {
return $this->load->view(‘default/template/module/categoryproduct.tpl’, $data);
}
}
}
}

 

 

关于作者

清心醉 administrator

发表评论

如果喜欢作者的文章,您可以打赏给作者:

TRC20(虚拟货币):


ERC20(虚拟货币):


Bitcoin(BTC):