3

The error that I'm getting is :

Fatal error: Cannot declare class ExpectedDelivery, because the name is already in use in /var/www/html/app/code/Fabelio/Checkout/Block/ExpectedDelivery.php on line 89

I called the block in my html like this :

<?php
    $this->getLayout()->createBlock('Fabelio\Checkout\Block\ExpectedDelivery'); 
?>

and here is the Block class :

class ExpectedDelivery extends \Magento\Framework\View\Element\Template
{

    protected $todoItemFactory;
    protected $sessionCustomer;
    protected $sessionCheckout;

    /**
     * ExpectedDelivery constructor.
     * @param Template\Context $context
     * @param Session $session
     * @param array $data
     */
    public function __construct(
        Template\Context $context,
        Session $sessionCustomer,
        SessionCheckout $sessionCheckout,
        array $data = [])
    {
        $this->sessionCustomer = $sessionCustomer;
        $this->sessionCheckout = $sessionCheckout;
        parent::__construct($context, $data);
    }

    function _prepareLayout(){
    }

    public function getExpectedDelivery()
    {
        return 'goodbye magento 2'; 


    }
}

Any idea how to call the block the right way in magento 2.1.8 ?

Solve : The problem here is because of I forget to add the namespce Fabelio\Checkout\Block; in the Block class. Thanks everyone.

9
  • Please share code of app/code/Fabelio/Checkout/Block/ExpectedDelivery.php
    – Amit Bera
    Sep 27, 2017 at 8:44
  • have you checked this answer for generated error magento.stackexchange.com/a/136911/20064
    – Piyush
    Sep 27, 2017 at 8:45
  • 2
    if you calling $this->getLayout()->createBlock('Fabelio\Checkout\Block\ExpectedDelivery'); in phtml then it is wrong . And this->getLayout() should be ` $block->getLayout()`
    – Amit Bera
    Sep 27, 2017 at 8:46
  • @AmitBera yup I have modify the $this to $block but still doesn't work Sep 27, 2017 at 9:21
  • 1
    for your class ExpectedDelivery where defined namespace of full path? Sep 27, 2017 at 9:38

1 Answer 1

4

Call in your phtml file with below code,

<?php
   $expectedBlock =  $this->getLayout()->createBlock('Fabelio\Checkout\Block\ExpectedDelivery'); 
   echo $expectedBlock->getExpectedDelivery();
?>
1
  • Thank you this was cause by silly mistake I forgot to add the namespace :D. Sep 27, 2017 at 9:54

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.