Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/cartforge.co/app/code/Webkul/PrivateShop/Model/Plugin/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/cartforge.co/app/code/Webkul/PrivateShop/Model/Plugin/CustomerDataProviderPlugin.php
<?php
/**
 * Webkul Software
 *
 * @category  Webkul
 * @package   Webkul_PrivateShop
 * @author    Webkul Software Private Limited
 * @copyright Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */

namespace Webkul\PrivateShop\Model\Plugin;

use Magento\Customer\Model\Customer\DataProviderWithDefaultAddresses;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;

class CustomerDataProviderPlugin
{
    /**
     * @var CustomerRepositoryInterface
     */
    protected $customerFactory;

    /**
     * @var \Magento\Framework\Serialize\Serializer\Json
     */
    protected $jsonSerializer;

    /**
     * @var \Webkul\PrivateShop\Api\Data\PrivateGroupInterfaceFactory
     */
    protected $privateGroupFactory;

    /**
     * Constructor
     *
     * @param CustomerRepositoryInterface $customerFactory
     * @param \Webkul\PrivateShop\Api\Data\PrivateGroupInterfaceFactory $privateGroupFactory
     * @param \Magento\Framework\Serialize\Serializer\Json $jsonSerializer
     */
    public function __construct(
        CustomerRepositoryInterface $customerFactory,
        \Webkul\PrivateShop\Api\Data\PrivateGroupInterfaceFactory $privateGroupFactory,
        \Magento\Framework\Serialize\Serializer\Json $jsonSerializer
    ) {
        $this->customerFactory = $customerFactory;
        $this->privateGroupFactory = $privateGroupFactory;
        $this->jsonSerializer = $jsonSerializer;
    }

    /**
     * Add grops to the Customer Form
     *
     * @param DataProviderWithDefaultAddresses $subject
     * @param array $results
     * @see DataProviderWithDefaultAddresses::getData()
     * @return array $results
     */
    public function afterGetData(DataProviderWithDefaultAddresses $subject, $results)
    {
        if ($results) {
            $customerId = array_keys($results)[0];
            $groupsData = $this->getAttributeData($customerId);
            
            if ($groupsData) {
                $groups = $this->jsonSerializer->unserialize(
                    $groupsData->getValue()
                );
                $collection = $this->privateGroupFactory->create()
                        ->getCollection()
                        ->addFieldToFilter('entity_id', ['in' => $groups]);
                    
                $availableIds = [];
                foreach ($collection as $group) {
                    $availableIds[] = $group->getId();
                }
                $results[$customerId]['private_shop']['data'] = $availableIds;
            }
        }
        return $results;
    }

    /**
     * Get attribute data.
     *
     * @param int $customerId
     * @return \Magento\Framework\Api\AttributeValue|null
     */
    private function getAttributeData($customerId)
    {
        try {
            $customer = $this->customerFactory->getById($customerId);
            return $customer->getCustomAttribute('customer_private_group');
        } catch (NoSuchEntityException $e) {
            return null;
        }
    }
}

Spamworldpro Mini