0

I want to recuperate the value of the input of two attributes of the product to do a request o database to recuperate some value from the custom table according to the value of the two attributes before saving the product. So how to proceed and where to find the form of the new product or the phtml

Thanks in advance i tried the solution gived and this is my code in observer

<?php

namespace Modif\NameProduct\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productedit implements ObserverInterface
{
    /**
     * @var \BO\Famille\Model\Family
     */
    protected $_family;
    /**
     * @var \BO\NomCommercial\Model\Businessname
     */
    protected $_nc;
    /**
     * @var \BO\Epaisseur\Model\Epaisseur
     */
    protected $_ep;
    /**
     * @var \BO\Couleur\Model\Couleur
     */
    protected $_color;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \BO\Famille\Model\Family $family
     * @param \BO\NomCommercial\Model\Businessname $nc
     * @param \BO\Epaisseur\Model\Epaisseur $ep
     * @param \BO\Couleur\Model\Couleur $color
     * @param \Magento\Framework\App\ResourceConnection $resource
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \BO\Famille\Model\Family $family,
        \BO\NomCommercial\Model\Businessname $nc,
        \BO\Epaisseur\Model\Epaisseur $ep,
        \BO\Couleur\Model\Couleur $color,
        \Magento\Framework\App\ResourceConnection $resource,
        array $data = []
    )
    {
        $this->_family = $family;
        $this->_nc = $nc;
        $this->_ep = $ep;
        $this->_color = $color;
        $this->_resource = $resource;

        parent::__construct(
            $context,
            $data
        );
    }


    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getProduct();  // you will get product object
        //NOW CHANGE THE PRODUCT ATTRIBUTE OR WHAT YOU WANT

        $_sku = $product->getSku(); // for sku

        $var1 = substr($_sku, 0, 1);

        $var2 = substr($_sku, 1, 2);

        $var3 = substr($_sku, 3, 2);
        $var4 = substr($_sku, 5, 3);

        $int1 = (int)$var1;

        $des_f = $this->_family->getCollection()->addFieldToSelect('designation_f')->addFieldToFilter('code_famille', array('eq' => $int1));
        $des_nc= $this->_nc->getCollection()->addFieldToSelect('nom_commercial')->addFieldToFilter('code_nom_commercial', array('eq' => $var2));
        $des_ep= $this->_ep->getCollection()->addFieldToSelect('designation_ep')->addFieldToFilter('code_epaisseur', array('eq' => $var3));
        $des_col= $this->_ep->getCollection()->addFieldToSelect('designation_col')->addFieldToFilter('code_couleur', array('eq' => $var4));
        $designation_name= $des_f." ".$des_nc." ". $des_ep." ".$des_col ;
        $product->setName($designation_name);





    }
}

but i get 500 error and i realised that i should put a name or i don't want to put a thing in name i just want to get the name according to the sku value automatically so what should i change

1
  • you are calling this $designation_name= $des_f." ".$des_nc." ". $des_ep." ".$des_col ; but all the variables you are concatenating as strings are not strings. They are collection objects that cannot be converted to strings.
    – Marius
    Commented May 3, 2019 at 10:38

1 Answer 1

1

Here you need to use Magento event observer.

  1. create events.xml at the following location.

app\code\Vendor\Extension\etc\events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="catalog_product_save_after_cusomizationcode" instance="Vendor\Extension\Observer\Productedit" />
    </event>
</config>

2. Now create the observer file at following location.

app\code\Vendor\Extension\Observer\Productedit.php

<?php
namespace Vendor\Extension\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productedit implements ObserverInterface
{
   public function execute(\Magento\Framework\Event\Observer $observer)
   {
        $product = $observer->getEvent()->getProduct(); 
        //NOW CHANGE THE PRODUCT ATTRIBUTE OR WHAT YOU WANT

   }
}
14
  • thanks for your answer but is'nt that after saving ??? Commented May 3, 2019 at 8:05
  • i need to set the name of product when creationg the product not after saving product Commented May 3, 2019 at 8:16
  • Yes, but if you change it here, then it also same that you want. Commented May 3, 2019 at 9:47
  • ok i will try that and tell you the result and really thank you a lot for your replay Commented May 3, 2019 at 9:52
  • i get error 500 when trying to save product i just want to leave the name empty and when i save the product i want to find the name as the change i made so wha't the prodblem here is my code Commented May 3, 2019 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.