2017-01-06 8 views
1

現在、管理パネルにあり、外部APIを使用するPrestashop 1.6.10用のモジュールをプログラミングしています。問題は、バックオフィスのデータベースに製品を追加する方法がわかりません。Prestashop 1.6.10の製品をAPI外部から追加しますか?

これは私がwritedしているコードです:私は「製品」オブジェクトを使用しますが、それは動作していない

public function Product() { 
    if (empty($_GET['product'])) { 
     return false; 
    } 

    switch($_GET['product']) { 
     case 'add' : 
      $product = new ProductCore(); 
      $product->id_shop_default = 1; 
      $product->id_manufacturer = 1; 
      $product->id_supplier = 1; 
      $product->reference = "adding_product"; 
      $product->supplier_reference = ""; 
      $product->location = ""; 
      $product->width = "0.00000"; 
      $product->height = "0.00000"; 
      $product->depth = "0.00000"; 
      $product->weight = "0.00000"; 
      $product->quantity_discount = "0"; 
      $product->ean13 = "0"; 
      $product->upc = ""; 
      $product->cache_is_pack = "0"; 
      $product->cache_has_attachments = "0"; 
      $product->is_virtual = "0"; 
      $product->save(); 
      $product->add(); 
      break; 
     /** Product suppression. 
     case 'del' : 
      if (Product::existsInDatabase()) { 

      } 
      break; 
    } 
    return false; 
} 

を、私はなぜ:(

誰かが私を助けてくださいもらえません

答えて

1

かわりProductCoreProductクラスを使用する必要があります。

save機能はDBに製品を保存するのに十分です。私は、その後、add関数を使用する必要はありません。

製品の値が正しくない場合、エラーが表示されます。しかし、最初にDEBUG MODEを有効にする必要があります。 Activate Prestashop Debug Mode

幸いです。

0

次のコードを使用してください。

{ 
     $object = new Product(); 
     foreach ($_POST as $key => $value) { 
      if (array_key_exists($key, $object) && $key != 'id_product') { 
       $object->{$key} = $value; 
      } 
     } 

     $languages = Language::getLanguages(false); 
     $class_vars = get_class_vars(get_class($object)); 
     $fields = array(); 
     if (isset($class_vars['definition']['fields'])) { 
      $fields = $class_vars['definition']['fields']; 
     } 
     foreach ($fields as $field => $params) { 
      if (array_key_exists('lang', $params) && $params['lang']) { 
       foreach ($languages as $language) { 
        $value = ''; 

        if (Tools::getIsset($field . '_' . (int)$language['id_lang'])) { 
         $value = Tools::getValue($field . '_' . (int)$language['id_lang']); 
        } elseif (isset($object->{$field}[(int)$language['id_lang']])) { 
         $value = $object->{$field}[(int)$language['id_lang']]; 
        } 
      foreach ($languages as $lang) { 
       if (Tools::getIsset($field . '_' . (int)$lang['id_lang']) && Tools::getValue($field . '_' . (int)$lang['id_lang']) != '') 
        $value = Tools::getValue($field . '_' . (int)$lang['id_lang']); 
      } 
        if ($field == 'description_short') { 
         $short_description_limit = Configuration::get('PS_PRODUCT_SHORT_DESC_LIMIT') 
          ? Configuration::get('PS_PRODUCT_SHORT_DESC_LIMIT') : 400; 
         $object->{$field}[(int)$language['id_lang']] = strip_tags(
          $this->clipLongText(
           $value, 
           '', 
           $short_description_limit, 
           false 
          ) 
         ); 
        } else { 
         $object->{$field}[(int)$language['id_lang']] = $value; 
        } 
       } 
      } 
     } 

     foreach ($languages as $language) { 
      $keywords = ''; 
      if (Tools::getIsset('meta_keywords_' . $language['id_lang'])) { 
       $keywords = Tools::getValue('meta_keywords_' . $language['id_lang']); 
      } elseif (isset($object->meta_keywords[$language['id_lang']])) { 
       $keywords = $object->meta_keywords[$language['id_lang']]; 
      } 
      $keywords = $this->cleanMetaKeywords(
       Tools::strtolower($keywords) 
      ); 
      $object->meta_keywords[$language['id_lang']] = $keywords; 
     } 

     $_POST['width'] = (!Tools::getIsset('width')) ? '0' : str_replace(',', '.', Tools::getValue('width')); 
     $_POST['height'] = (!Tools::getIsset('height')) ? '0' : str_replace(',', '.', Tools::getValue('height')); 
     $_POST['depth'] = (!Tools::getIsset('depth')) ? '0' : str_replace(',', '.', Tools::getValue('depth')); 
     $_POST['weight'] = (!Tools::getIsset('weight')) ? '0' : str_replace(',', '.', Tools::getValue('weight')); 

     if (Tools::getIsset('unit_price') != null) { 
      $object->unit_price = str_replace(',', '.', Tools::getValue('unit_price')); 
     } 

     $object->available_for_order = (int)Tools::getValue('available_for_order'); 
     $object->show_price = $object->available_for_order ? 1 : (int)Tools::getValue('show_price'); 
     $object->on_sale = (int)Tools::getValue('on_sale'); 
     $object->online_only = (int)Tools::getValue('online_only'); 

     $ecotaxTaxRate = Tax::getProductEcotaxRate(); 
     if ($ecotax = Tools::getValue('ecotax')) { 
      $_POST['ecotax'] = Tools::ps_round($ecotax/(1 + $ecotaxTaxRate/100), 6); 
     } 
     if (Tools::getIsset('ecotax') != null) { 
      $object->ecotax = str_replace(',', '.', Tools::getValue('ecotax')); 
     } 
     $object->add(); 
    } 
0

私は私のコードに問題が見つかりました:私はカタログページから「製品の追加」をクリックすると、機能の製品は、コントローラadminControllerによって実行されていません。

また、私はPrestashopに関数を実行させると機能しますが、Prestashopはこれを気に入らないのですが、バグを作成してモジュールにアクセスできないためです。

[17時35 GMTでのUPDATE 2017年1月9日]

は現在、コードが働いているが、私は今、この問題を持っている...私は、製品を作成するとき、それは言語パラメータについてだと思います、しかし、私はこの問題を解決するために必要なことはしません。

enter image description here

関連する問題