2016-11-10 5 views
0

Hallo, weiß jemand, ob es eine Möglichkeit gibt zu wählen, welche Ihrer vorgestellten Produkte zuerst angezeigt werden? Wie kann ich Produkte in einem bestimmten Modul sortieren? wie neu am ältesten. Dies ist der Code für das Modul. Wie kann ich die Sortierreihenfolge für Produkte einstellen?Wie kann ich Sortierreihenfolge für vorgestellte Produkte in opencart hinzufügen?

$data['heading_title'] = $this->language->get('heading_title'); 

    $data['text_tax'] = $this->language->get('text_tax'); 

    $data['button_cart'] = $this->language->get('button_cart'); 
    $data['button_wishlist'] = $this->language->get('button_wishlist'); 
    $data['button_compare'] = $this->language->get('button_compare'); 

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

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

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

    if (!$setting['limit']) { 
     $setting['limit'] = 4; 
    } 

    if (!empty($setting['product'])) { 
     $products = array_slice($setting['product'], 0, (int)$setting['limit']); 

     foreach ($products as $product_id) { 
      $product_info = $this->model_catalog_product->getProduct($product_id); 

      if ($product_info) { 
       if ($product_info['image']) { 
        $image = $this->model_tool_image->resize($product_info['image'], $setting['width'], $setting['height']); 
       } else { 
        $image = $this->model_tool_image->resize('placeholder.png', $setting['width'], $setting['height']); 
       } 

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

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

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

       if ($this->config->get('config_review_status')) { 
        $rating = $product_info['rating']; 
       } else { 
        $rating = false; 
       } 

       $data['products'][] = array(
        'product_id' => $product_info['product_id'], 
        'thumb'  => $image, 
        'name'  => $product_info['name'], 
        'description' => utf8_substr(strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..', 
        'price'  => $price, 
        'special'  => $special, 
        'tax'   => $tax, 
        'rating'  => $rating, 
        'href'  => $this->url->link('product/product', 'product_id=' . $product_info['product_id']) 
       ); 
      } 
     } 
    } 

    if ($data['products']) { 
     if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/featured.tpl')) { 
      return $this->load->view($this->config->get('config_template') . '/template/module/featured.tpl', $data); 
     } else { 
      return $this->load->view('default/template/module/featured.tpl', $data); 
     } 
    } 
} 

}

+2

Haben Sie einen Code, was haben Sie versucht, haben Sie heute Mittag gegessen, wie weit ist der Mond? Brauche vielleicht ein wenig mehr Informationen, Kumpel. – Blinkydamo

+0

Ich habe einen Code im Internet gefunden, aber ich weiß nicht, welche Datei und welche Zeile ich hinzufügen sollte. Ich fand dieses "ORDER BY p.date_added DESC", aber ich weiß nicht, wie oder ist es korrekt. – Neconeco

+0

Mate, die auch nicht hilft, müssen Sie lesen und verstehen Sie den Code, den Sie heruntergeladen haben. Wenn wir den Code nicht sehen können, können wir Ihnen nicht helfen. – Blinkydamo

Antwort

1

Jedes Produkt in Opencart eine "Sortierung" -Feld auf "Daten" Tab hat, können Sie es füllen, wenn Sie ein Produkt hinzufügen oder bearbeiten:

Opencart Sort order field for product

Gehen Sie zu dieser Datei:

catalog/controller/module/featured.php 

Fügen Sie diese zu Zeile zu Produkten Array

'sort_order' => $product_info['sort_order'], 
'date_added' => $product_info['date_added'] 

und verwenden array_multisort als Gebrüll:

$temp_array = array(); 
foreach ($data['products'] as $key => $row){ 
    $temp_array[$key] = $row['sort_order']; 
} 
array_multisort($temp_array, SORT_ASC, $data['products']); 

Einsatz oberhalb Code kurz vor if ($data['products']) {

ist hier voll Code:

<?php 
class ControllerModuleFeatured extends Controller { 
    public function index($setting) { 
     $this->load->language('module/featured'); 

     $data['heading_title'] = $this->language->get('heading_title'); 

     $data['text_tax'] = $this->language->get('text_tax'); 

     $data['button_cart'] = $this->language->get('button_cart'); 
     $data['button_wishlist'] = $this->language->get('button_wishlist'); 
     $data['button_compare'] = $this->language->get('button_compare'); 

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

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

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

     if (!$setting['limit']) { 
      $setting['limit'] = 4; 
     } 

     if (!empty($setting['product'])) { 
      $products = array_slice($setting['product'], 0, (int)$setting['limit']); 

      foreach ($products as $product_id) { 
       $product_info = $this->model_catalog_product->getProduct($product_id); 

       if ($product_info) { 
        if ($product_info['image']) { 
         $image = $this->model_tool_image->resize($product_info['image'], $setting['width'], $setting['height']); 
        } else { 
         $image = $this->model_tool_image->resize('placeholder.png', $setting['width'], $setting['height']); 
        } 

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

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

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

        if ($this->config->get('config_review_status')) { 
         $rating = $product_info['rating']; 
        } else { 
         $rating = false; 
        } 

        $data['products'][] = array(
         'product_id' => $product_info['product_id'], 
         'thumb'  => $image, 
         'name'  => $product_info['name'], 
         'description' => utf8_substr(strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..', 
         'price'  => $price, 
         'special'  => $special, 
         'tax'   => $tax, 
         'rating'  => $rating, 
         'href'  => $this->url->link('product/product', 'product_id=' . $product_info['product_id']), 
         // Add 'sort_order' and 'date_added' to products array 
         'sort_order' => $product_info['sort_order'], 
         'date_added' => $product_info['date_added'] 
        ); 
       } 
      } 
     } 

     // Create a temporary array 
     $temp_array = array(); 
     foreach ($data['products'] as $key => $row){ 
      // You can use 'date_added' or 'sort_order' or 'price' or ... 
      $temp_array[$key] = $row['date_added']; 
     } 
     // You can use SORT_ASC or SORT_DESC 
     array_multisort($temp_array, SORT_ASC, $data['products']); 


     if ($data['products']) { 
      if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/featured.tpl')) { 
       return $this->load->view($this->config->get('config_template') . '/template/module/featured.tpl', $data); 
      } else { 
       return $this->load->view('default/template/module/featured.tpl', $data); 
      } 
     } 
    } 
} 

ich dies auf Opencart getestet habe 2.1 .0.1, Hoffe, das hilft dir.

+0

Still Produkte, die oben auf die Featured und neue unten hinzugefügt wird. Ich kopiere den ganzen Code immer noch gleich. Vielleicht seine Datei mit dem gleichen Namen im Admin-Ordner – Neconeco

+0

Es funktionierte jetzt. Ich habe SORT_ASC in desc und voila geändert! Vielen Dank. – Neconeco

+0

Gut zu hören, es hat funktioniert, gern geschehen. – DigitCart

Verwandte Themen