Page 1 of 1

[Solved] Can't apply a manufacturer coupon - OC 3.0.2.0

Posted: Fri Jan 21, 2022 8:00 pm
by Smucka
Hello,

I've successfully edited files neccessary for creating, editing and deleting a coupon by manufacturer. I've inserted a few tables in my DB and I think everything should be working fine. The problem I'm having is that the coupon can't be applied to any product / manufacturer. Website gives me a warning:

Code: Select all

"Warning: The coupon is either invalid, expired, or has reached the spending limit!"
I've checked my error_log file and don't see any errors.

I'm a bit suspicious of my DB, because it seems to me, that the system can't read / find the created coupon or it's somehow lost in translation. I went and checked my DB and can see the created coupon.

Any ideas on how to solve this?

Related thread: viewtopic.php?f=202&t=227006

Thanks!

Re: Can't apply a manufacturer coupon - OC 3.0.2.0

Posted: Fri Jan 21, 2022 9:29 pm
by Gergely
Hi Smucka,

It seems you have successfully duplicated the admin functionality of the vanilla coupon feature, with the intent to create a new type of coupon that would be restricted per manufacturer, is that correct? Have you done the same for the catalog side?

The default OC coupon is defined in extension/total, you can follow that as a guide to implement your coupon's functionality.

Hope this helps,
Gergely

Re: Can't apply a manufacturer coupon - OC 3.0.2.0

Posted: Mon Jan 31, 2022 5:47 pm
by Smucka
Sorry for the late reply, I was out of office.

That is correct.
I've checked all files and still can't seem to figure it out.

Thanks!

Re: Can't apply a manufacturer coupon - OC 3.0.2.0

Posted: Fri Feb 18, 2022 5:21 pm
by Smucka
I've noticed a PHP fatal error. Could this error do something with not applying a coupon?

Code: Select all

[09-Feb-2022 12:22:38 UTC] PHP Fatal error:  Uncaught Exception: Error: Unknown column 'ca.customer_activity_id' in 'field list'<br />
Error No: 1054<br />
SELECT ca.customer_activity_id, ca.customer_id, ca.key, ca.data, ca.ip, ca.date_added 
FROM me_customer_activity ca LEFT JOIN me_customer c ON (ca.customer_id = c.customer_id) 
ORDER BY ca.date_added 
DESC LIMIT 0,20 in .../system/library/db/mysqli.php:40

Stack trace:
#0 .../system/library/db.php(45): DB\MySQLi->query('SELECT ca.custo...')
#1 .../admin/model/extension/report/customer.php(264): DB->query('SELECT ca.custo...')
#2 .../vqmod/vqcache/vq2-system_storage_modification_system_engine_loader.php(259): ModelExtensionReportCustomer->getCustomerActivities(Array)
#3 .../system/engine/proxy.php(47): Loader->{closure}(Array, Array)
#4 /home/.../subdomain in .../system/library/db/mysqli.php on line 40
Thanks!

Re: Can't apply a manufacturer coupon - OC 3.0.2.0

Posted: Tue Feb 22, 2022 4:16 pm
by Smucka
I somehow found the error I'm getting but don't really know how to solve it.

Code: Select all

PHP Fatal error:  Uncaught Exception: Error: Unknown column 'manufacturer_id' in 'where clause'<br />
Error No: 1054<br />
SELECT COUNT(*) AS total FROM `me_product_to_manufacturer` WHERE `product_id` = '404' AND manufacturer_id = '0' in .../system/library/db/mysqli.php:40

Stack trace:
#0 .../system/library/db.php(45): DB\MySQLi->query('SELECT COUNT(*)...')
#1 .../catalog/model/extension/total/coupon.php(95): DB->query('SELECT COUNT(*)...')
#2 .../vqmod/vqcache/vq2-system_storage_modification_system_engine_loader.php(259): ModelExtensionTotalCoupon->getCoupon('afroxy')
#3 .../system/engine/proxy.php(47): Loader->{closure}(Array, Array)
#4 .../catalog/controller/extension/total/coupon.php(30): Proxy->__call('getCoupon', Array)
#5 /home/... in .../system/library/db/mysqli.php on line 40
I created the table (me_product_to_manufacturer) and the only difference between "me_product_to_manufacturer" and "me_product_to_category" I can see is "cardinality". What am I missing?

Stack trace #2 is also pretty weird since I don't have a coupon named "afroxy" anymore.

