gpt4 book ai didi

php - Opencart:插入批量产品,product_id 不唯一

转载 作者:行者123 更新时间:2023-11-29 00:32:44 26 4
gpt4 key购买 nike

我刚看到 opencart 的一些扩展可以更新产品列表页面的产品,我在我的 opencart 项目中尝试了它,很有趣...现在我开始为我的 opencart 项目开发一个新的扩展,即: Insert Bulk product to admin...这一次最多可以添加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>

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

型号

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

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

谢谢...

最佳答案

您必须更新您的模型。检查 /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

关于php - Opencart:插入批量产品,product_id 不唯一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15785097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com