1

I need to make the cart page accessible to an external user?

In Magento 1 I had this in my Controller:

class Sostanza_Livehelp_IndexController extends Mage_Core_Controller_Front_Action {

    protected $_cartInfo;

    public function getcartlhinfoAction() {
    $query_params = $this->getRequest()->getParams();
    if(isset($query_params["MY_PARAM"]){

        $cart = Mage::getUrl('checkout/cart/', array(
                            '_secure' => true,
                            '_query' => array('SID' => $query_params["MY_PARAM"]),  
                            '_store_to_url' => true)
        ); // lastly add store to our url if needed

        $cookieName = "frontend";
        // AN UPPERCASE VERSION OF THIS "frontend" is available in Mag2
        //http://docs.magento.com/m2/ce/user_guide/stores/cookie-reference.html
        $cookieVal = $query_params["MY_PARAM"];
        $cookie = Mage::getSingleton('core/cookie');
        $cookie->set($cookieName, $cookieVal);
        $this->_redirectUrl($cart);
    }
    else {
        $session_id = Mage::getSingleton("core/session")->getEncryptedSessionId();
        $cart_url = Mage::getUrl() . "livehelp/index/getcartlhinfo/?MY_PARAM=" . $session_id;
        $this->_cartInfo = "<a href='";
        $this->_cartInfo .= $cart_url;
        $this->_cartInfo .= "' target='_blank' title='Magento Cart' class='magento-cart'>";
        $this->_cartInfo .= "Magento Cart";
        $this->_cartInfo .= "</a>";

        $cart_encoded = urlencode($this->_cartInfo);

        $this->getResponse()->setHeader("Content-type", "application/json");
        $this->getResponse()->setBody($cart_encoded);
    }

}

So basically if you call /livehelp/getcartlhinfo/ without param it will behave like a rest service and print the url with the session id, otherwise it will redirect you to associated user cart

I already have the route for a custom frontend page so basically I only need to convert this code to read cookie/session and do the redirect in Magento 2

Thank you so much

EDIT

Till now I accomplished this:

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

    protected $_resultPageFactory;
    protected $_urlBuilder;
    protected $_jsonResult;
    protected $cartParams = array(
        "_secure" => true,
        "_store_to_url" => true,
        "_query" => array()
    );

    CONST PARAM_NAME = "MY_PARAM";

    public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory
    , \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
    , \Magento\Framework\UrlInterface $urlBuilder
    ) {
        $this->_resultPageFactory = $resultPageFactory;
        $this->_urlBuilder = $urlBuilder;
        $this->_jsonResult = $resultJsonFactory;
        parent::__construct($context);
    }

    public function execute() {
        $params = $this->getRequest()->getParams();
        $cartParams = $this->cartParams;
        if (isset($params[self::PARAM_NAME])) {
            $cartParams["_query"] = array("SID" => $params[self::PARAM_NAME]);
            $cart = $this->_urlBuilder->getUrl("checkout/cart/", $cartParams);
            echo $cart;
            $resultPage = $this->_resultPageFactory->create();
            return $resultPage;
        } else {
           $session_id = "5";
        $cartParams["_query"] = array(self::PARAM_NAME => $session_id);
        $cart_url = $this->_urlBuilder->getUrl("livehelp/index/cart/", $cartParams);

        $cartInfo = "<a href='";
        $cartInfo .= $cart_url;
        $cartInfo .= "' target='_blank' title='Magento Cart' class='magento-cart'>";
        $cartInfo .= "Magento Cart";
        $cartInfo .= "</a>";
        $cart_encoded = urlencode($cartInfo);
        return $this->_jsonResult->create()->setData($cart_encoded);
        }
    }

}

This has the same behaviour, if MY_PARAM is in the URL then it prints something, otherwise it returns a json response with the url to call.

What I miss now is

  1. Acquire session ID
  2. Redirect to cart

The idea is to make cart visible to anyone who has the url

2

I figured it out by deep searching the web. It would be nice to know if this is the proper way of doing it. Anyway this is the Magento2 version of my above code :)

MY PAGE ACTION

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

    protected $_resultPageFactory;
    protected $_session;
    protected $_urlBuilder;
    protected $_urlParams;
    protected $cartParams = array(
        "_secure" => true,
        "_store_to_url" => true,
        "_query" => array()
    );
    protected $cartUrl = "";

    CONST PARAM_NAME = "LH_SID";
    CONST COOKIE_NAME = "FRONTEND";

    public function __construct(Context $context
    , \Magento\Framework\App\Response\Http $response
    , \Magento\Framework\View\Result\PageFactory $pageFactory
    , \Magento\Framework\Session\SessionManager $sessionManager
    , \Magento\Framework\UrlInterface $urlBuilder) {

        $this->response = $response;
        $this->_resultPageFactory = $pageFactory;
        $this->_session = $sessionManager;
        $this->_urlBuilder = $urlBuilder;

        parent::__construct($context);
    }

    protected function getSid() {
        return $this->_session->getSessionId();
    }

    public function execute() {
        $this->_urlParams = $this->getRequest()->getParams();
        $params = $this->_urlParams;
        if (isset($params[self::PARAM_NAME])) {
            $cartParams["_query"] = array("SID" => $params[self::PARAM_NAME]);

            $this->response->setRedirect($this->_urlBuilder->getUrl("checkout/cart/", $cartParams));
            $resultPage = $this->_resultPageFactory->create();
            return $resultPage;
        } else {
            $cartUrl = $this->_urlBuilder->getUrl("livehelp/index/cart");
            $cartUrl .= "?" . self::PARAM_NAME . "=" . $this->getSid();
            $cartInfo = "<a href='" . $cartUrl . "' target='_blank' title='Magento Cart' class='magento-cart'>Magento Cart</a>";
            $cartEncoded = urlencode($cartInfo);
            return $this->getResponse()->representJson($cartEncoded);
        }
    }

}
| improve this answer | |
  • Thank you, this is what i was after all this time ... $this->_session->getSessionId(); – BENN1TH Dec 1 '18 at 10:52

Your Answer

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