1

I need to get and add a new Core table data. So, I tried based on get custom table data way(Model, ResourceModel) but it's not working for me.

For example I need to collect customer_group data

What I did: (Using Api)

      <?php
        namespace Vendor\Module\Controller\Index;
        use Magento\Checkout\Model\Cart as CustomerCart;
        use Magento\Customer\Model\Session;
        use Magento\Customer\Api\Data\GroupInterfaceFactory;
        use Magento\Customer\Api\Data\GroupInterface;
        use Magento\Customer\Api\GroupRepositoryInterface;

        class Test extends \Magento\Framework\App\Action\Action
        {

            protected  $_groupDataFactory;

            protected $dataObjectProcessor;

            public function __construct(
                \Magento\Framework\App\Action\Context $context,
                Session $csession,
                \Psr\Log\LoggerInterface $logger,
                \Magento\Framework\Registry $coreRegistry,
                GroupRepositoryInterface $groupRepository,
                GroupInterfaceFactory $groupDataFactory,
                \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor

            )
            {
                $this->dataObjectProcessor = $dataObjectProcessor;
                parent::__construct($context,$groupRepository,
                    $groupDataFactory);
            }

            public function execute()
            {

                        try
                        {
                         $customerGroup = $this->_groupDataFactory->create();
                         $this->_logger->addDebug(serialize($customerGroup));


                        }
                        catch (\Exception $e)
                        {
                           // echo $e;
                        }
                    }
                }
            }

        }

Model Way:

           protected $_groupFactory;

            public function __construct(
                \Magento\Framework\View\Element\Template\Context $context,
                \Magento\Customer\Model\Group $groupFactory

            )
            {
                parent::__construct($context);
                $this->_groupFactory=$groupFactory;
            }

            public function getTableData()
            {

                $result=$this->_groupFactory->create()->getCollection();

                var_dump($result);

            }

Both above way not working for me , Suggest me How to get and add a data to core table.

0

You injected the right class which is Magento\Customer\Api\GroupRepositoryInterface but you didn't use it.

To get the list of customer groups you can call:

$this->groupRepository->getList();

PS: don't forget to assign this class to a class variable of yours such as protected $groupRepository if you want this code to work

To create new customer groups you first need to use Magento\Customer\Api\Data\GroupInterfaceFactory to create the customer group:

$customerGroup = $this->groupDataFactory->create();
$customerGroup->setCode('customer_group_custom_code');
$customerGroup->setTaxClassId($taxClassId);

NB: please replace the code and tax class id with what you need

And then you can save it using the group repository:

$this->groupRepository->save($customerGroup);
2

Your Answer

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

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