I have an embedded C background so used to programming, just not HTML/PHP/Opencart. I have read and followed a number of tutorials about OpenCart's MVC architecture and after experimenting I think I have the basics down.
The eventual output from my custom module will be a featured product box that displays only 1 product rather than the 3/4 products currently in the "Featured" module, but with more information and and quick add to cart button.
Starting at the admin back end i have modified the Featured.php controller, language and template. the FeaturedSingle module I created is working in the admin backend except for 1 part. I can't successfully change the part of the form that allows for multiple products to be selected into one that only allows one to be selected.
The original Controller code:
Code: Select all
$this->load->model('catalog/product');
$data['products'] = array();
if (isset($this->request->post['product'])) {
$products = $this->request->post['product'];
} elseif (!empty($module_info)) {
$products = $module_info['product'];
} else {
$products = array();
}
foreach ($products as $product_id) {
$product_info = $this->model_catalog_product->getProduct($product_id);
if ($product_info) {
$data['products'][] = array(
'product_id' => $product_info['product_id'],
'name' => $product_info['name']
);
}
}
Code: Select all
<div class="form-group">
<label class="col-sm-2 control-label" for="input-product"><?php echo $entry_product; ?></label>
<div class="col-sm-10">
<input type="text" name="product" value="" placeholder="<?php echo $entry_product; ?>" id="input-product" class="form-control" />
<div id="featured-product" class="well well-sm" style="height: 150px; overflow: auto;">
<?php foreach ($products as $product) { ?>
<div id="featured-product<?php echo $product['product_id']; ?>"><i class="fa fa-minus-circle"></i> <?php echo $product['name']; ?>
<input type="hidden" name="product[]" value="<?php echo $product['product_id']; ?>" />
</div>
<?php } ?>
</div>
</div>
</div>
Controller
Code: Select all
$this->load->model('catalog/product');
if (isset($this->request->post['product_id'])) {
$data['product_id'] = $this->request->post['product_id'];
} elseif (!empty($module_info)) {
$data['product_id'] = $module_info['product_id'];
} else {
$data['product_id'] = 0;
}
if (isset($this->request->post['product'])) {
$data['product'] = $this->request->post['product'];
} elseif (!empty($module_info)) {
$product_info = $this->model_catalog_product->getProduct($product_id);
if ($product_info) {
$data['product'] = $product_info['name'];
} else {
$data['product'] = '';
}
} else {
$data['product'] = '';
}
Code: Select all
<div class="form-group">
<label class="col-sm-2 control-label" for="input-product"><?php echo $entry_product; ?></label>
<div class="col-sm-10">
<input type="text" name="product" value="<?php echo $product ?>" placeholder="<?php echo $entry_product; ?>" id="input-product" class="form-control" />
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>" />
</div>
</div>
Any ideas?