:::: MENU ::::

magento 2 get current quote

Today we talk about how Magento 2 get current quote. You can get quote data from the checkout session. You can get data to manipulate the data in the cart. So let start with our example.

namespace QaisarSatti/HelloWorld/Block;

use Magento\Backend\App\Action;


class GetQuote extends Action
{

    protected $checkoutSession;

    public function __construct(
              \Magento\Checkout\Model\Session $checkoutSession    
           )
    {
        $this->checkoutSession = $checkoutSession;
    }

    public function getQuotes()
    {
            return $this->checkoutSession->getQuote();

       
    }
}

Now we call our function and get a quote data.

$allItems = $this->getQuotes()->getAllVisibleItems();
foreach ($allItems as $item) {
    echo $item->getProductId();
}
Author: Qaisar Satti
Category: Magento 2
Last Modified: February 17, 2020


magento 2 get all customers

Today we talk about how Magento 2 get all customers. Sometimes you need to list down all the customers for many reasons. For exporting the customer the data or show all customer listing. So let start with our example to list down all the customer.

namespace QaisarSatti/HelloWorld/Block;

use Magento\Backend\App\Action;


class Delete extends Action
{

    protected $customerCollection;

    public function __construct(
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerCollection
    )
    {
        $this->customerCollection = $customerCollection;
    }

    public function getAllCustomers()
    {
            return $this->customerCollection->create();

       
    }
}

Now we call the function and list down all the customer data.

$allCustomers = $this->getAllCustomers();
foreach ($allCustomers as $customer) {
    echo $customer->getEmail();
    echo $customer->getFirstname();
    echo $customer->getLastname();
}
Author: Qaisar Satti
Category: Magento 2
Last Modified: February 17, 2020


Magento 2 get customer address

Today we discuss how in Magento 2 get customer address. Which include the default shipping address and default billing address also the all customer address list too. There are two types of addresses related to customer shipping and billing. you can add the unlimited address as a customer. let start with our example.

Customer Default Billing Address

Get the customer defaut billing address.

namespace QaisarSatti/HelloWorld/Block;

use Magento\Backend\App\Action;

class Delete extends Action
{

    protected $customerFactory;
    protected $addressFactory;

    public function __construct(
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\AddressFactory $addressFactory
    )
    {
        $this->customerFactory = $customerFactory;
        $this->addressFactory = $addressFactory;
    }

    public function getCustomerDefaultBillingAddress()
    {
        $customerObject = $this->customerFactory->create()->load(10);  
        $billingAddressId = $customerObject->getDefaultBilling();
        return $billingAddress = $this->addressFactory->create()->load($billingAddressId);
       
    }
}

Customer Default Shipping Address

Get the customer defaut billing address.

namespace QaisarSatti/HelloWorld/Block;

use Magento\Backend\App\Action;

class Delete extends Action
{

    protected $customerFactory;
    protected $addressFactory;

    public function __construct(
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\AddressFactory $addressFactory
    )
    {
        $this->customerFactory = $customerFactory;
        $this->addressFactory = $addressFactory;
    }

    public function getCustomerDefaultBillingAddress()
    {
        $customerObject = $this->customerFactory->create()->load(10);  
        $shippingAddressId = $customerObject->getDefaultShipping();
        return $shippingAddress = $this->addressFactory->create()->load($shippingAddressId);
       
    }
}

Customer All Address

Get the customer all address.

namespace QaisarSatti/HelloWorld/Block;

use Magento\Backend\App\Action;

class Delete extends Action
{

    protected $customerFactory;
    protected $addressFactory;

    public function __construct(
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\AddressFactory $addressFactory
    )
    {
        $this->customerFactory = $customerFactory;
        $this->addressFactory = $addressFactory;
    }

    public function getCustomerDefaultBillingAddress()
    {
        $customerObject = $this->customerFactory->create()->load(10);  
        return $customerObject->getAddresses();
       
    }
}
Author: Qaisar Satti
Category: Magento 2
Last Modified: February 17, 2020


Magento 2 current order

In this tutorial, we talk about how to get Magento 2 current order. Which includes order information current order id, Increment Id and other order-related data too like shipping information, billing information, and customer information. Now we start with our example.

You have to inject the checkout model session to get the latest order information.

protected $_checkoutSession;


public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,

) {

        $this->_checkoutSession = $checkoutSession;


    }

Now with $_checkoutSession obejct you can get order following information.

 $order = $this->_checkoutSession->getLastRealOrder();
 $orderId=$order->getEntityId();  // order id
 $order->getIncrementId();  // order increment id
 $order->getShippingMethod(); // shippingm ehtod
 $order->getPayment();   // payment method
 $order->getCustomerEmail(); // customer email
Author: Qaisar Satti
Category: Magento 2
Last Modified: February 4, 2020


Magento 2 order PayPal information

Today we talk about how in Magento 2 order PayPal information. In this tutorial, we focus on how to get order PayPal information which includes the PayPal express token and PayPal correlation id. So let start with our example.

First, we get payment additional information.

[cc lang="php" tab_size="2" lines="40"]
$paymentAdditionalInfo = $order->getPayment()->getAdditionalInformation();

With the heplp of addiotnal information obejct we get paypal expreess token.

// get PayPal express checkout token
$token = $paymentAdditionalInfo['paypal_express_checkout_token'];

With the heplp of addiotnal information obejct we get paypal correlation id.

// get PayPal correlation ID
$correlationId = $paymentAdditionalInfo['paypal_correlation_id'];
Author: Qaisar Satti
Category: Magento 2
Last Modified: February 4, 2020


Magento 2 delete file or image

Today we discuss how to delete a file or image in Magento 2. In this tutorial, we focus on Magento 2 ways to delete the file or image. As a PHP developer, you already about the function unlink(filepathhere) will delete the file for you. So let start with our example.

namespace QaisarSatti/HelloWorld/Block;

use Magento\Backend\App\Action;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Filesystem;
use Magento\Framework\App\Filesystem\DirectoryList;

class Delete extends Action
{

    protected $fileSystem;
    protected $file;

    public function __construct(
        Filesystem $fileSystem,
        File $file
    )
    {
        $this->fileSystem = $fileSystem;
        $this->file = $file;
    }

    public function delete()
    {
        $fileName= "helloworld.jpg";
        $mediaRootDir = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();
        if ($this->file->isExists($mediaRootDir . $fileName)) {
            $this->file->deleteFile($mediaRootDir . $fileName);
        }
       
    }
}

Delete FIle with ObjectManager.

$fileName= "helloworld.jpg";
$file = $this->_objectManager->get('Magento\Framework\Filesystem\Driver\File');
$mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
$mediaRootDir = $mediaDirectory->getAbsolutePath();

if ($file->isExists($mediaRootDir . $fileName))  {

    $file->deleteFile($mediaRootDir . $fileName);
}
Author: Qaisar Satti
Category: Magento 2
Last Modified: February 17, 2020


PrestaShop get current user information

Today we talk about PrestaShop get current user information. In this tutorial, we focus on how to get customer information. User email, User name, User id, and other users related information.
You can get the context variable by following.

in the Controller file.

 $contextObject = $this->context;

Other files

 $contextObject = Context::getContext();

You can get the following user information using the $contextObject.

$contextObject->customer->id;
$contextObject->customer->firstname
$contextObject->customer->lastname
$contextObject->customer->email
Author: Qaisar Satti
Category: Prestashop
Last Modified: November 28, 2019


Pages:1234567...23