0

I want to add a custom checkbox list filed (not attribute)in product edit page. Therefore I call a phtml file in product admin and set checkbox input filed (which is dynamic).But when i am using observer "controller_action_catalog_product_save_entity_after" and print $_POST value my custom filed is not showing or coming.

i am shsreing the code

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="add_product_label_ids">
       <argument name="data" xsi:type="array">
           <item name="config" xsi:type="array">
               <item name="label" xsi:type="string" translate="true">Product Label</item>
               <item name="collapsible" xsi:type="boolean">true</item>
               <item name="sortOrder" xsi:type="number">9999</item>
           </item>
       </argument>
       <container name="add_product_label_ids" >
           <argument name="data" xsi:type="array">
               <item name="config" xsi:type="array">
                   <item name="sortOrder" xsi:type="number">1</item>
               </item>
           </argument>
           <htmlContent name="html_content_demo">
               <argument name="block" xsi:type="object">Custommization\Productlabelcustomization\Block\Adminhtml\Catalog\Product\Edit\Tab\Productlabel</argument>
           </htmlContent>
       </container>
   </fieldset>
</form>

then in Custommization\Productlabelcustomization\Block\Adminhtml\Catalog\Product\Edit\Tab\Productlabel

<?php
namespace Custommization\Productlabelcustomization\Block\Adminhtml\Catalog\Product\Edit\Tab;

use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Backend\Block\Widget\Tab\TabInterface;
use Magento\Framework\ObjectManagerInterface;
class Productlabel extends Generic implements TabInterface
{
    protected $_template = 'Productlabel.phtml';

    protected $_systemStore;

    protected $_scopeConfig;
    
    protected $_labellistFactory;
    
    protected $objectManager;
    
    protected $_registry;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Store\Model\System\Store $systemStore,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Custommization\Productlabelcustomization\Model\LabellistFactory $labellistFactory,
        \Magento\Framework\Registry $registry,
        ObjectManagerInterface $objectManager,
        array $data = []
    ) {
        $this->_systemStore = $systemStore;
        $this->_scopeConfig = $scopeConfig;
        $this->_labellistFactory = $labellistFactory;
        $this->objectManager = $objectManager;
        $this->_registry = $registry;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    public function getTabLabel()
    {
        return __('Actions');
    }

    public function getTabTitle()
    {
        return __('Actions');
    }

    public function canShowTab()
    {
        return true;
    }

    public function isHidden()
    {
        return false;
    }

    public function getReqest()
    {
        return $this->getRequest()->getParams();
    }
    
    public function getProductlabels()
    {
        $labelmodel = $this->_labellistFactory->create();
        return $labelmodel->getCollection();
    }
    public function getMediaUrl(){

        $media_dir = $this->objectManager->get('Magento\Store\Model\StoreManagerInterface')
            ->getStore()
            ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

        return $media_dir;
    }
    public function getCurrentProductsku()
    {        
        $productdata = $this->_registry->registry('current_product');
        return $productdata->getSku();
    }  
    public function getcheckval($labelid,$skuval){
        $serval = array();
        $flag = 0;
        $labelmodel = $this->_labellistFactory->create()->load($labelid);
        $labelarray = $labelmodel->getData();
        if (array_key_exists("cond_serialize",$labelarray)){
            $serval = json_decode($labelarray['cond_serialize'], true);
            if($serval != NULL){
                foreach($serval as $key=>$val){
                    if($key == 'conditions'){
                        //$data[] = $val; 
                        foreach($val as $nest){
                            if($nest['attribute'] == 'sku'){
                                if($nest['value'] == $skuval)
                                return $flag = 1;
                            }
                        }
                    }
                }
            }       
        }
        return $flag;
    }
    
}

Next in Productlabel.phtml

<div class="fieldset-wrapper-amasty-product-label" id="add-products-to-amasty-product-label">
    <fieldset class="fieldset-amasty-product-label" id="grop_fields-amasty-product-label">
        <legend class="legend-amasty-product-label">
            <span><?= $block->escapeHtml(__('Product Label')) ?></span>
        </legend>
        <br>
        <div class="store-scope-amasty-product-label">
            <div class="store-tree-amasty-product-label" id="add-products-to-amasty-product-label-content">
                <?php 
                    $skuval = $block->getCurrentProductsku();
                ?>
                
                 <?php foreach ($block->getProductlabels() as $_label):?>
                    <div class="label-name-amasty-product-label">
                        <?php
                                $checkcheked = $block->getcheckval($_label->getLabelId(),$skuval);                               
                               ?>
                        <input name="product[add_product_label_ids][]"
                               value="<?= $block->escapeHtmlAttr($_label->getLabelId()) ?>"
                               class="checkbox label-checkbox"
                               id="add_product_label_<?= $block->escapeHtmlAttr($_label->getLabelId()) ?>"
                               type="checkbox" 
                               <?php if ($checkcheked == 1):?>
                                    checked
                                <?php endif;?>
                               />
                        <img src="<?= $block->getMediaUrl().'amasty/amlabel/'.$_label->getProdImg() ?>" alt="<?= $_label->getName() ?>" >
                        <!--<label for="add_product_label_<?= $block->escapeHtmlAttr($_label->getLabelId()) ?>">
                            <?= $block->escapeHtml($_label->getName()) ?>
                        </label>-->
                        
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </fieldset>
</div>

using i get checkbox list in product edit page but when click on save under this observer "controller_action_catalog_product_save_entity_after" i am tring get print_r($POST) all post are showing expect my custom filed. can anyone help me out.

1 Answer 1

0

I solve it my own. just add data-form-part="product_form" in input filed

<input name="add_product_label_ids[<?= $block->escapeHtmlAttr($_label->getLabelId()) ?>]"
                               data-form-part="product_form"
                               value="<?= $block->escapeHtmlAttr($_label->getLabelId()) ?>"
                               class="checkbox label-checkbox"
                               id="add_product_label_<?= $block->escapeHtmlAttr($_label->getLabelId()) ?>"
                               type="checkbox" 
                               <?php if ($checkcheked == 1):?>
                                    checked
                                <?php endif;?>
                                
                               />

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.