CMS OpenCart — добавление поля для товара
Q: Как добавить дополнительное поле для продукта?
A: Вам нужно сделать ряд изменений:
Admin
ALTER TABLE `имя_таблицы` ADD `price_for` varchar(64) NOT NULL;
Model
/admin/model/catalog/product.php
$this->db->query(«INSERT INTO » . DB_PREFIX . «product
SET model = ‘» . $this->db->escape($data[‘model’]) . «‘,
sku = ‘» . $this->db->escape($data[‘sku’]) . «‘,
location = ‘» . $this->db->escape($data[‘location’]) . «‘,
quantity = ‘» . (int)$data[‘quantity’] . «‘,
stock_status_id = ‘» . (int)$data[‘stock_status_id’] . «‘,
date_available = ‘» . $this->db->escape($data[‘date_available’]) . «‘,
manufacturer_id = ‘» . (int)$data[‘manufacturer_id’] . «‘,
shipping = ‘» . (int)$data[‘shipping’] . «‘,
price = ‘» . (float)$data[‘price’] . «‘,
price_for = ‘» . (float)$data[‘price_for’] . «‘,
weight = ‘» . (float)$data[‘weight’] . «‘,
weight_class_id = ‘» . (int)$data[‘weight_class_id’] . «‘,
length = ‘» . (float)$data[‘length’] . «‘,
width = ‘» . (float)$data[‘width’] . «‘,
height = ‘» . (float)$data[‘height’] . «‘,
measurement_class_id = ‘» . (int)$data[‘measurement_class_id’] . «‘,
status = ‘» . (int)$data[‘status’] . «‘,
tax_class_id = ‘» . (int)$data[‘tax_class_id’] . «‘,
date_added = NOW()»);
$this->db->query(«UPDATE » . DB_PREFIX . «product
SET model = ‘» . $this->db->escape($data[‘model’]) . «‘,
sku = ‘» . $this->db->escape($data[‘sku’]) . «‘,
location = ‘» . $this->db->escape($data[‘location’]) . «‘,
quantity = ‘» . (int)$data[‘quantity’] . «‘,
stock_status_id = ‘» . (int)$data[‘stock_status_id’] . «‘,
date_available = ‘» . $this->db->escape($data[‘date_available’]) . «‘,
manufacturer_id = ‘» . (int)$data[‘manufacturer_id’] . «‘,
shipping = ‘» . (int)$data[‘shipping’] . «‘,
price = ‘» . (float)$data[‘price’] . «‘,
price_for = ‘» . (float)$data[‘price_for’] . «‘,
weight = ‘» . (float)$data[‘weight’] . «‘,
weight_class_id = ‘» . (int)$data[‘weight_class_id’] . «‘,
length = ‘» . (float)$data[‘length’] . «‘,
width = ‘» . (float)$data[‘width’] . «‘,
height = ‘» . (float)$data[‘height’] . «‘,
measurement_class_id = ‘» . (int)$data[‘measurement_class_id’] . «‘,
status = ‘» . (int)$data[‘status’] . «‘,
tax_class_id = ‘» . (int)$data[‘tax_class_id’] . «‘,
date_modified = NOW() WHERE product_id = ‘» . (int)$product_id . «‘»);
Controller
/admin/controller/catalog/product.php
$this->data[‘entry_price’] = $this->language->get(‘entry_price’);
$this->data[‘entry_price_for’] = $this->language->get(‘entry_price_for’);
if (isset($this->request->post[‘price’])) {
$this->data[‘price’] = $this->request->post[‘price’];
} else if (isset($product_info)) {
$this->data[‘price’] = $product_info[‘price’];
} else {
$this->data[‘price’] = »;
}
if (isset($this->request->post[‘price_for’])) {
$this->data[‘price_for’] = $this->request->post[‘price_for’];
} else if (isset($product_info)) {
$this->data[‘price_for’] = $product_info[‘price_for’];
} else {
$this->data[‘price_for’] = »;
}
View
/admin/view/template/catalog/product_form.tpl
<tr>
<td><?php echo $entry_price; ?></td>
<td><input type=»text» name=»price» value=»<?php echo $price; ?>» /></td>
</tr>
<tr>
<td><?php echo $entry_price_for; ?></td>
<td><input type=»text» name=»price_for» value=»<?php echo $price_for; ?>» /></td>
</tr>
Language
/admin/language/russian/catalog/product.php
$_[‘entry_price_for’] = ‘Цена за:’;
Template
Controller
/catalog/controller/product/product.php
$this->data[‘text_price’] = $this->language->get(‘text_price’);
$this->data[‘text_price_for’] = $this->language->get(‘text_price_for’);
$this->data[‘price’] = $product_info[‘price’];
$this->data[‘price_for’] = $product_info[‘price_for’];
$this->data[‘products’][] = array(
‘product_id’ => $result[‘product_id’],
‘thumb’ => $image,
‘name’ => $result[‘name’],
‘price’ => $price,
‘price_for’ => $price_for,
‘special’ => $special,
‘rating’ => $rating,
‘reviews’ => sprintf($this->language->get(‘text_reviews’), (int)$result[‘reviews’]),
‘href’ => $this->url->link(‘product/product’,
‘product_id=’ . $result[‘product_id’])
);
View
/catalog/view/theme/ваша_тема/template/product/product.tpl
<tr>
<td>[b]<?php echo $text_price; ?>[/b]</td>
<td><?php if (!$special) { ?>
<?php echo $price; ?>
<?php } else { ?>
<span style=»text-decoration: line-through;»><?php echo $price; ?></span> [color= #F00;]<?php echo $special; ?>[/color]
<?php } ?></td>
</tr>
<tr>
<td><b><?php echo $text_price_for; ?></b></td>
<td><?php echo $price_for; ?></td>
</tr>
Language
/catalog/language/russian/product/product.php
$_[‘text_price’] = ‘Цена:’;
$_[‘text_price_for’] = ‘Цена за:’;