0

I have a custom attribute that i will create my product configurable according to it and every product has his specific values so i want to filter the options when creation product to just show me the associated option of this product. I tried this via Ui/DataProvider Modifier but nothing changed i still see all the options an d not filtered ones: Here is my code

<?php

namespace Vendor\MyModule\Ui\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Model\Locator\LocatorInterface;
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Framework\UrlInterface;
use Magento\Ui\Component\Form\Field;

/**
 * Data provider for "Custom Attribute" field of product page
 */
class PrixVente extends AbstractModifier
{
    //const SUGGEST_FILTER_URI = 'Mdweb_ConfigAttribute/something/suggestCustomAttr';

    /**
     * @param LocatorInterface $locator
     * @param UrlInterface $urlBuilder
     * @param ArrayManager $arrayManager
     */
    protected $eavConfig;
    protected $registry;

    public function __construct(
        LocatorInterface $locator,
        UrlInterface $urlBuilder,
        ArrayManager $arrayManager,
        \Magento\Eav\Model\Config $eavConfig,
        \Magento\Framework\Registry $registry
    )
    {
        $this->locator = $locator;
        $this->urlBuilder = $urlBuilder;
        $this->arrayManager = $arrayManager;
        $this->_eavConfig = $eavConfig;
        $this->registry = $registry;

    }

    /**
     * {@inheritdoc}
     */
    public function modifyMeta(array $meta)
    {
        $meta = $this->customiseCustomAttrField($meta);

        return $meta;
    }

    /**
     * {@inheritdoc}
     */
    public function modifyData(array $data)
    {
        return $data;
    }

    /**
     * Customise Custom Attribute field
     *
     * @param array $meta
     *
     * @return array
     */
    protected function customiseCustomAttrField(array $meta)
    {
        $fieldCode = 'choix_prix_product'; //your custom attribute code

        $elementPath = $this->arrayManager->findPath($fieldCode, $meta, null, 'children');
        $containerPath = $this->arrayManager->findPath(static::CONTAINER_PREFIX . $fieldCode, $meta, null, 'children');

        if (!$elementPath) {
            return $meta;
        }



        $meta = $this->arrayManager->merge(
            $containerPath,
            $meta,
            [
                'arguments' => [
                    'data' => [
                        'config' => [
                            'dataScope' => '',
                            'formElement' => 'container',
                            'componentType' => 'container',

                            'component' => 'Magento_Ui/js/form/components/group',
                            'scopeLabel' => __('[GLOBAL]'),
                        ],
                    ],
                ],
                'children' => [
                    $fieldCode => [
                        'arguments' => [
                            'data' => [
                                'config' => [
                                    'componentType' => Field::NAME,
                                    'formElement' => 'select',
                                    'options' => $this->getOptions(),
                                    'config' => [
                                        'dataScope' => $fieldCode,

                                    ],
                                ],
                            ],
                        ],
                    ]
                ]
            ]
        );

        return $meta;
    }

    /**
     * Retrieve custom attribute collection
     *
     * @return array
     */

    protected function toOptionArray()
    {
        // Get your options
        $attributeCode = "choix_prix_product";

        $attribute = $this->_eavConfig->getAttribute('catalog_product', $attributeCode);
        $options = $attribute->getSource()->getAllOptions();

        $_product = $this->registry->registry('current_product');

        $idprod = $_product->getId();
        $tab = [];

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

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


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

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        $tableName = $resource->getTableName('table_prix');


        $fields = array('prix_unitaire', 'prix_id');


        $sql = $connection->select()
            ->from($tableName, $fields)
            ->where('code_famille' . '=?', $var1)
            ->where('code_nom_commercial' . '=?', $var2)
            ->join('table_choix',
                'table_choix.choix_id = table_prix.code_choix',
                [
                    'designation_choix'
                ]);

        $result = $connection->fetchAll($sql);
        foreach ($options as $option) {
            if ($result) {
                $i = 0;
                foreach ($result as $elt) {

                    if ($option['label'] == " Choix" . $elt['designation_choix'] . "-" . $elt['prix_unitaire'] . "£") {

                        $tab[] = [['label' => $option['label'], 'value' => $option['value']]];

                    }
                    $i++;

                }

            }
        }

        return $tab;



    }
    public function getOptions()
    {


        return $this->toOptionArray();

    }
}

any help please

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.