I know I'm missing something so any help would be greatly appreciated. If you need more info please let me know what exactly do you need and I'll provide it.

Thanks!

Re: Can't apply a manufacturer coupon - OC 3.0.2.0

Posted: Tue Feb 22, 2022 5:14 pm
by xxvirusxx
You have created coupon_manufacturer like coupon_category ?

Re: Can't apply a manufacturer coupon - OC 3.0.2.0

Posted: Tue Feb 22, 2022 5:24 pm
by Smucka
Correct.

Re: Can't apply a manufacturer coupon - OC 3.0.2.0

Posted: Tue Feb 22, 2022 7:56 pm
by xxvirusxx
I don't understand why is so hard do duplicate category coupon code and rename it to manufacturer coupon.

These are the files

Code: Select all

admin/language/en-gb/marketing/coupon.php
admin/controller/marketing/coupon.php
admin/model/marketing/coupon.php
admin/view/template/marketing/coupon_form.twig
catalog/model/extension/total/coupon.php

Re: Can't apply a manufacturer coupon - OC 3.0.2.0

Posted: Tue Feb 22, 2022 8:42 pm
by Smucka
I've edited all these files.
I've been thinking, ... should I edit any other files if I'm using Journal 2.xx.x?

Code: Select all

admin/language/en-gb/marketing/coupon.php

Code: Select all

// Entry
.
.
.
$_['entry_category']      = 'Category';
$_['entry_manufacturer']  = 'Manufacturer';
.
.
.

Code: Select all

admin/controller/marketing/coupon.php

Code: Select all

if (isset($this->request->post['coupon_category'])) {
		$categories = $this->request->post['coupon_category'];
	} elseif (isset($this->request->get['coupon_id'])) {
		$categories = $this->model_marketing_coupon->getCouponCategories($this->request->get['coupon_id']);
	} else {
		$categories = array();
	}

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

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

	foreach ($categories as $category_id) {

		$category_info = $this->model_catalog_category->getCategory($category_id);

		if ($category_info) {
			$data['coupon_category'][] = array(
				'category_id' => $category_info['category_id'],
				'name'        => ($category_info['path'] ? $category_info['path'] . ' &gt; ' : '') . $category_info['name']
			);
		}
	}
if (isset($this->request->post['coupon_manufacturer'])) {
		$manufacturer = $this->request->post['coupon_manufacturer'];
	} elseif (isset($this->request->get['coupon_id'])) {
		$manufacturer = $this->model_marketing_coupon->getCouponManufacturer($this->request->get['coupon_id']);
	} else {
		$manufacturer = array();
	}

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

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

	foreach ($manufacturer as $manufacturer_id) {
		$manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($manufacturer_id);

		if ($manufacturer_info) {
			$data['coupon_manufacturer'][] = array(
				'manufacturer_id' => $manufacturer_info['manufacturer_id'],
				'name'       	  => ($manufacturer_info['path'] ? $manufacturer_info['path'] . ' &gt; ' : '') . $manufacturer_info['name']
			);
		}
	}

Code: Select all

admin/model/marketing/coupon.php

Code: Select all

if (isset($data['coupon_category'])) {
	foreach ($data['coupon_category'] as $category_id) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "coupon_category SET coupon_id = '" . (int)$coupon_id . "', category_id = '" . (int)$category_id . "'");
	}
}

if (isset($data['coupon_manufacturer'])) {
	foreach ($data['coupon_manufacturer'] as $manufacturer_id) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "coupon_manufacturer SET coupon_id ='" . (int)$coupon_id . "', manufacturer_id = '" . (int)$manufacturer_id . "'");
	}
}

$this->db->query("DELETE FROM " . DB_PREFIX . "coupon_category WHERE coupon_id = '" . (int)$coupon_id . "'");

if (isset($data['coupon_category'])) {
	foreach ($data['coupon_category'] as $category_id) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "coupon_category SET coupon_id = '" . (int)$coupon_id . "', category_id = '" . (int)$category_id . "'");
	}
}

$this->db->query("DELETE FROM " . DB_PREFIX . "coupon_manufacturer WHERE coupon_id = '" . (int)$coupon_id . "'");

if (isset($data['coupon_manufacturer'])) {
	foreach ($data['coupon_manufacturer'] as $manufacturer_id) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "coupon_manufacturer SET coupon_id = '" . (int)$coupon_id . "', manufacturer_id = '" . (int)$manufacturer_id . "'");
	}
}

