How to save bulk products on admin product list page on Opencart?
Posted: Sat Nov 11, 2017 6:17 am
Hi. I want to save selected products automaticly by clicking a save button. Actually, I wrote some codes to make calculations with product price and customer group's special prices. And I have to edit 2 times a product. First, I enter the price and a percentage value, then save. So, the controller file (product.php) makes the calculations for special prices. And the second save is for saving these prices to database. This second save is just click edit and then click save
That's why I need a "save all selected products" button.
I added a button on opencart product listing page : \admin\view\template\catalog\product_list.tpl
And make the changes below on the controller file : \admin\controller\catalog\product.php
I used this function:
And after the code :
I added :
However, when I selected some products and clicked the save button, nothing is happen and the selected products are disappeared. But they are still on database (with old values of course).
Is there any way to do that? What is wrong with my codes? I would appreciate if anyone gives suggestion.

I added a button on opencart product listing page : \admin\view\template\catalog\product_list.tpl
Code: Select all
<button type="button" data-toggle="tooltip" title="<?php echo 'SAVE SELECTED'; ?>" class="btn btn-primary" onclick="$('#form-product').attr('action', '<?php echo $save_selected; ?>').submit()"><i class="fa fa-save"></i></button>
I used this function:
Code: Select all
public function save_selected() {
$this->load->language('catalog/product');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('catalog/product');
if (isset($this->request->post['selected']) && $this->validateCopy()) {
foreach ($this->request->post['selected'] as $product_id) {
$data = array();
$data = $this->model_catalog_product->getProduct($product_id);
$this->model_catalog_product->editProduct($product_id, $data);
}
$this->session->data['success'] = $this->language->get('text_success');
$url = '';
if (isset($this->request->get['filter_name'])) {
$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['filter_model'])) {
$url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['filter_price'])) {
$url .= '&filter_price=' . $this->request->get['filter_price'];
}
if (isset($this->request->get['filter_quantity'])) {
$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
}
if (isset($this->request->get['filter_status'])) {
$url .= '&filter_status=' . $this->request->get['filter_status'];
}
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}
$this->response->redirect($this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url, 'SSL'));
}
$this->getList();
}
Code: Select all
$data['copy'] = $this->url->link('catalog/product/copy', 'token=' . $this->session->data['token'] . $url, 'SSL');
Code: Select all
$data['save_selected'] = $this->url->link('catalog/product/save_selected', 'token=' . $this->session->data['token'] . $url, 'SSL');
Is there any way to do that? What is wrong with my codes? I would appreciate if anyone gives suggestion.