mans
mans

Reputation: 1087

Opencart: Insert bulk products, product_id not unique

i just saw some extensions from opencart that update product from product list page, i tried it my opencart project, intresting... now i started to develop a new extension for my opencart project that is: Insert Bulk product to admin... this can add up to 10 products one time, after fill product form just click Save button this 10 products can store to database... each product forms only with 5 following form fields,

i can use opencart default product edit page for add extra details/option/descriptions later, and when insert this bulk product only 3 following tables connect with my query,

I had tried some codes, my problem is when i click save button table product and product_description data insert function success, but product_id not unique from these both table, for example if product table product_id = 125 then product_description table product_id = 5, i need these 3 tables product_id must be same unique like our default opencart insert work... any idea..??

what i tried here.... Form

<input type="text" name="product_description[<?php echo $language['language_id']; ?>][name]" value="<?php echo isset($product_description[$language['language_id']]) ? $product_description[$language['language_id']]['name'] : ''; ?>" />
<input type="text" name="model" value=""  />
<input type="text" name="quantity" value=""  />
<input type="text" name="price" value=""  />
<select name="stock_status_id">
<?php foreach ($stock_statuses as $stock_status) { ?>
<?php if ($stock_status['stock_status_id'] == $stock_status_id) { ?>
<option value="<?php echo $stock_status['stock_status_id']; ?>" selected="selected"><?php echo $stock_status['name']; ?></option>
<?php } else { ?>
<option value="<?php echo $stock_status['stock_status_id']; ?>"><?php echo $stock_status['name']; ?></option>
<?php } ?>
<?php } ?>
</select>

Controller

public function simple_pu() {
    $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->validateSimplePu()) {
        $url = '';

        foreach ($this->request->post['selected'] as $product_id) {

        $price_str = $product_id.'_price';
        $quantity_str = $product_id.'_quantity';
        $model_str = $product_id.'_model';
        $name_str = $product_id.'_name';
        $status_str = $product_id.'_status';

        $price = $this->request->post[$price_str];
        $quantity = $this->request->post[$quantity_str];
        $model = $this->request->post[$model_str];
        $name = $this->request->post[$name_str];
        $status = $this->request->post[$status_str];

        $su_data = array('price' => $price, 'quantity' => $quantity, 'model' => $model, 'name' => $name, 'status' => $status);
        $this->model_catalog_product->editPrices($product_id, $su_data);
        }

        $this->redirect($this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url, 'SSL'));
    }
    $this->getListbulk();
}

Model

public function addBulkproduct($product_id, $su_data) {     
    if (isset($su_data['product'])) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "product SET price = '" . $this->db->escape($su_data['price']) . "', model = '" . $this->db->escape($su_data['model']) . "', quantity = '" . $this->db->escape($su_data['quantity']) . "', student_id = '" . (int)$student_id . "', date_added = NOW()");
    }

    if (isset($su_data['product_description'])) {
        $this->db->query("UPDATE " . DB_PREFIX . "product_description SET name = '" . $this->db->escape($su_data['name']) . "' WHERE product_id = '" . (int)$product_id . "'");
    }
}

thats all.... above code only a structure If you know any extensions related my question, you can suggest me...

thanks...

Upvotes: 1

Views: 4538

Answers (1)

shadyyx
shadyyx

Reputation: 16055

You have to update Your model. Check the /admin/model/catalog/product.php and the method addProduct() - after the product is inserted, You have to retrieve the $product_id, so in Your model it should be like this:

public function addBulkproduct($su_data) {     
    if (isset($su_data['product'])) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "product SET price = '" . $this->db->escape($su_data['price']) . "', model = '" . $this->db->escape($su_data['model']) . "', quantity = '" . $this->db->escape($su_data['quantity']) . "', student_id = '" . (int)$student_id . "', date_added = NOW()");

        $product_id = $this->db->getLastId();

        // INSERT INTO PRODUCT TO STORE
        $this->db->query("INSERT INTO " . DB_PREFIX . "product_store (store_id, product_id) SELECT store_id, " . (int)$product_id . " FROM " . DB_PREFIX . "store");

        if (isset($su_data['product_description'])) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET name = '" . $this->db->escape($su_data['name']) . "', product_id = '" . (int)$product_id . "'");
        }
    }
}

Notice that You are not passing the $product_id as a method parameter anymore - the product gets inserted, new ID gets generated and for this ID the description is stored. Also, the product description is not updated but inserted as new, too! Product description is stored only if a product is inserted - it would have no sense to store a product description even there is no data for product...

And You should set the language_id for the product description row, too.

Upvotes: 1

Related Questions