本文整理汇总了PHP中Magento\Customer\Model\Session类的典型用法代码示例。如果您正苦于以下问题:PHP Session类的具体用法?PHP Session怎么用?PHP Session使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Session类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _preDispatchValidateCustomer
/**
* Make sure customer is valid, if logged in
*
* By default will add error messages and redirect to customer edit form
*
* @param bool $redirect - stop dispatch and redirect?
* @param bool $addErrors - add error messages?
* @return bool
*/
protected function _preDispatchValidateCustomer($redirect = true, $addErrors = true)
{
try {
$customerId = $this->_customerSession->getCustomerId();
$customer = $this->_customerAccountService->getCustomer($customerId);
} catch (NoSuchEntityException $e) {
return true;
}
if (isset($customer)) {
$validationResult = $this->_customerAccountService->validateCustomerData($customer, $this->_customerMetadataService->getAllCustomerAttributeMetadata());
if (!$validationResult->isValid()) {
if ($addErrors) {
foreach ($validationResult->getMessages() as $error) {
$this->messageManager->addError($error);
}
}
if ($redirect) {
$this->_redirect('customer/account/edit');
$this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
}
return false;
}
}
return true;
}
示例2: _construct
/**
* @return void
*/
protected function _construct()
{
parent::_construct();
$orders = $this->_orderCollectionFactory->create()->addFieldToSelect('*')->addFieldToFilter('customer_id', $this->_customerSession->getCustomerId())->addFieldToFilter('status', array('in' => $this->_orderConfig->getVisibleOnFrontStatuses()))->setOrder('created_at', 'desc');
$this->setOrders($orders);
$this->pageConfig->setTitle(__('My Orders'));
}
示例3: dispatch
/**
* Check customer authentication for some actions
*
* @param \Magento\Framework\App\RequestInterface $request
* @return \Magento\Framework\App\ResponseInterface
*/
public function dispatch(RequestInterface $request)
{
if (!$this->customerSession->authenticate()) {
$this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
}
return parent::dispatch($request);
}
示例4: testExecute
public function testExecute()
{
$event = $this->getMockBuilder('Magento\\Framework\\Event\\Observer')->disableOriginalConstructor()->getMock();
/** @var $event \Magento\Framework\Event\Observer */
$this->customerSession->expects($this->once())->method('setWishlistItemCount')->with($this->equalTo(0));
$this->observer->execute($event);
}
示例5: dispatch
/**
* Dispatch request
*
* @param RequestInterface $request
* @return ResponseInterface
* @throws NotFoundException
*/
public function dispatch(RequestInterface $request)
{
if (!$this->customerSession->authenticate()) {
$this->_actionFlag->set('', 'no-dispatch', true);
}
return parent::dispatch($request);
}
示例6: testToHtml
/**
* @magentoConfigFixture current_store persistent/options/customer 1
* @magentoConfigFixture current_store persistent/options/enabled 1
* @magentoConfigFixture current_store persistent/options/remember_enabled 1
* @magentoConfigFixture current_store persistent/options/remember_default 1
* @magentoAppArea frontend
* @magentoAppIsolation enabled
*/
public function testToHtml()
{
$this->_customerSession->loginById(1);
$translation = __('Not you?');
$this->assertStringMatchesFormat('%A<span>%A<a%Ahref="' . $this->_block->getHref() . '"%A>' . $translation . '</a>%A</span>%A', $this->_block->toHtml());
$this->_customerSession->logout();
}
示例7: execute
/**
* @param Observer $observer
* @return void
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute(Observer $observer)
{
if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && $this->taxHelper->isCatalogPriceDisplayAffectedByTax()) {
/** @var \Magento\Customer\Model\Data\Customer $customer */
$customer = $observer->getData('customer');
$customerGroupId = $customer->getGroupId();
$customerGroup = $this->groupRepository->getById($customerGroupId);
$customerTaxClassId = $customerGroup->getTaxClassId();
$this->customerSession->setCustomerTaxClassId($customerTaxClassId);
/** @var \Magento\Customer\Api\Data\AddressInterface[] $addresses */
$addresses = $customer->getAddresses();
if (isset($addresses)) {
$defaultShippingFound = false;
$defaultBillingFound = false;
foreach ($addresses as $address) {
if ($address->isDefaultBilling()) {
$defaultBillingFound = true;
$this->customerSession->setDefaultTaxBillingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null, 'postcode' => $address->getPostcode()]);
}
if ($address->isDefaultShipping()) {
$defaultShippingFound = true;
$this->customerSession->setDefaultTaxShippingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null, 'postcode' => $address->getPostcode()]);
}
if ($defaultShippingFound && $defaultBillingFound) {
break;
}
}
}
}
}
示例8: beforeDispatch
/**
* Authenticate user
*
* @param \Magento\Framework\App\ActionInterface $subject
* @param RequestInterface $request
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeDispatch(\Magento\Framework\App\ActionInterface $subject, RequestInterface $request)
{
$loginUrl = $this->customerUrl->getLoginUrl();
if (!$this->customerSession->authenticate($loginUrl)) {
$subject->getActionFlag()->set('', $subject::FLAG_NO_DISPATCH, true);
}
}
示例9: getBillingAgreements
/**
* Retrieve billing agreements collection
*
* @return \Magento\Paypal\Model\ResourceModel\Billing\Agreement\Collection
*/
public function getBillingAgreements()
{
if ($this->_billingAgreements === null) {
$this->_billingAgreements = $this->_agreementCollection->create()->addFieldToFilter('customer_id', $this->_customerSession->getCustomerId())->setOrder('agreement_id', 'desc');
}
return $this->_billingAgreements;
}
示例10: setUp
protected function setUp()
{
$this->markTestIncomplete();
$this->messageManager = $this->getMockForAbstractClass('Magento\\Framework\\Message\\ManagerInterface');
$this->config = $this->getMock('Magento\\Paypal\\Model\\Config', [], [], '', false);
$this->request = $this->getMock('Magento\\Framework\\App\\Request\\Http', [], [], '', false);
$this->quote = $this->getMock('Magento\\Quote\\Model\\Quote', [], [], '', false);
$this->quote->expects($this->any())->method('hasItems')->will($this->returnValue(true));
$this->redirect = $this->getMockForAbstractClass('Magento\\Framework\\App\\Response\\RedirectInterface');
$this->response = $this->getMock('Magento\\Framework\\App\\Response\\Http', [], [], '', false);
$this->customerData = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterface', [], [], '', false);
$this->checkout = $this->getMock('Magento\\Paypal\\Model\\Express\\Checkout', [], [], '', false);
$this->customerSession = $this->getMock('Magento\\Customer\\Model\\Session', [], [], '', false);
$this->customerSession->expects($this->any())->method('getCustomerDataObject')->will($this->returnValue($this->customerData));
$this->checkoutSession = $this->getMock('Magento\\Checkout\\Model\\Session', [], [], '', false);
$this->checkoutFactory = $this->getMock('Magento\\Paypal\\Model\\Express\\Checkout\\Factory', [], [], '', false);
$this->checkoutFactory->expects($this->any())->method('create')->will($this->returnValue($this->checkout));
$this->checkoutSession->expects($this->any())->method('getQuote')->will($this->returnValue($this->quote));
$this->session = $this->getMock('Magento\\Framework\\Session\\Generic', [], [], '', false);
$objectManager = $this->getMock('Magento\\Framework\\ObjectManagerInterface');
$this->objectManagerCallback = function ($className) {
if ($className == 'Magento\\Paypal\\Model\\Config') {
return $this->config;
}
return $this->getMock($className, [], [], '', false);
};
$objectManager->expects($this->any())->method('get')->will($this->returnCallback(function ($className) {
return call_user_func($this->objectManagerCallback, $className);
}));
$objectManager->expects($this->any())->method('create')->will($this->returnCallback(function ($className) {
return call_user_func($this->objectManagerCallback, $className);
}));
$helper = new ObjectManagerHelper($this);
$this->model = $helper->getObject('\\Magento\\\\Paypal\\Controller\\Express\\' . $this->name, ['messageManager' => $this->messageManager, 'response' => $this->response, 'redirect' => $this->redirect, 'request' => $this->request, 'customerSession' => $this->customerSession, 'checkoutSession' => $this->checkoutSession, 'checkoutFactory' => $this->checkoutFactory, 'paypalSession' => $this->session, 'objectManager' => $objectManager]);
}
示例11: getWishlist
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function getWishlist($wishlistId = null)
{
if ($this->wishlist) {
return $this->wishlist;
}
try {
if (!$wishlistId) {
$wishlistId = $this->request->getParam('wishlist_id');
}
$customerId = $this->customerSession->getCustomerId();
$wishlist = $this->wishlistFactory->create();
if (!$wishlistId && !$customerId) {
return $wishlist;
}
if ($wishlistId) {
$wishlist->load($wishlistId);
} elseif ($customerId) {
$wishlist->loadByCustomerId($customerId, true);
}
if (!$wishlist->getId() || $wishlist->getCustomerId() != $customerId) {
throw new \Magento\Framework\Exception\NoSuchEntityException(__('The requested Wish List doesn\'t exist.'));
}
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
$this->messageManager->addError($e->getMessage());
return false;
} catch (\Exception $e) {
$this->messageManager->addException($e, __('We can\'t create the Wish List right now.'));
return false;
}
$this->wishlist = $wishlist;
return $wishlist;
}
示例12: testSubscribe
public function testSubscribe()
{
$email = 'subscriber_email@magento.com';
$this->resource->expects($this->any())->method('loadByEmail')->willReturn(['subscriber_status' => 3, 'subscriber_email' => $email, 'name' => 'subscriber_name']);
$this->resource->expects($this->any())->method('getIdFieldName')->willReturn('id_field');
$this->scopeConfig->expects($this->any())->method('getValue')->willReturn(true);
$this->customerSession->expects($this->any())->method('isLoggedIn')->willReturn(true);
$customerDataModel = $this->getMock('\\Magento\\Customer\\Api\\Data\\CustomerInterface');
$this->customerSession->expects($this->any())->method('getCustomerDataObject')->willReturn($customerDataModel);
$this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(1);
$customerDataModel->expects($this->any())->method('getEmail')->willReturn($email);
$this->customerRepository->expects($this->any())->method('getById')->willReturn($customerDataModel);
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn(1);
$customerDataModel->expects($this->any())->method('getId')->willReturn(1);
$this->transportBuilder->expects($this->any())->method('setTemplateIdentifier')->willReturnSelf();
$this->transportBuilder->expects($this->any())->method('setTemplateOptions')->willReturnSelf();
$this->transportBuilder->expects($this->any())->method('setTemplateVars')->willReturnSelf();
$this->transportBuilder->expects($this->any())->method('setFrom')->willReturnSelf();
$this->transportBuilder->expects($this->any())->method('addTo')->willReturnSelf();
$storeModel = $this->getMock('\\Magento\\Store\\Model\\Store', ['getId'], [], '', false);
$this->scopeConfig->expects($this->any())->method('getValue')->willReturn('owner_email@magento.com');
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
$storeModel->expects($this->any())->method('getId')->willReturn(1);
$transport = $this->getMock('\\Magento\\Framework\\Mail\\TransportInterface');
$this->transportBuilder->expects($this->any())->method('getTransport')->willReturn($transport);
$transport->expects($this->any())->method('sendMessage')->willReturnSelf();
$inlineTranslation = $this->getMock('Magento\\Framework\\Translate\\Inline\\StateInterface');
$inlineTranslation->expects($this->any())->method('resume')->willReturnSelf();
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
$this->assertEquals(1, $this->subscriber->subscribe($email));
}
示例13: _prepareLayout
/**
* Prepare the layout of the address edit block.
*
* @return $this
*/
protected function _prepareLayout()
{
parent::_prepareLayout();
// Init address object
if ($addressId = $this->getRequest()->getParam('id')) {
try {
$this->_address = $this->_addressRepository->getById($addressId);
if ($this->_address->getCustomerId() != $this->_customerSession->getCustomerId()) {
$this->_address = null;
}
} catch (NoSuchEntityException $e) {
$this->_address = null;
}
}
if ($this->_address === null || !$this->_address->getId()) {
$this->_address = $this->addressDataFactory->create();
$customer = $this->getCustomer();
$this->_address->setPrefix($customer->getPrefix());
$this->_address->setFirstname($customer->getFirstname());
$this->_address->setMiddlename($customer->getMiddlename());
$this->_address->setLastname($customer->getLastname());
$this->_address->setSuffix($customer->getSuffix());
}
$this->pageConfig->getTitle()->set($this->getTitle());
if ($postedData = $this->_customerSession->getAddressFormData(true)) {
if (!empty($postedData['region_id']) || !empty($postedData['region'])) {
$postedData['region'] = ['region_id' => $postedData['region_id'], 'region' => $postedData['region']];
}
$this->dataObjectHelper->populateWithArray($this->_address, $postedData, '\\Magento\\Customer\\Api\\Data\\AddressInterface');
}
return $this;
}
示例14: testAroundDispatch
/**
* Test aroundDispatch
*/
public function testAroundDispatch()
{
$this->customerSessionMock->expects($this->once())->method('getCustomerGroupId')->will($this->returnValue(1));
$this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));
$this->httpContextMock->expects($this->atLeastOnce())->method('setValue')->will($this->returnValueMap([[Context::CONTEXT_GROUP, 'UAH', $this->httpContextMock], [Context::CONTEXT_AUTH, 0, $this->httpContextMock]]));
$this->assertEquals('ExpectedValue', $this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock));
}
示例15: beforeSubscribeCustomerById
public function beforeSubscribeCustomerById($subscriber, $customerId)
{
$subscriber->loadByCustomerId($customerId);
$storeId = $subscriber->getStoreId();
if ($this->_helper->isMonkeyEnabled($storeId)) {
$customer = $this->_customer->load($customerId);
$mergeVars = $this->_helper->getMergeVars($customer);
$api = $this->_api;
$isSubscribeOwnEmail = $this->_customerSession->isLoggedIn() && $this->_customerSession->getCustomerDataObject()->getEmail() == $subscriber->getSubscriberEmail();
if ($this->_helper->isDoubleOptInEnabled($storeId) && !$isSubscribeOwnEmail) {
$status = 'pending';
} else {
$status = 'subscribed';
}
if ($mergeVars) {
$data = array('list_id' => $this->_helper->getDefaultList(), 'email_address' => $customer->getEmail(), 'email_type' => 'html', 'status' => $status, 'merge_fields' => $mergeVars);
} else {
$data = array('list_id' => $this->_helper->getDefaultList(), 'email_address' => $customer->getEmail(), 'email_type' => 'html', 'status' => $status);
}
$return = $api->listCreateMember($this->_helper->getDefaultList(), json_encode($data));
if (isset($return->id)) {
$subscriber->setMagemonkeyId($return->id);
}
}
return [$customerId];
}