如何在magento2中获取信息签出来宾用户

时间:2017-02-07 13:31:37

标签: php plugins payment checkout magento2

我正在开发一个magento 2.1.1支付模块,问题是当我用( LOGGED USER )结帐时,我能够获得有关消费者的所有信息,

但如果我用( GUEST USER )和var_dump这个对象结账

  

$这 - > _checkoutSession->为getQuote()

它显示了有关我的卡(价格,产品,..)的所有信息,但有关消费者的所有其他数据均为空

这是我的代码

<?php 

namespace Fpay\Fastpay\Controller\Payment; 

use Magento\Framework\Controller\ResultFactory;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Sales\Model\Order;
use Magento\Quote\Api\GuestCartManagementInterface;


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

    protected $_customerSession;
    protected $resultPageFactory;
    protected $_paymentMethod;
    protected $_checkoutSession;
    protected $checkout;
    protected $cartManagement;
    protected $orderRepository;
    protected $_scopeConfig;
    protected $guestcartManagement;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param SessionManagerInterface $session
     * @param \Magento\Checkout\Model\Session $checkoutSession
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Fpay\Fastpay\Model\PaymentMethod $paymentMethod,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        CartManagementInterface $cartManagement,
        GuestCartManagementInterface $guestcartManagement,
        array $data = []
    ) {
        $this->_customerSession = $customerSession;
        $this->_paymentMethod   = $paymentMethod;
        $this->_checkoutSession = $checkoutSession;
        $this->cartManagement   = $cartManagement;
        $this->guestcartManagement = $guestcartManagement;
        $this->orderRepository  = $orderRepository;
        $this->_scopeConfig     = $scopeConfig;

        parent::__construct($context);
    }

    protected function log($message, $level=null) {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($message);
    }

    /**
     * Return checkout session.
     *
     * @return Magento\Backend\Model\Session|Magento\Backend\Model\Session\Quote
     */
    public function getCheckout()
    {
        $sessionClass = \Magento\Checkout\Model\Session::class;
        if ($this->isBackend()) {
            $sessionClass = \Magento\Backend\Model\Session\Quote::class;
        }

        return $this->objectManager->get($sessionClass);
    }  

    /**
     * Return true if this is a backend session.
     *
     * @return bool
     */
    public function isBackend()
    {
        return $this->appState->getAreaCode() == \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE;
    }

    public function execute(  )
    {

        try {   

            // Check The customer {Simple User, Guest User}

            // Object Contain Session Data
            $session = $this->_objectManager->get('Magento\Customer\Model\Session');

            if($session->isLoggedIn()) {

                // Logged User
                echo "Logged User =====================================<br>";
                $orderIncrementId = $this->cartManagement->placeOrder($this->_checkoutSession->getQuote()->getId());
                $order = $this->orderRepository->get($orderIncrementId);
                echo "Order Increment ID : " . $orderIncrementId . '<br>';
                echo "Order id           : " . $order->getId() . '<br>';
                echo 'Email              :' . $order->getCustomerEmail() . '<br>';
                echo "=================================================<br>";

            } else {

                // Guest User 
                echo "Guest User =====================================<br>";
                $order = $this->_paymentMethod->getFastpayCheckoutRedirection();
                echo "Order ID           : " . $order->getId() . '<br>';
                echo 'Email              :'  . $order->getCustomerEmail() . '<br>';
                echo "================================================<br>";

            }

            // clear quote data
            $this->getCheckout()->unsLastQuoteId()
                                ->unsLastSuccessQuoteId()
                                ->clearHelperData();


        } catch (\Exception $exception) {

            echo  $exception ;

        }

    }

} ?>

这是一个显示会话结帐的包含的屏幕截图

enter image description here

有关如何解决此问题的任何想法?

0 个答案:

没有答案