K
K
kycher2015-12-10 15:50:54
PHP
kycher, 2015-12-10 15:50:54

Simplacms how to make it so that it does not show product options in the filter with a quantity of 0?

In simple 2.1, there is no functionality for filters by product variants, I installed a filter , on top of a fresh system, everything works, but if a product variant has a quantity of 0, and even a price of 0, then it still shows this variant in the filter, while in the options goods everything works.
Tell me how to remove unnecessary options from the filters. All code is in the file above. But I will give the code in which I think you can change it so that everything works out) Thank you!

veiw/ProductsView.php
if(!empty($category) || !empty($brand)) {
                $variant_products = array();
                foreach($this->products->get_id_products($filter) as $p)
                    $variant_products[$p->id] = $p;

                $variant_products_ids = array_keys($variant_products);
                $features_variants = array();
                    $temp_variants = $this->variants->get_value_variants(array('product_id'=>$variant_products_ids, 'stock'=>true));
                    foreach($temp_variants as &$variant)
                       $features_variants[$variant->name] = $variant->name;  

                asort($features_variants);
                $this->design->assign('features_variants', $features_variants);

veiw/ProductsView.php
if(!empty($category) || !empty($brand)) {
                $variant_products = array();
                foreach($this->products->get_id_products($filter) as $p)
                    $variant_products[$p->id] = $p;

                $variant_products_ids = array_keys($variant_products);
                $features_variants = array();
                    $temp_variants = $this->variants->get_value_variants(array('product_id'=>$variant_products_ids, 'stock'=>true));
                    foreach($temp_variants as &$variant)
                       $features_variants[$variant->name] = $variant->name;  

                asort($features_variants);
                $this->design->assign('features_variants', $features_variants);

api/Variants.php
  public function get_value_variants($filter = array())
    {        
        $product_id_filter = '';
        $instock_filter = '';

        if(!empty($filter['product_id']))
           $product_id_filter = $this->db->placehold('AND v.product_id in([email protected])', (array)$filter['product_id']);
        if(!empty($filter['in_stock']) && $filter['in_stock'])
           $instock_filter = $this->db->placehold('AND (v.stock>0 OR v.stock IS NULL)');
        if(!$product_id_filter)
           return array();

        $query = $this->db->placehold("SELECT v.name
                   FROM __variants AS v
                   WHERE
                   v.name <> ''
                   $product_id_filter                  
                   $instock_filter
                   GROUP BY v.name                  
                   ");

        $this->db->query($query);    
        return $this->db->results();
    }

And the output in the template:
{if $features_variants|count > 1}
    <tr>       

    <td class="feature_name">Варианты:</td>
        <td class="feature_values">
            {foreach $features_variants as $o}
            <label><input type="checkbox" name="v[]" value="{$o}"{if $smarty.get.v && $o|in_array:$smarty.get.v} checked{/if}>&nbsp;
      {$o|escape}{$o|count|escape}
            {/foreach}
      
      </label> 
        </td>
    </tr>
    {/if}

Those v.stock>0 OR v.stock IS NULL does not work, but in $features_variants there are options that have stock 0... PS please help me fix the bug a little for help 100r on the phone

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2015-12-11
@healqq

$temp_variants = $this->variants->get_value_variants(array('product_id'=>$variant_products_ids, 'stock'=>true));

 if(!empty($filter['in_stock']) && $filter['in_stock'])

You're checking in_stock , but passing stock as a parameter .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question