Jump to content

Problem with custom image field for Product


miguelcubica

Recommended Posts

Hi.

I have a problem creating an extra field for the products. I do it through a module and, if this field is file type, in this case I want to add a secondary image to the product, the information about this new field is not available in the hookActionProductSave.

The field is displayed correctly, but I can't upload the image or get its data.


I show my code to see if you can help me.


Thank you very much.

 

Module code:

 

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

use PrestaShopBundle\Form\Admin\Type\TranslateType;
use PrestaShopBundle\Form\Admin\Type\FormattedTextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use PrestaShopBundle\Form\Admin\Type\SwitchType;
use Symfony\Component\Form\AbstractType;

use Symfony\Component\HttpFoundation\File\Exception\FileException;
use App\Service\FileUploader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\String\Slugger\SluggerInterface;

use App\Entity\Product;
use App\Form\ProductType;

class Ps_AddProductFields extends Module{
    public function __construct(){
        $this->name = 'ps_addproductfields';
        $this->tab = 'front_office_features';
        $this->version = '2.0.0';
        $this->author = -----';

        $this->bootstrap = true;
        parent::__construct();

        $this->displayName = $this->getTranslator()->trans('Añade campos al producto', array(), 'Modules.Ps_Addproductfields.Admin');
        $this->description = $this->getTranslator()->trans('Añade ciertos campos a la administración de la ficha de producto.', array(), 'Modules.Ps_Addproductfields.Admin');
        $this->ps_versions_compliancy = array('min' => '1.7.1.0', 'max' => _PS_VERSION_);
    }

    public function install(){
        return parent::install() && $this->registerHook('displayAdminProductsExtra') && $this->registerHook('actionProductSave') && $this->registerHook('actionProductUpdate') && $this->registerHook('actionAdminControllerSetMedia');
    }

    public function uninstall(){
        if (!parent::uninstall()) {
            return false;
        }
        return true;
    }
    
    public function hookActionAdminControllerSetMedia($params){
        $this->context->controller->addJS($this->_path.'assets/js/ps_addproductfields.js');
    }

    public function hookActionProductSave($params){
        $productAdapter = $this->get('prestashop.adapter.data_provider.product');
        if($_REQUEST['form']['id_product']){
            $product = $productAdapter->getProduct($_REQUEST['form']['id_product']);
            
            $imagen_secundaria = $_REQUEST['form']['imagen_secundaria']->getData();
            if($imagen_secundaria->move(_PS_MODULE_DIR_.'/ps_addproductfields/img/product', $imagen_secundaria->getClientOriginalName())){
                if(!Db::getInstance()->update('product', array('imagen_secundaria' => $imagen_secundaria->getClientOriginalName()), 'id_product = ' .$_REQUEST['form']['id_product'])){
                    $this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();
                } else {
                    $this->context->controller->_errors[] = Tools::displayError('Error: No se ha podido guardar la imagen secundaria.');
                }
            } else {
                $this->context->controller->_errors[] = Tools::displayError('Error: No se ha podido guardar la imagen secundaria.');
            }
        }
    }
    
    public function hookDisplayAdminProductsExtra($params){
        //$this->context->controller->addJS($this->_path.'assets/js/ps_addproductfields.js');
        
        $productAdapter = $this->get('prestashop.adapter.data_provider.product');
        $product = $productAdapter->getProduct($params['id_product']);
        
        $formFactory = $this->get('form.factory');

        $formData = [
           'imagen_secundaria' => $product->imagen_secundaria
        ];
        
        $form = $formFactory->createBuilder(FormType::class, $formData)->add('imagen_secundaria', FileType::class, ['mapped' => false])->getForm();
            
        return $this->get('twig')->render(_PS_MODULE_DIR_.'ps_addproductfields/views/templates/hook/display-admin-products-extra.html.twig', ['form' => $form->createView()]) ;
    }

}

Link to comment
Share on other sites

  • 1 year later...
