Post by msangani » Wed Jul 11, 2018 1:58 pm

I want to add options for related products as well. So far I'm able to show the options for related products, however when I click on "Add to cart" for related products, the option values aren't getting added to cart.

Any help will be greatly appreciated.

Test URL: http://www.teakfurniturecenter.com/inde ... uct_id=156

I made following changes to catalog/view/theme/default/template/product/product.php

$results = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);

foreach ($results as $result) {
// CUSTOM CODE START: Options for related product
$data['relatedoptions'] = array();
foreach ($this->model_catalog_product->getProductOptions($result['product_id']) as $option) {
$related_product_option_value_data = array();

foreach ($option['product_option_value'] as $option_value) {
if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
$price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax') ? 'P' : false), $this->session->data['currency']);
} else {
$price = false;
}

$related_product_option_value_data[] = array(
'product_option_value_id' => $option_value['product_option_value_id'],
'option_value_id' => $option_value['option_value_id'],
'name' => $option_value['name'],
'image' => $this->model_tool_image->resize($option_value['image'], 50, 50),
'price' => $price,
'price_prefix' => $option_value['price_prefix']
);
}
}

$data['relatedoptions'][] = array(
'product_option_id' => $option['product_option_id'],
'product_option_value' => $related_product_option_value_data,
'option_id' => $option['option_id'],
'name' => $option['name'],
'type' => $option['type'],
'value' => $option['value'],
'required' => $option['required']
);
}
// CUSTOM END: Options for related product

I then made changes to catalog/view/theme/default/template/product/product.twig

<button type="button" id="button-cart" data-loading-text="{{ text_loading }}" class="btn btn-primary btn-lg btn-block"><i class="fa fa-shopping-cart"></i> &nbsp; {{ button_cart }}</button>
</div>

<!-- CUSTOM START: Fabric for furniture -->
{% if products %}
{% for product in products %}
<h3>{{ product.name }}</h3>
{{ product.description }}<br><br>
{% if relatedoptions %}
{% for option in relatedoptions %}
{% if option.type == 'select' %}
<div class="form-group{% if option.required %} required {% endif %}">
<label class="control-label" for="input-option{{ option.product_option_id }}">{{ option.name }}</label>
<select name="option[{{ option.product_option_id }}]" id="input-option{{ option.product_option_id }}" class="form-control">
<option value="">{{ text_select }}</option>
{% for option_value in option.product_option_value %}
<option value="{{ option_value.product_option_value_id }}">{{ option_value.name }}
{% if option_value.price %}
({{ option_value.price_prefix }}{{ option_value.price }})
{% endif %} </option>
{% endfor %}
</select>
</div>
{% endif %}
{% endfor %}
{% endif %}
<div class="button-group">
<button type="button" onclick="cart.add('{{ product.product_id }}', '{{ product.minimum }}');"><span class="hidden-xs hidden-sm hidden-md">{{ button_cart }}</span> <i class="fa fa-shopping-cart"></i></button>
</div>
{% endfor %}
{% endif %}
<!-- CUSTOM END: Fabric for furniture -->

New member

Posts

Joined
Sat Jan 17, 2015 1:12 pm

Post by msangani » Wed Jul 11, 2018 9:08 pm

I worked on this, and found the culprit is below line where $option needs to be passed to cart.add as 3rd parameter (product.twig). Still don't know how to pass an array. If someone can you please guide me.

<button type="button" onclick="cart.add('{{ product.product_id }}', '{{ product.minimum }}');"><span class="hidden-xs hidden-sm hidden-md">{{ button_cart }}</span> <i class="fa fa-shopping-cart"></i></button>

New member

Posts

Joined
Sat Jan 17, 2015 1:12 pm
Who is online

Users browsing this forum: No registered users and 11 guests