I am trying to figure out an issue with an undefined index error when a product is saved or edited.
I have created a new entry in the product table in the database called disable_cart which is set to int(1) and has a default value of 0.
On the product_form.tpl it is simply a checkbox.
<div class="col-sm-10">
<div class="checkbox">
<label>
<?php if ($disable_cart) { ?>
<input type="checkbox" name="disable_cart" value="1" checked="checked" id="input-disable_cart" />
<?php } else { ?>
<input type="checkbox" name="disable_cart" value="1" id="input-disable_cart" />
<?php } ?>
</label>
</div>
</div>
The code in the product controller
if (isset($this->request->post['disable_cart'])) {
$data['disable_cart'] = $this->request->post['disable_cart'];
} elseif (!empty($product_info['disable_cart'])) {
$data['disable_cart'] = $product_info['disable_cart'];
} else {
$data['disable_cart'] = 0;
}
And the model in the addProduct() function
public function addProduct($data) {
$this->event->trigger('pre.admin.product.add', $data);
. "', disable_cart = '" . (int)$data['disable_cart'] . "', date_added = NOW()");
Same in the editProduct() function. It works fine on my localhost but only works on the dev server if the checkbox is ticked otherwise I get the following error
Undefined index: disable_cart in /admin/model/catalog/product.php on line 134Warning: Cannot modify header information - headers already sent by (output started at /admin/index.php:80) in /system/library/response.php on line 12
Can't figure out the issue, any help is greatly appreciated.
Thanks
edit: The value does save in the DB but it still throws the error
Your problem is due to the fact that an unchecked Checkbox will not be sent through post
post['disable_cart']
. When you post a new product or make an edit, this part of the code is called:
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { $this->model_catalog_product->addProduct($this->request->post);
$data['disable_cart']
has not been defined at all . so the addProduct
function will throw an exception.
The solution is to add this line of code:
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
// add this line to `add` and to `edit`
if(!isset($this->request->post['disable_cart'])) $this->request->post['disable_cart'] = 0;
$this->model_catalog_product->addProduct($this->request->post);
...
As for the warning, it is simply saying that you have a premature output due to the error being thrown.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.