Opencart:插入批量产品,product_id不是唯一的

时间:2013-04-03 10:38:11

标签: php mysql e-commerce opencart

我刚刚看到opencart的一些扩展,从产品列表页面更新了产品,我尝试了我的opencart项目,有趣...现在我开始为我的opencart项目开发一个新的扩展, :将批量产品插入管理员...这一次最多可以添加10个产品,填写产品表单后点击保存按钮这10个产品可以存储到数据库...每个产品只能形成5个在表单字段之后,

  • 模型
  • 名称
  • 状态

我可以使用opencart默认产品编辑页面稍后添加额外的详细信息/选项/说明,当插入此批量产品时,只有3个以下表格连接我的查询,

  • 产品
  • PRODUCT_DESCRIPTION
  • product_to_store

我尝试了一些代码,我的问题是当我点击保存按钮表productproduct_description数据插入功能成功,但product_id并非这两个表都是唯一的,例如productproduct_id = 125然后product_descriptionproduct_id = 5,我需要这3个表 product_id 必须与我们默认的opencart插入工作一样唯一...任何想法.. ??

我在这里尝试了...... 的表格

<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>

控制器

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();
}

模型

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 . "'");
    }
}

那就是.... 上面的代码只有一个结构如果你知道任何与我的问题有关的扩展,你可以建议我......

感谢...

1 个答案:

答案 0 :(得分:1)

您必须更新您的型号。检查/admin/model/catalog/product.php和方法addProduct() - 插入产品后,您必须检索$product_id,因此在您的模型中应该是这样的:

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 . "'");
        }
    }
}

请注意,您不再将$product_id作为方法参数传递 - 插入产品,生成新ID,并为此ID存储说明。此外,产品说明不会更新,但也会以新的形式插入!只有在插入产品时才会存储产品描述 - 即使没有产品数据,也无法存储产品描述......

您也应该为产品说明行设置language_id