<?xml version="1.0" encoding="utf-8"?>
<modification>

  <name>E-Piksel Filter Instock on Category Page</name>
  <version>1.0.1</version>
  <code>EPMOD35</code>
  <author>Ekrem KAYA</author>
  <link>http://e-piksel.com</link>

  <file path="catalog/controller/product/category.php">
    <operation>
      <search><![CDATA[$data['breadcrumbs'] = array();]]></search>
      <add position="before"><![CDATA[
    if (isset($this->request->get['instock'])) {
      $instock = $this->request->get['instock'];
    } else {
      $instock = '';
    }
    ]]></add>
    </operation>

    <operation>
      <search><![CDATA[$data['button_cart'] = $this->language->get('button_cart');]]></search>
      <add position="before"><![CDATA[
      $data['entry_instock'] = $this->language->get('entry_instock');
    ]]></add>
    </operation>

    <operation>
      <search><![CDATA[$filter,]]></search>
      <add position="after"><![CDATA[
     'filter_instock'     => $instock,
    ]]></add>
    </operation>

    <operation>
      <search><![CDATA[$data['limit'] = $limit;]]></search>
      <add position="after"><![CDATA[
     $data['url'] = html_entity_decode($this->url->link('product/category', 'path=' . $this->request->get['path'] . $url), ENT_QUOTES, 'UTF-8');
     $data['instock'] = $instock;
    ]]></add>
    </operation>
  </file>

  <file path="catalog/language/*/product/category.php">
    <operation>
      <search><![CDATA[$_['text_refine']]]></search>
      <add position="before"><![CDATA[
$_['entry_instock']       = 'In stock only';
    ]]></add>
    </operation>
  </file>

  <file path="catalog/model/catalog/product.php">
    <operation>
      <search><![CDATA[if (!empty($data['filter_name']) || !empty($data['filter_tag'])) {]]></search>
      <add position="before"><![CDATA[
    if (!empty($data['filter_instock'])) {
      $sql .= " AND (p.stock_status_id = '3' || p.expres=1)";
    }
    ]]></add>
    </operation>
  </file>

  <file path="catalog/view/theme/*/template/product/category.tpl">
    <operation>
      <search><![CDATA[<div class="btn-group ]]></search>
      <add position="after" offset="3"><![CDATA[
      <label for="input-instock"><input type="checkbox" name="instock" id="input-instock" <?php echo ($instock) ? 'value="1" checked' : 'value=""'; ?> /> <?php echo $entry_instock; ?></label>
    ]]></add>
    </operation>

    <operation>
      <search><![CDATA[<?php echo $footer; ?>]]></search>
      <add position="before"><![CDATA[
<script type="text/javascript"><!--
var url_filter = '<?php echo $url; ?>';

$('#input-instock').on('click', function() {
  url = url_filter;

  var instock = $('#content input[name=\'instock\']').val();

  if (instock == '1') {
    url += '';
  } else if (instock == '') {
    url += '&instock=true';
  } else {
    url += '';
  }

  location = url;
});
--></script>
    ]]></add>
    </operation>
  </file>
</modification>