Blog

  1. How to update magento 2.3.0 to 2.3.1

    Upgrade Magento version 2.3.0 to 2.3.1 follow below steps:

    Magento 2 Version Upgrade Commands.

    1) php bin/magento maintenance:enable

    2) composer require magento/product-community-edition 2.3.1 --no-update

    3) composer update

    4) rm -rf var/* pub/static/* generated/*

    5) php bin/magento setup:upgrade

    6) php bin/magento setup:static-content:deploy -f

    7) php bin/magento setup:di:compile

    8) php bin/magento indexer:reindex

    9) php bin/magento cache:clean

    10) php bin/magento cache:flush

    After upgrade, check your Magento version with the following command:

    11) php bin/magento --version

    12) php bin/magento maintenance:disable

    Done.

    Read more »
  2. How to create order programmatically Magento 2

    How to create order programmatically Magento 2

    Now, you can create quote and order with the below content:

    $tempOrder=[
         'currency_id'  => 'USD',
         'email'        => 'helloworld@magenuts.com', //buyer email id
         'shipping_address' =>[
                'firstname'    => 'John', //address Details
                'lastname'     => 'Doe',
                        'street' => '123 Demo',
                        'city' => 'Magenuts',
                'country_id' => 'US',
                'region' => 'xxx',
                'postcode' => '10019',
                'telephone' => '0123456789',
                'fax' => '32423',
                'save_in_address_book' => 1
                     ],
       'items'=> [ //array of product which order you want to create
                  ['product_id'=>'1','qty'=>1],
                  ['product_id'=>'2','qty'=>2]
                ]
    ];
    

    Continuing to declare the order in the module helper file by the following function:

    <?php
    namespace YourNameSpace\ModuleName\Helper;
     
    class Data extends \Magento\Framework\App\Helper\AbstractHelper
    {
         /**
        * @param Magento\Framework\App\Helper\Context $context
        * @param Magento\Store\Model\StoreManagerInterface $storeManager
        * @param Magento\Catalog\Model\Product $product
        * @param Magento\Framework\Data\Form\FormKey $formKey $formkey,
        * @param Magento\Quote\Model\Quote $quote,
        * @param Magento\Customer\Model\CustomerFactory $customerFactory,
        * @param Magento\Sales\Model\Service\OrderService $orderService,
        */
        public function __construct(
            \Magento\Framework\App\Helper\Context $context,
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Catalog\Model\Product $product,
            \Magento\Framework\Data\Form\FormKey $formkey,
            \Magento\Quote\Model\QuoteFactory $quote,
            \Magento\Quote\Model\QuoteManagement $quoteManagement,
            \Magento\Customer\Model\CustomerFactory $customerFactory,
            \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
            \Magento\Sales\Model\Service\OrderService $orderService  
        ) {
            $this->_storeManager = $storeManager;
            $this->_product = $product;
            $this->_formkey = $formkey;
            $this->quote = $quote;
            $this->quoteManagement = $quoteManagement;
            $this->customerFactory = $customerFactory;
            $this->customerRepository = $customerRepository;
            $this->orderService = $orderService;
            parent::__construct($context);
        }
     
        /**
         * Create Order On Your Store
         * 
         * @param array $orderData
         * @return array
         * 
        */
        public function createMageOrder($orderData) {
            $store=$this->_storeManager->getStore();
            $websiteId = $this->_storeManager->getStore()->getWebsiteId();
            $customer=$this->customerFactory->create();
            $customer->setWebsiteId($websiteId);
            $customer->loadByEmail($orderData['email']);// load customet by email address
            263135
            if(!$customer->getEntityId()){
                //If not avilable then create this customer 
                $customer->setWebsiteId($websiteId)
                        ->setStore($store)
                        ->setFirstname($orderData['shipping_address']['firstname'])
                        ->setLastname($orderData['shipping_address']['lastname'])
                        ->setEmail($orderData['email']) 
                        ->setPassword($orderData['email']);
                $customer->save();
            }
            $quote=$this->quote->create(); //Create object of quote
            $quote->setStore($store); //set store for which you create quote
            // if you have allready buyer id then you can load customer directly 
            $customer= $this->customerRepository->getById($customer->getEntityId());
            $quote->setCurrency();
            $quote->assignCustomer($customer); //Assign quote to customer
     
            //add items in quote
            foreach($orderData['items'] as $item){
                $product=$this->_product->load($item['product_id']);
                $product->setPrice($item['price']);
                $quote->addProduct(
                    $product,
                    intval($item['qty'])
                );
            }
     
            //Set Address to quote
            $quote->getBillingAddress()->addData($orderData['shipping_address']);
            $quote->getShippingAddress()->addData($orderData['shipping_address']);
     
            // Collect Rates and Set Shipping & Payment Method
     
            $shippingAddress=$quote->getShippingAddress();
            $shippingAddress->setCollectShippingRates(true)
                            ->collectShippingRates()
                            ->setShippingMethod('freeshipping_freeshipping'); //shipping method
            $quote->setPaymentMethod('checkmo'); //payment method
            $quote->setInventoryProcessed(false); //not effetc inventory
            $quote->save(); //Now Save quote and your quote is ready
     
            // Set Sales Order Payment
            $quote->getPayment()->importData(['method' => 'checkmo']);
     
            // Collect Totals & Save Quote
            $quote->collectTotals()->save();
     
            // Create Order From Quote
            $order = $this->quoteManagement->submit($quote);
            
            $order->setEmailSent(0);
            $increment_id = $order->getRealOrderId();
            if($order->getEntityId()){
                $result['order_id']= $order->getRealOrderId();
            }else{
                $result=['error'=>1,'msg'=>'Your custom message'];
            }
            return $result;
        }
    }
     
    ?>
    

    When you have done the above instruction, congratulate you have created Magento 2 orders programmatically!

    Read more »
  3. Magento 2 admin panel blank page

    This is Magento bug. Wrong paths to Windows are generated. The fixed fix is

    Magento 2.3.0

    #/vendor/magento/framework/View/Element/Template/File/Validator.php:114

    the string

    $realPath = $this->fileDriver->getRealPath($path);

    to replace

    $realPath = str_replace('\\', '/', $this->fileDriver->getRealPath($path));

    Magento 2.2.7

    /vendor/magento/framework/View/Element/Template/File/Validator.php:113

    code

    protected function isPathInDirectories($path, $directories)
    {
        if (!is_array($directories)) {
            $directories = (array)$directories;
        }
        foreach ($directories as $directory) {
            if (0 === strpos($this->fileDriver->getRealPath($path), $directory)) {
                return true;
            }
        }
        return false;
    }

    to replace

    protected function isPathInDirectories($path, $directories)
        {
            $realPath = str_replace('\\', '/', $this->fileDriver->getRealPath($path));
            if (!is_array($directories)) {
                $directories = (array)$directories;
            }
            foreach ($directories as $directory) {
                if (0 === strpos($realPath, $directory)) {
                    return true;
                }
            }
            return false;
        }
    Read more »
  4. Magento 2 Get Review, Rating Collection

    As you might have already known, review and rating plays an important role in every online business, especially those which runs on Magento 2 platform as it related directly to the reputation of stores as well as the purchase decisions of customers.

    Therefore, in today post, I will provide you the simplest way to get review, rating collection in Magento 2.

     

     

    How to get review, rating collection

    To get product review, ratting collection, you need to create a ProductReviews.php file. Follow this path Mageplaza/HelloWorld/Model/ProductReviews.php and here are how you are going to do it.

    <?php
    
    namespace Mageplaza\HelloWorld\Model;
    use Magento\Framework\Model\AbstractModel;
    class ProductReviews extends AbstractModel{
    
        protected $_ratingFactory;
        protected $_productFactory;
        protected $_ratingFactory;
        protected $_reviewFactory;
    
        public function __construct(
                \Magento\Store\Model\StoreManagerInterface $storeManager,
                \Magento\Catalog\Model\ProductFactory $productFactory,
                \Magento\Review\Model\RatingFactory $ratingFactory,
                \Magento\Review\Model\ResourceModel\Review\CollectionFactory $reviewFactory,
            ) {
                $this->_storeManager = $storeManager;
                $this->_productFactory = $productFactory;
                $this->_ratingFactory = $ratingFactory;
                $this->_reviewFactory = $reviewFactory;
            }
    
        public function getReviewCollection($productId){
            $collection = $this->_reviewFactory->create()
            ->addStatusFilter(
                \Magento\Review\Model\Review::STATUS_APPROVED
            )->addEntityFilter(
                'product',
                $productId
            )->setDateOrder();
    
        }
    
        public function getRatingCollection(){
            $ratingCollection = $this->_ratingFactory->create()
            ->getResourceCollection()
            ->addEntityFilter(
                'product' 
            )->setPositionOrder()->setStoreFilter(
                $this->_storeManager->getStore()->getId()
            )->addRatingPerStoreName(
                $this->_storeManager->getStore()->getId()
            )->load();
    
            return $ratingCollection->getData();
        }
    
    }
    

    Conclusion

    Above is the instruction on how to get product review and rating collection in Magento 2. I hope this post is helpful for you when getting and managing the collection.

    Read more »
  5. Magento 2 How to Add Admin User via Command Line

    How to Add Admin User via Command Line

    Go to Magento admin root folder and show usage:

     

     

    php bin/magento admin:user:create --help
    

    Output:

    Usage:
      admin:user:create [options]
    
    Options:
          --admin-user=ADMIN-USER                    (Required) Admin user
          --admin-password=ADMIN-PASSWORD            (Required) Admin password
          --admin-email=ADMIN-EMAIL                  (Required) Admin email
          --admin-firstname=ADMIN-FIRSTNAME          (Required) Admin first name
          --admin-lastname=ADMIN-LASTNAME            (Required) Admin last name
          --magento-init-params=MAGENTO-INIT-PARAMS  Add to any command to customize Magento initialization parameters
                                                     For example: "MAGE_MODE=developer&MAGE_DIRS[base][path]=/var/www/example.com&MAGE_DIRS[cache][path]=/var/tmp/cache"
      -h, --help                                     Display this help message
      -q, --quiet                                    Do not output any message
      -V, --version                                  Display this application version
          --ansi                                     Force ANSI output
          --no-ansi                                  Disable ANSI output
      -n, --no-interaction                           Do not ask any interactive question
      -v|vv|vvv, --verbose                           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
    
    Help:
      Creates an administrator
    

    Let create a admin account with the following information:

    • User: mageplaza
    • Password: WeLoveMagento
    • Email: hi@mageplaza.com
    • First Name: Mageplaza
    • Last Name: Family

    So we the command line is:

    php bin/magento admin:user:create --admin-user=mageplaza --admin-password=WeLoveMagento --admin-email=hi@mageplaza.com --admin-firstname=Mageplaza --admin-lastname=Family
    

    It shows an error: Your password must include both numeric and alphabetic characters. The password should be more complex.

    • Password: WeLoveMagento123
    php bin/magento admin:user:create --admin-user=mageplaza --admin-password=WeLoveMagento123 --admin-email=hi@mageplaza.com --admin-firstname=Mageplaza --admin-lastname=Family
    

    And I created successfully a new admin account, it returned message:

    Created Magento administrator user named mageplaza
    

    Now to go to admin login page, and fill the above information

    magento 2 login

    and I’m in now, see this screenshot

    magento 2 admin pannel

    Read more »
  6. Magento 2 Custom Order Number

    What’s Magento 2 Custom Order Number

    Convenient sales management with Magento 2 Customer Order Number by Magenuts IT Solutions Private Limited. Most of companies using their own custom and complex order numbers which magento 2 does not support. For customer convenience Magenuts IT Solutions Private Limited provides solution to setup random numbers to their orders by defaults and label them automatically. No matter they are too long or to short having prefix or having suffix as we provide easiest solution to handle all the numbers of sales document. Magento 2 Custom Order Number is a perfect solution which allows customizing of order, invoice, shipment and credit memo numbers as per your needs.

    Key Features

    • Set up custom numbers for all order related documentation like order numbers, invoice numbers, shipment numbers and credit memo numbers.
    • Enables the customization of numbers using a flexible numbering system.
    Read more »
This is the privacy policy sample page.
It was created automatically and do not substitute the one you need to create and provide to your store visitors.
Please, replace this text with the correct privacy policy by visiting the Customers > GDPR > Privacy Policy section in the backend.