首页>Magento>source

我想在自定义模块中获取会话跟踪.没有与会话相关的代码,它工作正常,但是当我添加它时它会破坏模块

namespace Scaledesk\Trails\Block;
use \Magento\Framework\View\Element\Template;
use \Magento\Framework\View\Element\Template\Context;
use \Scaledesk\Trails\Model\ResourceModel\Post\Collection as PostCollection;
use \Scaledesk\Trails\Model\ResourceModel\Post\CollectionFactory as PostCollectionFactory;
use \Scaledesk\Trails\Model\Post;
class Users extends Template
{
    /**
     * CollectionFactory
     * @var null|CollectionFactory
     */
    protected $_postCollectionFactory = null;

    protected $customerSession;
    /**
     * Constructor
     *
     * @param Context $context
     * @param PostCollectionFactory $postCollectionFactory
     * @param \Magento\Customer\Model\Session $customerSession
     * @param array $data
     */
    public function __construct(
        Context $context,
        PostCollectionFactory $postCollectionFactory,
        \Magento\Customer\Model\Session $customerSession,
        array $data = []
    ) {
        $this->customerSession = $customerSession;
        $this->_postCollectionFactory = $postCollectionFactory;
        parent::__construct($context, $data);
    }
    /**
     * @return Post[]
     */
    public function getPost()
    {
        /** @var PostCollection $postCollection */
        $customerId=$this->customerSession->getCustomer()->getId();

        $sort=1;
        $postCollection = $this->_postCollectionFactory->create();
        $postCollection->addFieldToSelect('*');
        $postCollection->addFieldToFilter('main_table.entity_id',$customerId);
        $postCollection->getSelect()->join( array('customer_entity'=> $postCollection->getTable('customer_entity')), 'customer_entity.entity_id = main_table.entity_id');
        $postCollection->setOrder('trails_id', ($sort==1 ? 'asc':'desc'));
        $postCollection->load();
//        var_dump($postCollection->getSelect()->__toString());
//        die();
//        var_dump($postCollection->getItems()); die();
        return $postCollection->getItems();

    }
    /**
     * For a given post, returns its url
     * @param Post $post
     * @return string
     */
    public function getPostUrl(
        Post $post
    ) {
        return $this->getUrl().'trails/post/view/id/'. $post->getId();
    }


}

$ postCollection->addFieldToFilter( 'main_table.entity_id',$客户ID);
  如何获得会话 customerSession->getCustomer()->getId()

最新回答
  • 2019-12-5
    1 #

    你需要注入 \Magento\Customer\Model\Session   用于在客户会话中设置和获取数据的类

    Using Factory Method

    protected $customerSession;
    public function _construct(
        ...
        \Magento\Customer\Model\SessionFactory $customerSession
        ...
    ) {
        ...
        $this->customerSession = $customerSession;
        ...
    }   
    public function setValue()
    {
        return $this->customerSession->setId('YourValue'); //set value in customer session
    }
    public function getValue()
    {
        return $this->customerSession->getId(); //Get value from customer session
    }
    

    Using ObjectManager (需要在构造函数中注入ObjectManager):

    $customerSession = $this->objectManager->create("Magento\Customer\Model\Session");
    if($customerSession->isLoggedIn()){
      echo $customerSession->getCustomerId();
    }
    

  • 2019-12-5
    2 #

    Ronak Chauhan是对的,您需要Magento \ Customer \ Model \ SessionFactory(注意工厂部分),您可以使用create()函数创建会话实例,然后尝试使用它,就像这样 :

    public function __construct(
        Context $context,
        PostCollectionFactory $postCollectionFactory,
        \Magento\Customer\Model\SessionFactory $customerSession,
        array $data = []
    ) {
        $this->customerSession = $customerSession->create();
        $this->_postCollectionFactory = $postCollectionFactory;
        parent::__construct($context, $data);
    }
    

    然后在你的代码中继续我认为你可以这样做:

    $customerId = $this->getLoggedinCustomerId();
    if(isset($customerId) && $customerId > 0) {
        $customerData = $this->customerSession->getCustomerData();
    

    然后使用您需要的会话信息.我希望它有所帮助。

  • magento2:从Magento 16导出产品并在Magento 209中导入产品
  • magento2:是否有Magento 2内置的机制可以使用拱形信用卡重新授权订单?