On 9/11/2020 at 6:44 PM, miguelcubica said:

Hi.

I have a problem creating an extra field for the products. I do it through a module and, if this field is file type, in this case I want to add a secondary image to the product, the information about this new field is not available in the hookActionProductSave.

The field is displayed correctly, but I can't upload the image or get its data.


I show my code to see if you can help me.


Thank you very much.

 

Module code:

 

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

use PrestaShopBundle\Form\Admin\Type\TranslateType;
use PrestaShopBundle\Form\Admin\Type\FormattedTextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use PrestaShopBundle\Form\Admin\Type\SwitchType;
use Symfony\Component\Form\AbstractType;

use Symfony\Component\HttpFoundation\File\Exception\FileException;
use App\Service\FileUploader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\String\Slugger\SluggerInterface;

use App\Entity\Product;
use App\Form\ProductType;

class Ps_AddProductFields extends Module{
    public function __construct(){
        $this->name = 'ps_addproductfields';
        $this->tab = 'front_office_features';
        $this->version = '2.0.0';
        $this->author = -----';

        $this->bootstrap = true;
        parent::__construct();

        $this->displayName = $this->getTranslator()->trans('Añade campos al producto', array(), 'Modules.Ps_Addproductfields.Admin');
        $this->description = $this->getTranslator()->trans('Añade ciertos campos a la administración de la ficha de producto.', array(), 'Modules.Ps_Addproductfields.Admin');
        $this->ps_versions_compliancy = array('min' => '1.7.1.0', 'max' => _PS_VERSION_);
    }

    public function install(){
        return parent::install() && $this->registerHook('displayAdminProductsExtra') && $this->registerHook('actionProductSave') && $this->registerHook('actionProductUpdate') && $this->registerHook('actionAdminControllerSetMedia');
    }

    public function uninstall(){
        if (!parent::uninstall()) {
            return false;
        }
        return true;
    }
    
    public function hookActionAdminControllerSetMedia($params){
        $this->context->controller->addJS($this->_path.'assets/js/ps_addproductfields.js');
    }

    public function hookActionProductSave($params){
        $productAdapter = $this->get('prestashop.adapter.data_provider.product');
        if($_REQUEST['form']['id_product']){
            $product = $productAdapter->getProduct($_REQUEST['form']['id_product']);
            
            $imagen_secundaria = $_REQUEST['form']['imagen_secundaria']->getData();
            if($imagen_secundaria->move(_PS_MODULE_DIR_.'/ps_addproductfields/img/product', $imagen_secundaria->getClientOriginalName())){
                if(!Db::getInstance()->update('product', array('imagen_secundaria' => $imagen_secundaria->getClientOriginalName()), 'id_product = ' .$_REQUEST['form']['id_product'])){
                    $this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();
                } else {
                    $this->context->controller->_errors[] = Tools::displayError('Error: No se ha podido guardar la imagen secundaria.');
                }
            } else {
                $this->context->controller->_errors[] = Tools::displayError('Error: No se ha podido guardar la imagen secundaria.');
            }
        }
    }
    
    public function hookDisplayAdminProductsExtra($params){
        //$this->context->controller->addJS($this->_path.'assets/js/ps_addproductfields.js');
        
        $productAdapter = $this->get('prestashop.adapter.data_provider.product');
        $product = $productAdapter->getProduct($params['id_product']);
        
        $formFactory = $this->get('form.factory');

        $formData = [
           'imagen_secundaria' => $product->imagen_secundaria
        ];
        
        $form = $formFactory->createBuilder(FormType::class, $formData)->add('imagen_secundaria', FileType::class, ['mapped' => false])->getForm();
            
        return $this->get('twig')->render(_PS_MODULE_DIR_.'ps_addproductfields/views/templates/hook/display-admin-products-extra.html.twig', ['form' => $form->createView()]) ;
    }

}

 Hello 

I am also facing this problem can you please share the project.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...