0

good morning guys, in my case I want to retrieve the customer name which is responsible for doing a reservation, but I can't get any attribute from reservation describe me the customer name

Can anyone please tell me how to get costumer name from the reservation ? , because any reservation was done by a customer.

this is how do i get the current reservation :

public function getReservation()
    {
        return $this->_coreRegistry->registry('current_reservation');
    }

this is another way , but why it throws for me an error while fetching why ?

Why there are a problems when fetching the data

the block :

 public function getReservationCollection()
    {
        $collection = $this->_reservationCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->setPageSize();
        return $collection;
    }

the phtml file :

<?php
$res = $block->getReservationCollection();
var_dump($res);
foreach ($res as $re) {
    print_r($re->getData());
    echo "<br>";
}?>

2 Answers 2

1

try this

Add this in block

    protected $_customers;

    public function __construct(
        .........................
        \Magento\Customer\Model\Customer $customers,
         ........................
    )
    {
        .............................
        $this->_customers = $customers;
        ...............................
    }
............................
............................

    public function getCustomer($customerId)
    {
        $customer = $this->_customers->load($customerId);
        $customer_name = $customer->getFirstname() . " " . $customer->getLastname();
        return $customer_name;
    }

Add this in phtml file

<?php

    $res = $block->getReservationCollection();
    //var_dump($res);
    foreach ($res as $re) {
        //print_r($re->getData());
        $cust_name =  $block->getCustomer($re->getCustomerId());
        echo "quote_id : ".$re->getQuoteId()." And  Customer Name: ".$cust_name;
        echo "<br>";
    }
?>

I Hope This Helps You.

1

You need to fetch customer firstname and lastname separately. By merging both values you can finally get full customer name. There is no separate attribute like customer name.

5
  • Yeah i understand , but how can i retrieve the data from customer entity inside a function in the block Commented Apr 14, 2020 at 14:58
  • I believe that reservation concept does not available in default magento. Hence, this should your custom or any third party extension code. In such case, you will have to look for the object methods which you can access in phtml or any other fie in which you need customer name. Fo that you can check with "get_class_methods($reservation)" method and you will able to know which data you can get from this object. If your required data is not available in this object then you will have to find some alternative way. I can help you further if you can post some more code of your module. Commented Apr 14, 2020 at 15:06
  • There is a way to retrieve the data from the database inside the block in a function and return it to phtml file? Commented Apr 14, 2020 at 15:35
  • Yes, you can definitely create such methods inside your block file like "getCustomerFirstname()" and "getCustomerLastname()". Then you can access those like "$block-getCustomerFirstname()" and can merge both values in your respective phtml file. Commented Apr 14, 2020 at 16:45
  • i have updated my question with some codes please check Commented Apr 14, 2020 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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