0

I have created a custom attribute for product with static options but i want them to be dynamic i tried to created it with dynamic value via the class Options but i got error and i can't resolve it so after the suggestion of Alex one member in this forum i decided to change the value oof options accoring to some calcul of the currect product that i created and then make my product config accoring to this attribute he also gave me this link Magento2 - programmatically add product attribute options but i feel lost please help me achieve this task and thanks in advance

code :`

namespace Mdweb\ConfigAttribute\Model\Config\Source;

use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory; use Magento\Framework\DB\Ddl\Table;

class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource { /** * @var OptionFactory */ protected $optionFactory; protected $registry; protected $prixchoix; protected $colchoix; /** * @var \BO\Choix\Model\Choix */ protected $_choice;

/**
 * @param OptionFactory $optionFactory
 */
public function __construct(OptionFactory $optionFactory, \Magento\Framework\Registry $registry,
                            \BO\Prix\Model\Prix $choixprix,
                            \BO\Choix\Model\Choix $choix)
{
    //you can use this if you want to prepare options dynamically

    $this->optionFactory = $optionFactory;
    $this->registry = $registry;
    $this->prixchoix = $choixprix;
    $this->_choice = $choix;
}

/**
 * Get all options
 *
 * @return array
 */
public function getAllOptions()
{

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

    $_sku = $_product->getSku();
    if (isset($_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');


    $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);

    if ($result) {
        $i = 0;
        foreach ($result as $elt) {
            $this->_options[$i] = ['label' => " Choix" . $elt['designation_choix'] . "-" . $elt['prix_unitaire'] . "£", 'value' => " Choix" . $elt['designation_choix'] . "-" . $elt['prix_unitaire'] . "£"];
            $i++;
        }


    }  else{
        $this->_options = [ ['label' => __('No'), 'value'=>'0'], ['label' => __('Yes'), 'value'=>'1'], ['label' => __('Other'), 'value'=>'2'] ];
    }


    return  $this->_options;


}}`

and this is the result when i try to access the detailed product attribute enter image description here

Teja Bhagavan Kollepara
  • 3,816
  • 5
  • 32
  • 69
Developper Magento
  • 1,603
  • 2
  • 14
  • 47

1 Answers1

0

Try This :-

<?php


namespace Mdweb\ConfigAttribute\Model\Config\Source;

use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory;
use Magento\Framework\DB\Ddl\Table;


class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
    /**
     * @var OptionFactory
     */
    protected $optionFactory;
    protected $registry;
    protected $prixchoix;
    protected $colchoix;
    /**
     * @var \BO\Choix\Model\Choix
     */
    protected $_choice;


    /**
     * @param OptionFactory $optionFactory
     */
    public function __construct(OptionFactory $optionFactory, \Magento\Framework\Registry $registry,
                                \BO\Prix\Model\Prix $choixprix,
                                \BO\Choix\Model\Choix $choix)
    {
        //you can use this if you want to prepare options dynamically

        $this->optionFactory = $optionFactory;
        $this->registry = $registry;
        $this->prixchoix = $choixprix;
        $this->_choice = $choix;
    }

    /**
     * Get all options
     *
     * @return array
     */
    public function toOptionArray()
    {

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

        $_sku = $_product->getSku();
        if (isset($_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');


            $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);

            if ($result) {
                $i = 0;
                foreach ($result as $elt) {
                    $this->_options[$i] = ['label' => " Choix" . $elt['designation_choix'] . "-" . $elt['prix_unitaire'] . "£", 'value' => " Choix" . $elt['designation_choix'] . "-" . $elt['prix_unitaire'] . "£"];
                    $i++;
                }


            } else {
                $this->_options = [['label' => __('No'), 'value' => '0'], ['label' => __('Yes'), 'value' => '1'], ['label' => __('Other'), 'value' => '2']];
            }


            return $this->_options;


        }
    }

    public function getAllOptions()
    {
        return $this->toOptionArray();
    }
}
Ronak Rathod
  • 6,322
  • 17
  • 42