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

0

1 Answer 1

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();
    }
}
6
  • ok i will check that May 7, 2019 at 9:00
  • still having same white page May 7, 2019 at 9:01
  • i really can't undrstand this article magento.stackexchange.com/questions/103934/… May 7, 2019 at 9:02
  • i think that i miss something like /* new attribute option */ $this->_option->setValue($name); $this->_attributeOptionLabel->setStoreId(0); $this->_attributeOptionLabel->setLabel($name); $this->_option->setLabel($this->_attributeOptionLabel); $this->_option->setStoreLabels([$this->_attributeOptionLabel]); $this->_option->setSortOrder(0); $this->_option->setIsDefault(false); May 7, 2019 at 9:03
  • but where to put it and how May 7, 2019 at 9:03

Your Answer

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

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