2016-04-11 20 views
0

PRESTASHOP ile optik dükkanı yapmaya çalışıyorum, ancak bir sorunla karşı karşıyayım. Her iki tabloda da aynı ürününü ürün ve müşteri tablosu (sol göz diyoptri, sağ göz diyoptri, köprü uzunluğu, bacak uzunluğu) olarak oluşturdum.Sadece özellik başına gösterme

Ne yapmak istediğim, dükkan ürünleri yüklediğinde, bu değişkenleri karşılaştırır ve aynıysa ürünü gösterir. Bu, gözlükleri müşteriye filtrelemeyi, sadece ona uygun gözlükleri göstermeyi denemektir.

orijinal sorgu

yanındadır:

$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) AS quantity'.(Combination::isFeatureActive() ? ', IFNULL(product_attribute_shop.id_product_attribute, 0) AS id_product_attribute, 
        product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity' : '').', pl.`description`, pl.`description_short`, pl.`available_now`, 
        pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, image_shop.`id_image` id_image, 
        il.`legend` as legend, m.`name` AS manufacturer_name, cl.`name` AS category_default, 
        DATEDIFF(product_shop.`date_add`, DATE_SUB("'.date('Y-m-d').' 00:00:00", 
        INTERVAL '.(int)$nb_days_new_product.' DAY)) > 0 AS new, product_shop.price AS orderprice 
       FROM `'._DB_PREFIX_.'category_product` cp 
       LEFT JOIN `'._DB_PREFIX_.'product` p 
        ON p.`id_product` = cp.`id_product` 
       '.Shop::addSqlAssociation('product', 'p'). 
       (Combination::isFeatureActive() ? ' LEFT JOIN `'._DB_PREFIX_.'product_attribute_shop` product_attribute_shop 
       ON (p.`id_product` = product_attribute_shop.`id_product` AND product_attribute_shop.`default_on` = 1 AND product_attribute_shop.id_shop='.(int)$context->shop->id.')':'').' 
       '.Product::sqlStock('p', 0).' 
       LEFT JOIN `'._DB_PREFIX_.'category_lang` cl 
        ON (product_shop.`id_category_default` = cl.`id_category` 
        AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').') 
       LEFT JOIN `'._DB_PREFIX_.'product_lang` pl 
        ON (p.`id_product` = pl.`id_product` 
        AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').') 
       LEFT JOIN `'._DB_PREFIX_.'image_shop` image_shop 
        ON (image_shop.`id_product` = p.`id_product` AND image_shop.cover=1 AND image_shop.id_shop='.(int)$context->shop->id.') 
       LEFT JOIN `'._DB_PREFIX_.'image_lang` il 
        ON (image_shop.`id_image` = il.`id_image` 
        AND il.`id_lang` = '.(int)$id_lang.') 
       LEFT JOIN `'._DB_PREFIX_.'manufacturer` m 
        ON m.`id_manufacturer` = p.`id_manufacturer` 
       WHERE product_shop.`id_shop` = '.(int)$context->shop->id.' 
        AND cp.`id_category` = '.(int)$this->id 
        .($active ? ' AND product_shop.`active` = 1' : '') 
        .($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '') 
        .($id_supplier ? ' AND p.id_supplier = '.(int)$id_supplier : ''); 

bunu değiştirmeye çalışıyorum, ama nasıl çok açık yok. Ve ardında, yanlış şeyler yapıyorum. Bir sonraki sol bağlantıyı sorguya ekledim.

LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON pa.`id_product` = p.`id_product` 
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON pac.`id_product_attribute` = pa.`id_product_attribute` 
LEFT JOIN `'._DB_PREFIX_.'attribute` attr ON attr.`id_attribute` = pac.`id_attribute` 
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` attr_lang ON (attr_lang.`id_attribute` = pac.`id_attribute` AND attr_lang.`id_lang` = '.(int)$id_lang.')LEFT JOIN `'._DB_PREFIX_.'attribute_group` attr_group ON attr_group.`id_attribute_group` = attr.`id_attribute_group` 
LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` attr_group_lang ON attr_group_lang.`id_attribute_group` = attr.`id_attribute_group` 

Herhangi bir öneriniz için teşekkür ederiz.

DÜZENLEME: PrestaShop içindeki ürünlerin

yeni alanlardır özellikler hata için üzgünüm (iç ürün tablo değildir).

Bilgileriniz için prestashop'un veri modelini koydum.

DataModel

DÜZENLEME 2:

Ben bir modül kullanarak bunu başarmak için şimdi çalışıyorum, bu yüzden benim modülün benim php dosyası bir sonraki kodu vardır. Kodları CategoryController.php'den takip ettim, ancak koşullar karşılanmadığı takdirde bir ürünü nasıl kaldıracağımı bilmiyorum.

<?php 

if (!defined('_PS_VERSION_')) 
    exit; 

class glassOptics extends Module 
{ 
    /* @var boolean error */ 
    protected $_errors = false; 

    public function __construct() 
    { 
     $this->name = 'glassOptics'; 
     $this->tab = 'front_office_features'; 
     $this->version = '1.0'; 
     $this->author = 'MAOL'; 
     $this->need_instance = 0; 

     parent::__construct(); 

     $this->displayName = $this->l('glassOptics'); 
     $this->description = $this->l('...'); 
    } 

    public function install() 
    { 
     if (!parent::install() OR 
      !$this->veopticasCustomerDB('add') OR 
      !$this->veopticasProductDB('add') OR    
      !$this->registerHook('hookActionProductListOverride') 
      return false; 
     return true; 
    } 

    public function uninstall() 
    { 
     if (!parent::uninstall() OR !$this->veopticasCustomerDB('remove') OR !$this->veopticasProductDB('remove')) 
      return false; 
     return true; 
    } 


    public function veopticasCustomerDB($method) 
    { 
     switch ($method) { 
      case 'add': 
       $sql = 'CREATE TABLE IF EXISTS `'._DB_PREFIX_.'customer_optics_data` (
       `id_customer` int(10) UNSIGNED NOT NULL, 
       `left_dioptrics` decimal(20,6) NOT NULL DEFAULT '0.000000', 
       `right_dioptrics` decimal(20,6) NOT NULL DEFAULT '0.000000', 
       `bridge` decimal(20,6) NOT NULL DEFAULT '0.000000', 
       `leg` decimal(20,6) NOT NULL DEFAULT '0.000000', 
       `glass_width` decimal(20,6) NOT NULL DEFAULT '0.000000', 
       `glass_height` decimal(20,6) NOT NULL DEFAULT '0.000000' 
       ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'; 

       break; 

      case 'remove': 
       $sql = 'DROP TABLE IF EXISTS `'._DB_PREFIX_ . 'customer_optics_data`'; 
       break; 
     } 

     if(!Db::getInstance()->Execute($sql)) 
      return false; 
     return true; 
    } 

    public function veopticasProductDB($method) 
    { 
     switch ($method) { 
      case 'add': 
       $sql = 'CREATE TABLE IF EXISTS `'._DB_PREFIX_.'product_optics_data` (
       `id_product` int(10) UNSIGNED NOT NULL, 
       `left_dioptrics` decimal(20,6) NOT NULL DEFAULT '0.000000', 
       `right_dioptrics` decimal(20,6) NOT NULL DEFAULT '0.000000', 
       `bridge` decimal(20,6) NOT NULL DEFAULT '0.000000', 
       `leg` decimal(20,6) NOT NULL DEFAULT '0.000000', 
       `glass_width` decimal(20,6) NOT NULL DEFAULT '0.000000', 
       `glass_height` decimal(20,6) NOT NULL DEFAULT '0.000000' 
       ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'; 

       break; 

      case 'remove': 
       $sql = 'DROP TABLE IF EXISTS `'._DB_PREFIX_ . 'product_optics_data`'; 
       break; 
     } 

     if(!Db::getInstance()->Execute($sql)) 
      return false; 
     return true; 
    } 


    public function hookActionProductListOverride($params) 
    { 
     $customer_settings = glassOptics::getCustomerSettings($this->context->customer); 

     if ($customer_settings) { 
      // Inform the hook was executed 
      $params['hookExecuted'] = true; 

      // Filter products here, you are now overriding the default 
      // functionality of CategoryController class. 
      // You can see blocklayered module for more details. 

      if ((isset($this->context->controller->display_column_left) && !$this->context->controller->display_column_left) 
      && (isset($this->context->controller->display_column_right) && !$this->context->controller->display_column_right)) 
      return false; 

      global $smarty; 
      if (!Configuration::getGlobalValue('PS_LAYERED_INDEXED')) 
       return; 

      $categories_count = Db::getInstance()->getValue(' 
       SELECT COUNT(*) 
       FROM '._DB_PREFIX_.'layered_category 
       WHERE id_category = '.(int)Tools::getValue('id_category', Tools::getValue('id_category_layered', Configuration::get('PS_HOME_CATEGORY'))).' 
       AND id_shop = '.(int) Context::getContext()->shop->id 
      ); 

      if ($categories_count == 0) 
       return; 


      // List of product to overrride categoryController 
      $params['catProducts'] = array(); 
      $selected_filters = $this->getSelectedFilters(); 
      $filter_block = $this->getFilterBlock($selected_filters); 
      $title = ''; 

      if (is_array($filter_block['title_values'])) 
       foreach ($filter_block['title_values'] as $key => $val) 
        $title .= ' > '.$key.' '.implode('/', $val); 

      $smarty->assign('categoryNameComplement', $title); 
      $this->getProducts($selected_filters, $params['catProducts'], $params['nbProducts'], $p, $n, $pages_nb, $start, $stop, $range); 
      // Need a nofollow on the pagination links? 
      $smarty->assign('no_follow', $filter_block['no_follow']); 

      foreach ($params['nbProducts'] as $product) { 

       $product_settings = glassOptics::getProductSettings($product); 

       if($product_settings){ 
        $same_bridge   = ($product_settings->bridge == $customer_settings->bridge ? true : false); 
        $same_leg   = ($product_settings->leg == $customer_settings->leg ? true : false); 
        $same_glass_width = ($product_settings->glass_width == $customer_settings->glass_width ? true : false); 
        $same_glass_heigth  = ($product_settings->glass_heigth == $customer_settings->glass_heigth ? true : false); 
       } 

      } 

     } 
    } 
} 
+0

Veritabanı şemasını göster lütfen. –

+0

@davidstrachan İlişkileri tanımak için datamodel'i ekledim. Katılması gereken tabloları renklendirdim. – MAOL

+0

Çekirdek veritabanı yapısına bulaşmamalısınız, bu sizin için güncellemeleri mahveder. Neden "customer_diopter" ve "product_diopter" gibi iki ek tablo ekleyemiyorsunuz ve sonra ürünlerin seçilme şeklini değiştirmek için yerleşik geçersiz kılma mekanizmasını kullanamıyorsunuz? Veya daha da iyisi - 'product_diopter''nı hep birlikte kaldırın, özellikleri/özniteliklerle değiştirin ve müşterinin değerlerini' customer_diopter' tablonuzdan alacak ve eşleşen özelliklere sahip ürünleri seçecek basit bir filtre oluşturun. – Eihwaz

cevap

2

Farklı bir yaklaşım kullanmayı ve bunun için bir modül kullanmayı öneririm. Yüklendikten sonra customer_optics_data gibi bir tablo oluşturur. Tablo yapısı şöyle görünebilir:

CREATE TABLE `'._DB_PREFIX_.'customer_optics_data` (
    `id_customer` int(10) UNSIGNED NOT NULL, 
    `left_eye_diopter` int(10) UNSIGNED NOT NULL, 
    `right_eye_diopter` int(10) UNSIGNED NOT NULL, 
    `bridge_length` decimal(20,6) NOT NULL DEFAULT '0.000000', 
    `leg_length` decimal(20,6) NOT NULL DEFAULT '0.000000' 
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8; 

Sonra modül actionProductListOverride kanca üzerine kanca, sizler kontrolü gerçekleştirmek istiyorum budur:

public function hookActionProductListOverride($params) 
{ 
    $customer_settings = MyDiopterModuleHelperClass::getCustomerSettings($this->context->customer); 

    if ($customer_settings) { 
     $params['hookExecuted'] = true; 

     // Filter products here, you are now overriding the default 
     // functionality of CategoryController class. 
     // You can see blocklayered module for more details. 
    } 
} 

modülü yardımcı sınıfı MyDiopterModuleHelperClass olurdu customer_optics_data tablosuna kayıt ve veri almak için var. Bu şekilde, çekirdeği geçersiz kılmıyorsunuz, güncellemeleriniz normal olarak işlev görecek, olabilecek en kötü şey, kanca PrestaShop'un gelecekteki sürümlerinden aniden kaldırılırsa, bu mümkün değil.

modül ayrıca aşağıdaki kanca kullandı:

  • displayCustomerIdentityForm - My Personal information yılında ek alanları görüntülemek için. Müşterilerinizin bu bilgileri modül için gireceği yer burasıdır.

  • actionObjectCustomerAddAfter - Eğer $_POST gelen bu verileri alıp modülün tabloda kaydetmek istiyorum budur

  • actionObjectCustomerUpdateAfter - Eğer müşteri tarafından değiştirilmiş ise, veri güncelleme veya veri eklemek istiyorum budur eğer bir sebepten dolayı orada değil.

İsteğe bağlı olarak, displayAdminCustomersForm üzerine modülü kanca olabilir - Arka ofisinizde customers şeklinde ek alanları görüntülemek için.

+0

Merhaba tekrar @Eihwaz, modemi koduma eklemek için gönderimi düzenledim. Koşulların yerine getirildiği duruma ulaşmayı başardım, ancak koşulları karşılamayan ürünleri nasıl sileceğimi/sildiğimi/gizlemeyi bilmiyorum. Her neyse, kaynak kodumun doğru olup olmadığını bilmiyorum, henüz test edemedim. Bir bakabilir misin? Teşekkürler – MAOL

İlgili konular