1

I have Link to open a popup modal. But before open the popup it should check whether the customer is logged in or not? If logged in It will show my popup modal else it should show one alert template file I have mentioned below code in my template

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$context = $objectManager->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
if ($isLoggedIn):

    $class = "action add add-your-review";

else:
    $class = "open-login-poup";

endif;

How do I get customer id or name using data attribute in my template

js file

 function ($) {
            "use strict";
            // Review form 
            var abc = {
                type: 'popup',
                responsive: true,
                innerScroll: true,
                buttons: false,
                title: "Header contant",
                modalClass: "popup-center xxx-product-review",
                clickableOverlay: true,
                heightStyle: "content"
            };
            var xxx = $('#review-form-content-wrapper').modal(abc);
            $(".add-your-review").click(function () {
                xxx.modal("openModal");
            });


            return function (config) {
                $('#review-form').on("click", "#review-submit", function () {
                    var form_data = $("#review-form").serialize();
                    var reviewurl = config.url;
                    $.ajax({
                        url: reviewurl,
                        type: 'POST',
                        // Pass the submitted form data to Controller
                        data: form_data,
                        success: function (response) {

                            var returnedData = JSON.parse(response);
                            if (returnedData.status === 'success')
                                $("#success").html(returnedData.messages);
                            else
                                $("#error").html(returnedData.messages);
                        },
                        error: function (response) {
                            var returnedData = JSON.parse(response);
                            $("#error").html(returnedData.messages);
                        }

                    });
                    return false;

                });
            };

        });
  • please add you code which you have tried for popup model? – Rakesh Jesadiya Dec 27 '17 at 10:58
  • @Rakesh I have updated the post – Jaisa Dec 27 '17 at 11:08
  • have you used any phtml file? – Abdul Dec 27 '17 at 12:38
  • I have updated my post @ Abdul – Jaisa Dec 27 '17 at 12:43
  • If you need I will update my full template file – Jaisa Dec 27 '17 at 12:44
1

Try this:

First add below code in your .phml file

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
$class = "";
if ($customerSession->isLoggedIn()):
    $class = "action add add-your-review";
else:
    $class = "open-login-poup";

endif;
?>
<script>
    window.isCustomerLoggedClass = "<?php echo $class ?>";
</script>

After get this class name in your js file

var customerLoggedClass = window.isCustomerLoggedClass;
console.log(customerLoggedClass);

Same way your got customer id , name & more...

for eg.

.phtml file

<script>
        window.customerId = "<?php echo $customerSession->getCustomer()->getId() ?>";
</script>

In js file

var customerId = window.customerId;
    console.log(customerId);

After run below command,

php bin/magento setup:static-content:deploy
0

YOu need to define if(customer.isLoggedIn()){} for check whether customer is guest or login.

 define(
    [
        'jquery',        
        'Magento_Customer/js/model/customer'
    ],
 function ($,customer) {
            "use strict";
            // Review form 
            var abc = {
                type: 'popup',
                responsive: true,
                innerScroll: true,
                buttons: false,
                title: "Header contant",
                modalClass: "popup-center xxx-product-review",
                clickableOverlay: true,
                heightStyle: "content"
            };
            var xxx = $('#review-form-content-wrapper').modal(abc);
            $(".add-your-review").click(function () {
                if(customer.isLoggedIn())
                    xxx.modal("openModal");
                else
                    alert('guest customer not allowed');
            });


            return function (config) {
                $('#review-form').on("click", "#review-submit", function () {
                    var form_data = $("#review-form").serialize();
                    var reviewurl = config.url;
                    $.ajax({
                        url: reviewurl,
                        type: 'POST',
                        // Pass the submitted form data to Controller
                        data: form_data,
                        success: function (response) {

                            var returnedData = JSON.parse(response);
                            if (returnedData.status === 'success')
                                $("#success").html(returnedData.messages);
                            else
                                $("#error").html(returnedData.messages);
                        },
                        error: function (response) {
                            var returnedData = JSON.parse(response);
                            $("#error").html(returnedData.messages);
                        }

                    });
                    return false;

                });
            };

        });

run command,

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
  • Thank you, it shows below error Uncaught TypeError: Cannot read property 'isLoggedIn' of undefined – Jaisa Dec 27 '17 at 11:29
  • please check using clear browser cache and try – Rakesh Jesadiya Dec 27 '17 at 11:33
  • I have cleared the browser cache but while clicking link it shows the above error what i mentioned above – Jaisa Dec 27 '17 at 11:44
  • Still error diaplayed – Jaisa Dec 27 '17 at 11:46
  • I think its the way to get customer logged in or not in magento 2 using ko/js, I am not aware other way. – Rakesh Jesadiya Dec 27 '17 at 11:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.