<?xml version="1.0" encoding="utf-8" ?>
<modification>
    
	<name>Restrict Shipping Methods</name>
    <code>restrict_shipping</code>
	<version>v303.2</version>
	<author>Clear Thinking, LLC</author>
	<link>http://www.getclearthinking.com/contact?inquiry=Question regarding Restrict Shipping Methods: </link>
	
	<!-- All OpenCart versions -->
	<file path="catalog/model/*/extension.php">
		<operation error="skip">
			<search trim="true"><![CDATA[
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "extension WHERE `type` = '" . $this->db->escape($type) . "'");
			]]></search>
			<add position="after" trim="true"><![CDATA[
				// Clear Thinking: Restrict Shipping Methods
				$prefix = (version_compare(VERSION, '3.0', '<')) ? '' : 'module_';
				if ($type == 'shipping' && $this->config->get($prefix . 'restrict_shipping_status')) {
					$this->load->model('extension/module/restrict_shipping');
					$query->rows = $this->model_extension_module_restrict_shipping->restrict($query->rows);
				}
				// end
			]]></add>
		</operation>
	</file>
	
	<!-- Individual Shipping extension -->
	<file path="catalog/model/{shipping,extension/shipping}/individual_shipping.php">
		<operation error="skip">
			<search index="1" trim="true"><![CDATA[
				$shipping_methods = $this->db->query("SELECT * FROM " . DB_PREFIX . "extension WHERE `type` = 'shipping' ORDER BY `code` ASC")->rows;
			]]></search>
			<add position="after" trim="true"><![CDATA[
				// Clear Thinking: Restrict Shipping Methods
				$prefix = (version_compare(VERSION, '3.0', '<')) ? '' : 'module_';
				if ($this->config->get($prefix . 'restrict_shipping_status')) {
					$this->load->model('extension/module/restrict_shipping');
					$shipping_methods = $this->model_extension_module_restrict_shipping->restrict($shipping_methods);
				}
				// end
			]]></add>
		</operation>
	</file>
	
	<!-- One-Page Checkout extension -->
	<file path="catalog/model/onecheckout/checkout.php">
		<operation error="skip">
			<search trim="true"><![CDATA[
				return $query->rows;
			]]></search>
			<add position="before" trim="true"><![CDATA[
				// Clear Thinking: Restrict Shipping Methods
				$prefix = (version_compare(VERSION, '3.0', '<')) ? '' : 'module_';
				if ($type == 'shipping' && $this->config->get($prefix . 'restrict_shipping_status')) {
					$this->load->model('extension/module/restrict_shipping');
					$query->rows = $this->model_extension_module_restrict_shipping->restrict($query->rows);
				}
				// end
			]]></add>
		</operation>
	</file>
	
	<!-- Disable individual shipping rates -->
	<file path="catalog/*/*/*.php|catalog/*/*/*/*.php">
		<operation error="skip">
			<search trim="true"><![CDATA[
				if ($quote) {
			]]></search>
			<add position="after" trim="true"><![CDATA[
				// Clear Thinking: Restrict Shipping Methods
				$prefix = (version_compare(VERSION, '3.0', '<')) ? '' : 'module_';
				if ($this->config->get($prefix . 'restrict_shipping_status') && isset($this->session->data['restrict_shipping'])) {
					foreach ($quote['quote'] as $index => $restricting_quote) {
						foreach ($this->session->data['restrict_shipping'] as $extension => $rules) {
							if ($extension != $result['code']) continue;
							foreach ($rules as $comparison => $values) {
								$adjusted_title = explode('(', $restricting_quote['title']);
								$adjusted_title = strtolower(html_entity_decode(trim($adjusted_title[0]), ENT_QUOTES, 'UTF-8'));
								if (($comparison == 'is' && in_array($adjusted_title, $values)) || ($comparison == 'not' && !in_array($adjusted_title, $values))) {
									unset($quote['quote'][$index]);
								}
							}
						}
					}
					if (empty($quote['quote'])) {
						continue;
					}
				}
				// end
			]]></add>
		</operation>
	</file>
	
</modification>