Post by ajemptage1 » Mon Oct 29, 2018 11:27 pm

Hi there,
I am developing a mod for our OpenCart 2.3.0.2 stores, and am running into a bit of a problem.
The mod needs to request product details from store A and return them to be displayed on store B, but I assume because it is going cross-domain, it is returning the "No 'Access-Control-Allow-Origin' header is present on the requested resource" error in console.
The mod will be displayed on a subdomain which is setup as a separate store in our multi-store installation, this has to be done for a variety of back-end reasons, unfortunately.

Can anyone point me in the right direction to get this figured out?

Here is the request:

Code: Select all

for (var key in paints) {
        $.ajax({
            url: 'http://www.ecospaintsdev.com/index.php?route=product/selectable_products/info&product_id=' + paints[key]["id"],
            type: 'get',
            dataType: 'text',
            success: function(responseText) {
                document.getElementById('products-container').insertAdjacentHTML('beforeend', responseText);
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    };
And here is the controller:

Code: Select all

<?php
class ControllerProductSelectableProducts extends Controller {
	private $error = array();

	public function info() {
		if (isset($this->request->get['product_id'])) {
			$product_id = (int)$this->request->get['product_id'];
		} else {
			$product_id = 0;
		}

		$this->load->model('catalog/product');

		$product_info = $this->model_catalog_product->getProduct($product_id);

		if ($product_info) {

			$data['product_url'] = $this->url->link('product/product', $url . '&product_id=' . $this->request->get['product_id']);
			$data['product_name'] = $product_info['name'];

			$this->load->model('catalog/review');

			$data['product_id'] = (int)$this->request->get['product_id'];
			$data['manufacturer'] = $product_info['manufacturer'];
			$data['manufacturers'] = $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $product_info['manufacturer_id']);
			$data['model'] = $product_info['model'];
			$data['reward'] = $product_info['reward'];
			$data['points'] = $product_info['points'];
			$data['description'] = html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8');

			if ($product_info['quantity'] <= 0) {
				$data['stock'] = $product_info['stock_status'];
			} elseif ($this->config->get('config_stock_display')) {
				$data['stock'] = $product_info['quantity'];
			} else {
				$data['stock'] = $this->language->get('text_instock');
			}

			$this->load->model('tool/image');

			if ($product_info['image']) {
				$data['popup'] = $this->model_tool_image->resize($product_info['image'], $this->config->get($this->config->get('config_theme') . '_image_popup_width'), $this->config->get($this->config->get('config_theme') . '_image_popup_height'));
			} else {
				$data['popup'] = '';
			}

			if ($product_info['image']) {
				$data['thumb'] = $this->model_tool_image->resize($product_info['image'], $this->config->get($this->config->get('config_theme') . '_image_thumb_width'), $this->config->get($this->config->get('config_theme') . '_image_thumb_height'));
			} else {
				$data['thumb'] = '';
			}

			$data['images'] = array();

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

			foreach ($results as $result) {
				$data['images'][] = array(
					'popup' => $this->model_tool_image->resize($result['image'], $this->config->get($this->config->get('config_theme') . '_image_popup_width'), $this->config->get($this->config->get('config_theme') . '_image_popup_height')),
					'thumb' => $this->model_tool_image->resize($result['image'], $this->config->get($this->config->get('config_theme') . '_image_additional_width'), $this->config->get($this->config->get('config_theme') . '_image_additional_height'))
				);
			}

			if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
				$data['price'] = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
			} else {
				$data['price'] = false;
			}

			if ((float)$product_info['special']) {
				$data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
			} else {
				$data['special'] = false;
			}

			if ($this->config->get('config_tax')) {
				$data['tax'] = $this->currency->format((float)$product_info['special'] ? $product_info['special'] : $product_info['price'], $this->session->data['currency']);
			} else {
				$data['tax'] = false;
			}

			$discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);

			$data['discounts'] = array();

			foreach ($discounts as $discount) {
				$data['discounts'][] = array(
					'quantity' => $discount['quantity'],
					'price'    => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'])
				);
			}

			$data['options'] = array();

			foreach ($this->model_catalog_product->getProductOptions($this->request->get['product_id']) as $option) {
				$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;
						}

						$data['text_price'] = 'Image';
						$data['text_image'] = 'Price';
						
						if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price'))) {
							if (version_compare(VERSION, '2.2.0.0', '<')) {
								if ($product_info['special']) {
									if ($option_value['price_prefix'] == '+') {
										$total_price = $this->currency->format($this->tax->calculate($option_value['price'] + $product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
									} elseif ($option_value['price_prefix'] == '-') {
										$total_price = $this->currency->format($this->tax->calculate($product_info['special'] - $option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
									} else {
										$total_price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
									}
								} else {
									if ($option_value['price_prefix'] == '+') {
										$total_price = $this->currency->format($this->tax->calculate($option_value['price'] + $product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
									} elseif ($option_value['price_prefix'] == '-') {
										$total_price = $this->currency->format($this->tax->calculate($product_info['price'] - $option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
									} else {
										$total_price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
									}
								}
							} else {
								if ($product_info['special']) {
									if ($option_value['price_prefix'] == '+') {
										$total_price = $this->currency->format($this->tax->calculate($option_value['price'] + $product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
									} elseif ($option_value['price_prefix'] == '-') {
										$total_price = $this->currency->format($this->tax->calculate($product_info['special'] - $option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
									} else {
										$total_price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
									}
								} else {
									if ($option_value['price_prefix'] == '+') {
										$total_price = $this->currency->format($this->tax->calculate($option_value['price'] + $product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
									} elseif ($option_value['price_prefix'] == '-') {
										$total_price = $this->currency->format($this->tax->calculate($product_info['price'] - $option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
									} else {
										$total_price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
									}
								}
							}
						} else {
							$total_price = false;
						}
						$product_option_value_data[] = array(
							'total_price'	=> $total_price,
							'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['options'][] = array(
					'product_option_id'    => $option['product_option_id'],
					'product_option_value' => $product_option_value_data,
					'option_id'            => $option['option_id'],
					'name'                 => $option['name'],
					'type'                 => $option['type'],
					'value'                => $option['value'],
					'required'             => $option['required']
				)

				if ($option['name'] == 'Paint Color') {
					$data['color_option'] = $option['product_option_id'];
				}

				if ($option['type'] == 'multiple') {
					$data['size_option'] = $option['product_option_id'];
					
					foreach ($option['product_option_value'] as $option_value) {
						if ($option_value['name'] == 'Quart') {
							$data['quart_option'] = $option_value['product_option_value_id'];
							$data['quart_price'] = $option_value['total_price'];
						}

						if ($option_value['name'] == 'Gallon') {
							$data['gallon_option'] = $option_value['product_option_value_id'];
							$data['gallon_price'] = $option_value['total_price'];
						}

						if ($option_value['name'] == '5 Gallon') {
							$data['five_gallon_option'] = $option_value['product_option_value_id'];
							$data['five_gallon_price'] = $option_value['total_price'];
						};
					};
				};
			}

			if ($product_info['minimum']) {
				$data['minimum'] = $product_info['minimum'];
			} else {
				$data['minimum'] = 1;
			}

			$data['review_status'] = $this->config->get('config_review_status');

			if ($this->config->get('config_review_guest') || $this->customer->isLogged()) {
				$data['review_guest'] = true;
			} else {
				$data['review_guest'] = false;
			}

			if ($this->customer->isLogged()) {
				$data['customer_name'] = $this->customer->getFirstName() . '&nbsp;' . $this->customer->getLastName();
			} else {
				$data['customer_name'] = '';
			}

			$data['reviews'] = sprintf($this->language->get('text_reviews'), (int)$product_info['reviews']);
			$data['rating'] = (int)$product_info['rating'];

			// Captcha
			if ($this->config->get($this->config->get('config_captcha') . '_status') && in_array('review', (array)$this->config->get('config_captcha_page'))) {
				$data['captcha'] = $this->load->controller('extension/captcha/' . $this->config->get('config_captcha'));
			} else {
				$data['captcha'] = '';
			}

			$data['share'] = $this->url->link('product/product', 'product_id=' . (int)$this->request->get['product_id']);

			$data['attribute_groups'] = $this->model_catalog_product->getProductAttributes($this->request->get['product_id']);

			$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;
				}

				$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,
					'short_desc'      => $result['short_desc'],
      				'gallon_coverage' => $result['gallon_coverage'],
          	  		'number_coats'    => $result['number_coats'],
					'href'        => $this->url->link('product/product', 'product_id=' . $result['product_id'])
				);
			}

			$this->response->setOutput($this->load->view('product/selectable_products', $data));
		}
	}
}
And the template:

Code: Select all

<button class="product-name-accordion">
  <i style="visibility: hidden; font-size: 26px; float: left; color: #fff;" id="selected-product-name-check" class="fa fa-check-square" aria-hidden="true"></i><?php echo $product_name; ?><i style="font-size: 26px; float: right;" class="fa fa-caret-down"></i>
</button>
<div class="paint-product-panel product-accordion-panel" id="<?php echo $product_id; ?>" colorid="<?php echo $color_option; ?>" sizeid="[<?php echo $size_option; ?>]">
  <a href="<?php echo $product_url; ?>" target="_blank"><img src="<?php echo $thumb; ?>"></a>
  <p>
    <span style="font-size: 18px !important; font-weight: bold;"><?php echo $product_name; ?></span><br />
    <?php echo $short_desc; ?><br />
    <span style="float: right; font-size: 12px;"><a href="<?php echo $product_url; ?>" target="_blank">See more</a></span>
  </p>
  <div class="form-group">
    <div style="display: inline-block; width: 70px;">Quarts </div>
    <input type="number" min="0" name="<?php echo $quart_option; ?>" value="0" price="<?php echo $quart_price; ?>" id="<?php echo $product_id; ?>-Quarts" />
    <small> $<?php echo $quart_price; ?> / Each</small>
  </div>
  <div class="form-group">
    <div style="display: inline-block; width: 70px;">Gallons </div>
    <input type="number" min="0" name="<?php echo $gallon_option; ?>" value="0" price="<?php echo $gallon_price; ?>" id="<?php echo $product_id; ?>-Gallons" />
    <small> $<?php echo $gallon_price; ?> / Each</small>
  </div>
  <?php if ($five_gallon_option) { ?>
  <div class="form-group">
    <div style="display: inline-block; width: 70px;">5 Gallon </div>
    <input type="number" min="0" name="<?php echo $five_gallon_option; ?>" value="0" price="<?php echo $five_gallon_price; ?>" id="<?php echo $product_id; ?>-fiveGallons">
    <small> $<?php echo $five_gallon_price; ?> / Each</small>
  </div>
  <?php } ?>
</div>

Newbie

Posts

Joined
Tue Mar 13, 2018 8:43 pm

Post by straightlight » Mon Nov 05, 2018 12:51 am

It would be suggested to post your service request in the Commercial Support section of the forum to get this done as a custom job.

The most generated errors being found on Opencart forum originates from contributed programming. The increased counters are caused by posted redundancies of the same solutions that were already provided prior.

F. Rules:

- viewtopic.php?f=176&t=200480
- viewtopic.php?f=176&t=200804


Regards,
Straightlight


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 4 guests