public function getCouponCategories($coupon_id) {
	$coupon_category_data = array();

	$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "coupon_category WHERE coupon_id = '" . (int)$coupon_id . "'");

	foreach ($query->rows as $result) {
		$coupon_category_data[] = $result['category_id'];
	}

	return $coupon_category_data;
}

public function getCouponManufacturer($coupon_id) {
	$coupon_manufacturer_data = array();

	$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "coupon_manufacturer WHERE coupon_id = '" . (int)$coupon_id . "'");

	foreach ($query->rows as $result) {
		$coupon_manufacturer_data[] = $result['manufacturer_id'];
	}

	return $coupon_manufacturer_data;
}

Code: Select all

admin/view/template/marketing/coupon_form.twig

Code: Select all

<div class="form-group">
              <label class="col-sm-2 control-label" for="input-category"><span data-toggle="tooltip" title="{{ help_category }}">{{ entry_category }}</span></label>
                <div class="col-sm-10">
                  <input type="text" name="category" value="" placeholder="{{ entry_category }}" id="input-category" class="form-control" />
                  <div id="coupon-category" class="well well-sm" style="height: 150px; overflow: auto;"> {% for coupon_category in coupon_category %}
                    <div id="coupon-category{{ coupon_category.category_id }}"><i class="fa fa-minus-circle"></i> {{ coupon_category.name }}
                      <input type="hidden" name="coupon_category[]" value="{{ coupon_category.category_id }}" />
                    </div>
                    {% endfor %} </div>
                </div>
              </div>
              
              <div class="form-group">
             <label class="col-sm-2 control-label" for="input-manufacturer"><span data-toggle="tooltip" title="{{ help_manufacturer }}">{{ entry_manufacturer }}</span></label>
                <div class="col-sm-10">
                  <input type="text" name="manufacturer" value="" placeholder="{{ entry_manufacturer }}" id="input-manufacturer" class="form-control" />
                  <div id="coupon-manufacturer" class="well well-sm" style="height: 150px; overflow: auto;"> {% for coupon_manufacturer in coupon_manufacturer %}
                    <div id="coupon-manufacturer{{ coupon_manufacturer.manufacturer_id }}"><i class="fa fa-minus-circle"></i> {{ coupon_manufacturer.name }}
                      <input type="hidden" name="coupon_manufacturer[]" value="{{ coupon_manufacturer.manufacturer_id }}" />
                    </div>
                    {% endfor %} </div>
                </div>
              </div>
              
