Magento 2: How can I get Customer during collectRates on custom shipping methodHow to add custom attribute to shipping method in magento2Custom Shipping Method - Please specify a shipping methodHow can i save custom field value in customer_entity table in Magento 2 using observerIs there any method to get cart details inside a plugin feature in Magento 2Migrating Shipping Extension for Magento 1.9 to Magento 2.2Magento 2 custom address attribute not loading in address book in checkout pageOverwrite Constructor to inject custom module helperSplit shipping with backorder - Magento 2Magento 2 Override Magento Checkout Model Cart updateItems functionMagento 2 - At Checkout page, the City Id is displaying in Shipping Address not the City Name

Can a cyclic Amine form an Amide?

Accidentally deleted the "/usr/share" folder

Was the ancestor of SCSI, the SASI protocol, nothing more than a draft?

Survey Confirmation - Emphasize the question or the answer?

Applying a function to a nested list

How does NAND gate work? (Very basic question)

A non-technological, repeating, phenomenon in the sky, holding its position in the sky for hours

Can I use 1000v rectifier diodes instead of 600v rectifier diodes?

CRT Oscilloscope - part of the plot is missing

Does hiding behind 5-ft-wide cover give full cover?

Did we get closer to another plane than we were supposed to, or was the pilot just protecting our delicate sensibilities?

Feels like I am getting dragged into office politics

How could a planet have most of its water in the atmosphere?

Was Unix ever a single-user OS?

How to assert on pagereference where the endpoint of pagereference is predefined

How to creep the reader out with what seems like a normal person?

Stark VS Thanos

Does the Darkness spell dispel the Color Spray and Flaming Sphere spells?

How do I tell my manager that his code review comment is wrong?

Can fracking help reduce CO2?

Has any spacecraft ever had the ability to directly communicate with civilian air traffic control?

Copy line and insert it in a new position with sed or awk

If Melisandre foresaw another character closing blue eyes, why did she follow Stannis?

What does air vanishing on contact sound like?



Magento 2: How can I get Customer during collectRates on custom shipping method


How to add custom attribute to shipping method in magento2Custom Shipping Method - Please specify a shipping methodHow can i save custom field value in customer_entity table in Magento 2 using observerIs there any method to get cart details inside a plugin feature in Magento 2Migrating Shipping Extension for Magento 1.9 to Magento 2.2Magento 2 custom address attribute not loading in address book in checkout pageOverwrite Constructor to inject custom module helperSplit shipping with backorder - Magento 2Magento 2 Override Magento Checkout Model Cart updateItems functionMagento 2 - At Checkout page, the City Id is displaying in Shipping Address not the City Name






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















How can I get the customer and cart during collectRates() function of my custom shipping method.



public function __construct(
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoQuoteModelQuoteAddressRateResultErrorFactory $rateErrorFactory,
PsrLogLoggerInterface $logger,
MagentoShippingModelRateResultFactory $rateResultFactory,
MagentoQuoteModelQuoteAddressRateResultMethodFactory $rateMethodFactory,
MagentoCheckoutModelCart $cart,
MagentoCustomerModelSession $customerSession,
array $data = []
)
$this->_cart = $cart;
$this->_rateResultFactory = $rateResultFactory;
$this->_rateMethodFactory = $rateMethodFactory;
$this->_customerSession = $customerSession;

parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);


