{"id":2059,"date":"2019-11-18T14:19:03","date_gmt":"2019-11-18T12:19:03","guid":{"rendered":"https:\/\/www.h-hennes.fr\/blog\/?p=2059"},"modified":"2019-11-18T14:31:47","modified_gmt":"2019-11-18T12:31:47","slug":"prestashop-1-7-ajouter-un-champ-produit-de-type-file-dans-ladministration","status":"publish","type":"post","link":"https:\/\/www.h-hennes.fr\/blog\/2019\/11\/18\/prestashop-1-7-ajouter-un-champ-produit-de-type-file-dans-ladministration\/","title":{"rendered":"Prestashop 1.7 : Ajouter un champ produit de type file dans l&rsquo;administration"},"content":{"rendered":"\n<p>Suite \u00e0 mon (ancien ) article <a href=\"https:\/\/www.h-hennes.fr\/blog\/2017\/10\/19\/prestashop-1-7-ajouter-des-champs-produit\/\">(https:\/\/www.h-hennes.fr\/blog\/2017\/10\/19\/prestashop-1-7-ajouter-des-champs-produit\/<\/a> ) sur l&rsquo;ajout de champs custom dans le formulaire d&rsquo;administration d&rsquo;un produit, j&rsquo;ai eut pas mal de questions sur comment ajouter un champ de type file.<\/p>\n\n\n\n<p>Il faut reconna\u00eetre que la fonctionnalit\u00e9 n&rsquo;est pas \u00e9vidente car le formulaire est soumis en ajax et le g\u00e8re pas l&rsquo;upload de fichiers.<\/p>\n<p>Le simple fait d&rsquo;ajouter un champ de type file dans le formulaire n&rsquo;est donc pas suffisant pour que cela fonctionne.<br><br>Cet article pr\u00e9sente une approche qui fonctionne, mais je ne peux pas garantir que c&rsquo;est la meilleure approche.<br><br>Pour son fonctionnement je me suis inspir\u00e9 de la gestion de l&rsquo;upload qui est faite pour la gestion des images standard du produit via la librairie Js <strong>Dropzone<\/strong><\/p>\n<p>Pour le code j&rsquo;ai donc repris le code de l&rsquo;ancien article en y ajoutant la logique d&rsquo;un champ image.<br><br>Voici visuellement comment se pr\u00e9sente le champ d&rsquo;upload<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"344\" height=\"237\" src=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2019\/11\/blog-input-file.png\" alt=\"\" class=\"wp-image-2060\" srcset=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2019\/11\/blog-input-file.png 344w, https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2019\/11\/blog-input-file-300x207.png 300w\" sizes=\"auto, (max-width: 344px) 100vw, 344px\" \/><\/figure>\n\n\n\n<p>L&rsquo;ensemble de la logique d&rsquo;upload et de sauvegarde de la valeur du produit va \u00eatre g\u00e9r\u00e9e directement dans le module<\/p>\n<p>Voici le rendu dans le cas ou une image existe d\u00e9j\u00e0.<br>Il est possible de la supprimer en cliquant sur le lien correspondant<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"546\" height=\"595\" src=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2019\/11\/blog-input-file-exists-e1574076447618.png\" alt=\"\" class=\"wp-image-2061\" srcset=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2019\/11\/blog-input-file-exists-e1574076447618.png 546w, https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2019\/11\/blog-input-file-exists-e1574076447618-275x300.png 275w\" sizes=\"auto, (max-width: 546px) 100vw, 546px\" \/><\/figure>\n\n\n\n<p>Voici le code pour l&rsquo;affichage et la gestion du champ dans le fichier tpl<br>Les commentaires devrait vous \u00e9clairer sur le fonctionnement, je l&rsquo;ai pens\u00e9 de mani\u00e8re \u00e0 pouvoir ajouter facilement plusieurs champs<\/p>\n\n\n\n<pre>\n{* Affichage du champ Image *}\n            &lt;div class=&quot;col-lg-12 col-xl-12&quot;&gt;\n                &lt;label class=&quot;form-control-label&quot;&gt;{l s='my custom field file' mod='hhproduct'}&lt;\/label&gt;\n\n                &lt;div id=&quot;custom_field_file_thumbnail&quot;&gt;\n                {* Si une image existe d&eacute;j&agrave; sur le produit *}\n                    {if isset($custom_field_file) &amp;&amp; $custom_field_file !=&quot;&quot;}\n                        &lt;div style=&quot;border: 1px dashed #000; padding:10px; max-width: 500px; margin-bottom: 30px; margin-top: 30px&quot; &gt;\n                            &lt;label class=&quot;form-control-label&quot;&gt;{l s='my current image' mod='hhproduct'}&lt;\/label&gt;&lt;br \/&gt;\n                        &lt;img src=&quot;{$file_dir}{$custom_field_file}&quot; style=&quot;max-width: 450px&quot; \/&gt;&lt;br \/&gt;\n                        &lt;a href=&quot;#&quot; class=&quot;custom_field_file_delete&quot;&gt;{l s='Delete this image' mod='hhproduct'}&lt;\/a&gt;\n                        &lt;\/div&gt;\n                {\/if}\n                &lt;\/div&gt;\n                &lt;input type=&quot;file&quot; class=&quot;dropzone&quot; name=&quot;custom_field_file_uploader&quot; id=&quot;custom_field_file_uploader&quot; \/&gt;\n            &lt;\/div&gt;\n                &lt;script&gt;\n                    $(function() {\n                        \/\/Gestion de l'upload via la librairie DropZone\n                        Dropzone.autoDiscover = false;\n                        var customFieldImageDropzone = $('#custom_field_file_uploader').dropzone({\n                            url: '{$moduleLink}&amp;id_product={$id_product}&amp;uploadProductImage=1&amp;field_name=custom_field_file',\n                            paramName: &quot;file&quot;, \/\/Nom du champs &agrave; envoyer\n                            previewTemplate : document.querySelector('#custom_field_file_dropzone_template').innerHTML,\n                            thumbnailWidth : 450,\n                            success : function(file){\n                                alert('{l s='File uploaded with success' mod='hhproduct'}');\n                                \/\/Affichage de la nouvelle image dans l'emplacement\n                                $('#custom_field_file_thumbnail').html('').html(file.previewElement);\n                            }\n                        });\n\n                        \/\/Gestion de la suppression\n                        $('#custom_field_file_thumbnail').on('click','.custom_field_file_delete',function(){\n                            $.ajax({\n                               method: 'post',\n                               url : '{$moduleLink}&amp;id_product={$id_product}&amp;deleteProductImage=1&amp;field_name=custom_field_file',\n                               success : function(msg){\n                                   console.log(msg);\n                                   alert('{l s='File delete with success' mod='hhproduct'}');\n                                   $('#custom_field_file_thumbnail').html('');\n                               }\n                            });\n                            return false;\n                        });\n                    });\n                &lt;\/script&gt;\n\n            {* template d'affichage dropzone *}\n            &lt;div id=&quot;custom_field_file_dropzone_template&quot; style=&quot;display:none&quot;&gt;\n                &lt;div style=&quot;border: 1px dashed #000; padding:10px; max-width: 500px; margin-bottom: 30px; margin-top: 30px&quot;&gt;\n                    &lt;div class=&quot;dz-details&quot;&gt;\n                        &lt;label class=&quot;form-control-label&quot;&gt;{l s='my new image' mod='hhproduct'}&lt;\/label&gt;&lt;br \/&gt;\n                        &lt;img data-dz-thumbnail style=&quot;max-width: 450px&quot; \/&gt;&lt;br \/&gt;\n                        &lt;a href=&quot;#&quot; class=&quot;custom_field_file_delete&quot;&gt;{l s='Delete this image' mod='hhproduct'}&lt;\/a&gt;\n                    &lt;\/div&gt;\n                &lt;\/div&gt;\n            &lt;\/div&gt;\n<\/pre>\n\n\n\n<p>Et voici le code php de gestion de cet upload<br \/>De mon c\u00f4t\u00e9 l&rsquo;objet product a \u00e9t\u00e9 surcharg\u00e9 pour ajouter ce champ mais libre \u00e0 vous de faire un traitement diff\u00e9rent<\/p>\n\n\n\n<pre lang=\"php\" escaped=\"true\">\n\/**\n     * Configuration admin du module\n     * @return string\n     * @throws PrestaShopDatabaseException\n     * @throws PrestaShopException\n     *\/\n    public function getContent()\n    {\n        \/\/Gestion de l'envoi des fichiers\n        if (Tools::getValue('uploadProductImage')) {\n            $id_product = (int)Tools::getValue('id_product');\n            $field_name = Tools::getValue('field_name');\n            if ($this->_updateProductImageField($id_product, $field_name)) {\n                return $this->l('Image uploaded with success');\n            }\n        } elseif (Tools::getValue('deleteProductImage')) {\n            $id_product = (int)Tools::getValue('id_product');\n            $field_name = Tools::getValue('field_name');\n            if ($this->_deleteProductImageField($id_product, $field_name)) {\n                return $this->l('Image deleted with success');\n            }\n        } else {\n            return 'No configuration needed for this module';\n        }\n\n    }\n\n    \/**\n     * Upload de l'image d'un produit\n     * @param $id_product\n     * @param $field_name\n     * @return bool\n     * @throws PrestaShopDatabaseException\n     * @throws PrestaShopException\n     *\/\n    protected function _updateProductImageField($id_product, $field_name)\n    {\n        \/\/Envoi de l'image\n        \/\/@TOdo g\u00e9rer le cas ou l'image existe d\u00e9j\u00e0\n        $savePath = dirname(__FILE__) . '\/views\/img\/p';\n        $uploader = new Uploader('file');\n        $file = $uploader->setSavePath($savePath)\n            ->setAcceptTypes(['jpg', 'png', 'git', 'jpeg'])\n            ->process();\n\n        $fileName = ltrim(str_replace($savePath, '', $file[0]['save_path']), '\/');\n        try {\n            \/\/Sauvegarde la valeur de l'image pour le produit\n            $product = new Product($id_product);\n            $product->$field_name = $fileName;\n            $product->save();\n        } catch (PrestaShopException $e) {\n            echo $e->getMessage();\n            return false;\n        }\n\n        return true;\n    }\n\n\n    \/**\n     * Suppression de l'image d'un produit\n     * @param $id_product\n     * @param $field_name\n     * @return bool\n     *\/\n    protected function _deleteProductImageField($id_product, $field_name)\n    {\n        try {\n            \/\/Sauvegarde d'une valeur vide pour le produit\n            $product = new Product($id_product);\n\n\n            \/\/Si le produit a une image et qu'elle existe on la supprime\n            if ($product->$field_name != \"\") {\n                $imagePath = dirname(__FILE__) . '\/views\/img\/p\/' . $product->$field_name;\n                if (is_file($imagePath)) {\n                    unlink($imagePath);\n                }\n            }\n            $product->$field_name = '';\n            $product->save();\n        } catch (PrestaShopException $e) {\n            echo $e->getMessage();\n            return false;\n        }\n    }\n<\/pre>\n\n\n\n<p>Vous pouvez t\u00e9l\u00e9charger ci-dessous une version du module qui impl\u00e9mente ce champ (je l&rsquo;ai test\u00e9 uniquement sur Ps 1.7.6 )<\/p>\n<p>\u00a0<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2019\/11\/hhproduct.zip\">hhproduct<\/a><a href=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2019\/11\/hhproduct.zip\" class=\"wp-block-file__button\" download>T\u00e9l\u00e9charger<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Suite \u00e0 mon (ancien ) article (https:\/\/www.h-hennes.fr\/blog\/2017\/10\/19\/prestashop-1-7-ajouter-des-champs-produit\/ ) sur l&rsquo;ajout de champs custom dans le formulaire d&rsquo;administration d&rsquo;un produit, j&rsquo;ai eut pas mal de questions sur comment ajouter un champ de type file. Il faut reconna\u00eetre que la fonctionnalit\u00e9 n&rsquo;est pas \u00e9vidente car le formulaire est soumis en ajax et le g\u00e8re pas l&rsquo;upload de [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[245],"tags":[512,551,483,550],"class_list":["post-2059","post","type-post","status-publish","format-standard","hentry","category-prestashop-2","tag-administration","tag-file","tag-prestashop-1-7","tag-product"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/2059","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/comments?post=2059"}],"version-history":[{"count":4,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/2059\/revisions"}],"predecessor-version":[{"id":2068,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/2059\/revisions\/2068"}],"wp:attachment":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/media?parent=2059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/categories?post=2059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/tags?post=2059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}