1

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

  • 价格
  • 数量
  • 模型
  • 姓名
  • 地位

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

  • 产品
  • 产品描述
  • 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 . "'");
    }
}

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

谢谢...

4

1 回答 1

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为产品描述行设置。

于 2013-04-03T11:30:17.220 回答