public function collectRates(RateRequest $request)
{

if (!$this->getConfigFlag('active'))
return false;


if(!$this->_customerSession->isLoggedIn())
return false;


$customer = $this->_customerSession->getCustomer();
$qty = $this->_cart->getItemsQty();
...


Using customer session and checking if isLoggedIn() only works for frontend but returns false when placing order in the admin.



How can I get customer properly and calculate my price per item for both frontend and admin order placement?










share|improve this question




























    0















    How can I get the customer and cart during collectRates() function of my custom shipping method.



    public function __construct(
    MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
    MagentoQuoteModelQuoteAddressRateResultErrorFactory $rateErrorFactory,
    PsrLogLoggerInterface $logger,
    MagentoShippingModelRateResultFactory $rateResultFactory,
    MagentoQuoteModelQuoteAddressRateResultMethodFactory $rateMethodFactory,
    MagentoCheckoutModelCart $cart,
    MagentoCustomerModelSession $customerSession,
    array $data = []
    )
    $this->_cart = $cart;
    $this->_rateResultFactory = $rateResultFactory;
    $this->_rateMethodFactory = $rateMethodFactory;
    $this->_customerSession = $customerSession;

    parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);


    public function collectRates(RateRequest $request)
    {

    if (!$this->getConfigFlag('active'))
    return false;


    if(!$this->_customerSession->isLoggedIn())
    return false;


    $customer = $this->_customerSession->getCustomer();
    $qty = $this->_cart->getItemsQty();
    ...


    Using customer session and checking if isLoggedIn() only works for frontend but returns false when placing order in the admin.



    How can I get customer properly and calculate my price per item for both frontend and admin order placement?










    share|improve this question
























      0












      0








      0








      How can I get the customer and cart during collectRates() function of my custom shipping method.



      public function __construct(
      MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
      MagentoQuoteModelQuoteAddressRateResultErrorFactory $rateErrorFactory,
      PsrLogLoggerInterface $logger,
      MagentoShippingModelRateResultFactory $rateResultFactory,
      MagentoQuoteModelQuoteAddressRateResultMethodFactory $rateMethodFactory,
      MagentoCheckoutModelCart $cart,
      MagentoCustomerModelSession $customerSession,
      array $data = []
      )
      $this->_cart = $cart;
      $this->_rateResultFactory = $rateResultFactory;
      $this->_rateMethodFactory = $rateMethodFactory;
      $this->_customerSession = $customerSession;

      parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);


      public function collectRates(RateRequest $request)
      {

      if (!$this->getConfigFlag('active'))
      return false;


      if(!$this->_customerSession->isLoggedIn())
      return false;


      $customer = $this->_customerSession->getCustomer();
      $qty = $this->_cart->getItemsQty();
      ...


      Using customer session and checking if isLoggedIn() only works for frontend but returns false when placing order in the admin.



      How can I get customer properly and calculate my price per item for both frontend and admin order placement?










      share|improve this question














      How can I get the customer and cart during collectRates() function of my custom shipping method.



      public function __construct(
      MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
      MagentoQuoteModelQuoteAddressRateResultErrorFactory $rateErrorFactory,
      PsrLogLoggerInterface $logger,
      MagentoShippingModelRateResultFactory $rateResultFactory,
      MagentoQuoteModelQuoteAddressRateResultMethodFactory $rateMethodFactory,
      MagentoCheckoutModelCart $cart,
      MagentoCustomerModelSession $customerSession,
      array $data = []
      )
      $this->_cart = $cart;
      $this->_rateResultFactory = $rateResultFactory;
      $this->_rateMethodFactory = $rateMethodFactory;
      $this->_customerSession = $customerSession;

      parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);


      public function collectRates(RateRequest $request)
      {

      if (!$this->getConfigFlag('active'))
      return false;


      if(!$this->_customerSession->isLoggedIn())
      return false;


      $customer = $this->_customerSession->getCustomer();
      $qty = $this->_cart->getItemsQty();
      ...


      Using customer session and checking if isLoggedIn() only works for frontend but returns false when placing order in the admin.



      How can I get customer properly and calculate my price per item for both frontend and admin order placement?







      magento2 shipping-method






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 22 at 19:53









      Cmaclean066Cmaclean066

      153




      153






















          1 Answer
          1






          active

          oldest

          votes


















          1














          If you can detect when your code runs in Admin, you can use
          MagentoBackendModelSessionQuote
          as your [admin_session] and use [admin_session]->getCustomerId(), along with the customer repository (inject interfaces in constructor and let DI pass the correct objects) to fetch the customer object.



          I would suggest to check the contents of the MagentoBackendModelSessionQuote object, as it might contain a pre-loaded customer object already, in which case you can avoid loading it.



          You could use MagentoFrameworkAppState::getAreaCode() to check if within admin






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306947%2fmagento-2-how-can-i-get-customer-during-collectrates-on-custom-shipping-method%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            If you can detect when your code runs in Admin, you can use
            MagentoBackendModelSessionQuote
            as your [admin_session] and use [admin_session]->getCustomerId(), along with the customer repository (inject interfaces in constructor and let DI pass the correct objects) to fetch the customer object.



            I would suggest to check the contents of the MagentoBackendModelSessionQuote object, as it might contain a pre-loaded customer object already, in which case you can avoid loading it.



            You could use MagentoFrameworkAppState::getAreaCode() to check if within admin






            share|improve this answer



























              1














              If you can detect when your code runs in Admin, you can use
              MagentoBackendModelSessionQuote
              as your [admin_session] and use [admin_session]->getCustomerId(), along with the customer repository (inject interfaces in constructor and let DI pass the correct objects) to fetch the customer object.



              I would suggest to check the contents of the MagentoBackendModelSessionQuote object, as it might contain a pre-loaded customer object already, in which case you can avoid loading it.



              You could use MagentoFrameworkAppState::getAreaCode() to check if within admin






              share|improve this answer

























                1












                1








                1







                If you can detect when your code runs in Admin, you can use
                MagentoBackendModelSessionQuote
                as your [admin_session] and use [admin_session]->getCustomerId(), along with the customer repository (inject interfaces in constructor and let DI pass the correct objects) to fetch the customer object.



                I would suggest to check the contents of the MagentoBackendModelSessionQuote object, as it might contain a pre-loaded customer object already, in which case you can avoid loading it.



                You could use MagentoFrameworkAppState::getAreaCode() to check if within admin






                share|improve this answer













                If you can detect when your code runs in Admin, you can use
                MagentoBackendModelSessionQuote
                as your [admin_session] and use [admin_session]->getCustomerId(), along with the customer repository (inject interfaces in constructor and let DI pass the correct objects) to fetch the customer object.



                I would suggest to check the contents of the MagentoBackendModelSessionQuote object, as it might contain a pre-loaded customer object already, in which case you can avoid loading it.



                You could use MagentoFrameworkAppState::getAreaCode() to check if within admin







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 25 at 20:55









                Dmitri SologoubenkoDmitri Sologoubenko

                2,67811625




                2,67811625





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306947%2fmagento-2-how-can-i-get-customer-during-collectrates-on-custom-shipping-method%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript