这个想法很简单;我想让我的客户在他们访问单个产品时更容易添加相关产品。我的相关产品需要选项,因此也需要显示选项。
我认为我很接近显示部分,不知道如何使用ajax调用等添加到购物车。请注意我不是程序员,我经常使用PHP代码,所以我知道一点
我开始在控制器中编辑; 的目录/控制器/产品/ product.php
$data['products'] = array();
$results = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);
//adding the array that needs to be filled with product options
$data['daaf_options'] = array();
foreach ($results as $result) {
if ($result['image']) {
$image = $this->model_tool_image->resize($result['image'], $this->config->get($this->config->get('config_theme') . '_image_related_width'), $this->config->get($this->config->get('config_theme') . '_image_related_height'));
} else {
$image = $this->model_tool_image->resize('placeholder.png', $this->config->get($this->config->get('config_theme') . '_image_related_width'), $this->config->get($this->config->get('config_theme') . '_image_related_height'));
}
if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
} else {
$price = false;
}
if ((float)$result['special']) {
$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
} else {
$special = false;
}
if ($this->config->get('config_tax')) {
$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price'], $this->session->data['currency']);
} else {
$tax = false;
}
if ($this->config->get('config_review_status')) {
$rating = (int)$result['rating'];
} else {
$rating = false;
}
$data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
'rating' => $rating,
'href' => $this->url->link('product/product', 'product_id=' . $result['product_id'])
);
//getting the options
$daaf_getoptions = $this->model_catalog_product->getProductOptions($result['product_id']);
$related_product_option_value_data = array();
foreach ($daaf_getoptions as $related_option) {
foreach ($related_option['product_option_value'] as $related_option_value) {
$related_product_option_value_data[] = array(
'product_option_value_id' => $related_option_value['product_option_value_id'],
'option_value_id' => $related_option_value['option_value_id'],
'name' => $related_option_value['name']
);
}
$data['daaf_options'][] = array(
'product_option_id' => $related_option['product_option_id'],
'product_option_value' => $related_product_option_value_data,
'option_id' => $related_option['option_id'],
'name' => $related_option['name'],
'type' => $related_option['type'],
'value' => $related_option['value'],
'required' => $related_option['required']
);
}
}
然后我将选项添加到 /catalog/view/theme/default/template/product/product.tpl
中的相关产品 $product_options_center = $modules_old_opencart->getModules('product_options_center');
if( count($product_options_center) ) {
foreach ($product_options_center as $module) {
echo $module;
}
} ?>
<?php if ($daaf_options) { ?>
<div class="options">
<?php foreach ($daaf_options as $related_option) { ?>
<?php if ($related_option['type'] == 'select') { ?>
<div class="form-group<?php echo ($related_option['required'] ? ' required' : ''); ?>">
<label class="control-label" for="input-option<?php echo $related_option['product_option_id']; ?>"><?php echo $related_option['name']; ?></label>
<select name="option[<?php echo $related_option['product_option_id']; ?>]" id="input-option<?php echo $related_option['product_option_id']; ?>" class="form-control">
<option value=""><?php echo $text_select; ?></option>
<?php foreach ($related_option['product_option_value'] as $related_option_value) { ?>
<option value="<?php echo $related_option_value['product_option_value_id']; ?>"><?php echo $related_option_value['name']; ?>
</option>
<?php } ?>
</select>
</div>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
这就是现在的样子; http://imgur.com/a/RDS3V
示例中的产品有7个相关产品,它们都有1个必需选项。显然它出了问题,因为每个相关产品都从7个相关产品中获得了所有选项。
我做错了什么?
真诚地希望任何人都可以帮忙! 干杯, 大卫
答案 0 :(得分:0)
尝试将//getting options
代码移到$data['products'][] = array
之上,以便我们将其放在产品数组中。
$data['products'] = array();
$results = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);
foreach ($results as $result) {
if ($result['image']) {
$image = $this->model_tool_image->resize($result['image'], $this->config->get($this->config->get('config_theme') . '_image_related_width'), $this->config->get($this->config->get('config_theme') . '_image_related_height'));
} else {
$image = $this->model_tool_image->resize('placeholder.png', $this->config->get($this->config->get('config_theme') . '_image_related_width'), $this->config->get($this->config->get('config_theme') . '_image_related_height'));
}
if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
} else {
$price = false;
}
if ((float)$result['special']) {
$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
} else {
$special = false;
}
if ($this->config->get('config_tax')) {
$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price'], $this->session->data['currency']);
} else {
$tax = false;
}
if ($this->config->get('config_review_status')) {
$rating = (int)$result['rating'];
} else {
$rating = false;
}
//adding the array that needs to be filled with product options
$daaf_options = array();
//getting the options
$daaf_getoptions = $this->model_catalog_product->getProductOptions($result['product_id']);
$related_product_option_value_data = array();
foreach ($daaf_getoptions as $related_option) {
foreach ($related_option['product_option_value'] as $related_option_value) {
$related_product_option_value_data[] = array(
'product_option_value_id' => $related_option_value['product_option_value_id'],
'option_value_id' => $related_option_value['option_value_id'],
'name' => $related_option_value['name']
);
}
$daaf_options[] = array(
'product_option_id' => $related_option['product_option_id'],
'product_option_value' => $related_product_option_value_data,
'option_id' => $related_option['option_id'],
'name' => $related_option['name'],
'type' => $related_option['type'],
'value' => $related_option['value'],
'required' => $related_option['required']
);
}
$data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
'rating' => $rating,
'href' => $this->url->link('product/product', 'product_id=' . $result['product_id']),
'options' => $daaf_options //we put the options here
);
}
然后,您可以使用if ($product['options'])
从{strong> product.tpl 调用它(当然在foreach ($products as $product)
内)。