// Category
$('input[name=\'category\']').autocomplete({
	'source': function(request, response) {
		$.ajax({
			url: 'index.php?route=catalog/category/autocomplete&user_token={{ user_token }}&filter_name=' +  encodeURIComponent(request),
			dataType: 'json',
			success: function(json) {
				response($.map(json, function(item) {
					return {
						label: item['name'],
						value: item['category_id']
					}
				}));
			}
		});
	},
	'select': function(item) {
		$('input[name=\'category\']').val('');
		
		$('#coupon-category' + item['value']).remove();
		
		$('#coupon-category').append('<div id="coupon-category' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="coupon_category[]" value="' + item['value'] + '" /></div>');
	}	
});

$('#coupon-category').delegate('.fa-minus-circle', 'click', function() {
	$(this).parent().remove();
});

// Manufacturer 
$('input[name=\'manufacturer\']').autocomplete({
	'source': function(request, response) {
		$.ajax({
			url: 'index.php?route=catalog/manufacturer/autocomplete&user_token={{ user_token }}&filter_name=' +  encodeURIComponent(request),
			dataType: 'json',
			success: function(json) {
				response($.map(json, function(item) {
					return {
						label: item['name'],
						value: item['manufacturer_id']
					}
				}));
			}
		});
	},
	'select': function(item) {
		$('input[name=\'manufacturer\']').val('');
		
		$('#coupon-manufacturer' + item['value']).remove();
		
		$('#coupon-manufacturer').append('<div id="coupon-manufacturer' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="coupon_manufacturer[]" value="' + item['value'] + '" /></div>');
	}	
}); 

Code: Select all

catalog/model/extension/total/coupon.php

Code: Select all

// Categories
			$coupon_category_data = array();

			$coupon_category_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "coupon_category` cc LEFT JOIN `" . DB_PREFIX . "category_path` cp ON (cc.category_id = cp.path_id) WHERE cc.coupon_id = '" . (int)$coupon_query->row['coupon_id'] . "'");

			foreach ($coupon_category_query->rows as $category) {
				$coupon_category_data[] = $category['category_id'];
			}

			$product_data = array();

			if ($coupon_product_data || $coupon_category_data) {
				foreach ($this->cart->getProducts() as $product) {
					if (in_array($product['product_id'], $coupon_product_data)) {
						$product_data[] = $product['product_id'];

						continue;
					}

					foreach ($coupon_category_data as $category_id) {
						$coupon_category_query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "product_to_category` WHERE `product_id` = '" . (int)$product['product_id'] . "' AND category_id = '" . (int)$category_id . "'");

						if ($coupon_category_query->row['total']) {
							$product_data[] = $product['product_id'];

							continue;
						}
					}
				}

				if (!$product_data) {
					$status = false;
				}
			}

			// Manufacturers
			$coupon_manufacturer_data = array();

			$coupon_manufacturer_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "coupon_manufacturer` cc LEFT JOIN `" . DB_PREFIX . "manufacturer_path` cp ON (cc.manufacturer_id = cp.path_id) WHERE cc.coupon_id = '" . (int)$coupon_query->row['coupon_id'] . "'");

			foreach ($coupon_manufacturer_query->rows as $manufacturer) {
				$coupon_manufacturer_data[] = $manufacturer['manufacturer_id'];
			}

			$product_data = array();

			if ($coupon_product_data || $coupon_manufacturer_data) {
				foreach ($this->cart->getProducts() as $product) {
					if (in_array($product['product_id'], $coupon_product_data)) {
						$product_data[] = $product['product_id'];

						continue;
					}

					foreach ($coupon_manufacturer_data as $manufacturer_id) {
						$coupon_manufacturer_query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "product_to_manufacturer` WHERE `product_id` = '" . (int)$product['product_id'] . "' AND manufacturer_id = '" . (int)$manufacturer_id . "'");

						if ($coupon_manufacturer_query->row['total']) {
							$product_data[] = $product['product_id'];

							continue;
						}
					}
				}

				if (!$product_data) {
					$status = false;
				}
			}

Re: Can't apply a manufacturer coupon - OC 3.0.2.0

Posted: Tue Feb 22, 2022 9:15 pm
by xxvirusxx
Change this

Code: Select all

if (isset($this->request->post['coupon_manufacturer'])) {
		$manufacturer = $this->request->post['coupon_manufacturer'];
	} elseif (isset($this->request->get['coupon_id'])) {
		$manufacturer = $this->model_marketing_coupon->getCouponManufacturer($this->request->get['coupon_id']);
	} else {
		$manufacturer = array();
	}
to this

Code: Select all

if (isset($this->request->post['coupon_manufacturer'])) {
		$manufacturers = $this->request->post['coupon_manufacturer'];
	} elseif (isset($this->request->get['coupon_id'])) {
		$manufacturers = $this->model_marketing_coupon->getCouponManufacturer($this->request->get['coupon_id']);
	} else {
		$manufacturers = array();
	}

this

Code: Select all

foreach ($manufacturer as $manufacturer_id) {
to this

Code: Select all

foreach ($manufacturers as $manufacturer_id) {
this

Code: Select all

$coupon_manufacturer_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "coupon_manufacturer` cc LEFT JOIN `" . DB_PREFIX . "manufacturer_path` cp ON (cc.manufacturer_id = cp.path_id) WHERE cc.coupon_id = '" . (int)$coupon_query->row['coupon_id'] . "'");

to:

Code: Select all

$coupon_manufacturer_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "coupon_manufacturer` WHERE coupon_id = '" . (int)$coupon_query->row['coupon_id'] . "'");
and this

Code: Select all

$coupon_manufacturer_query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "product_to_manufacturer` WHERE `product_id` = '" . (int)$product['product_id'] . "' AND manufacturer_id = '" . (int)$manufacturer_id . "'");
to this

Code: Select all

$coupon_manufacturer_query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "product` WHERE `product_id` = '" . (int)$product['product_id'] . "' AND manufacturer_id = '" . (int)$manufacturer_id . "'");

Re: Can't apply a manufacturer coupon - OC 3.0.2.0

Posted: Tue Feb 22, 2022 10:21 pm
by Smucka
I've changed the code and it works flawlessly. Huge thanks! Shoot me a PM to let me know where can I donate you for a beer!