1

I have a helper class with parameter as shown below. I need this function to work on a specific controller. But returns this error. My helper Code below

<?php
namespace Rk\Wallet\Helper;

class DataPlaceOrder 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
        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['483']);
            $product->setPrice($item['price']);
            $quote->addProduct(
                $product,
                intval($item['1'])
            );
        }

        //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('payu'); //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' => 'payu']);

        // 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;
    }
}

My controller

<?php

namespace Rk\Wallet\Controller\Index;

class Wallet extends \Magento\Framework\App\Action\Action
{
public function execute()
 { 
   $this->helperData->createMageOrder();

 }

But return the following error

Warning: Missing argument 1 for Rk\Wallet\Helper\DataPlaceOrder::createMageOrder(), called in /home/rkhomeappliances/public_html/test/app/code/Rk/Wallet/Controller/Index/Wallet.php on line 63 and defined in /home/rkhomeappliances/public_html/test/app/code/Rk/Wallet/Helper/DataPlaceOrder.php on line 48

4
  • Your helper function is called here but issue is you are not passing any argument while calling helper function and you have defined $data in helper function so its make issues – Manthan Dave Jan 31 '18 at 6:50
  • $orderdata? So how to call this arguement in controller? – Ramesh KR Jan 31 '18 at 6:51
  • in your controller change like this - $this->helperData->createMageOrder($orderData); – Manthan Dave Jan 31 '18 at 6:51
  • no it return undefined variable – Ramesh KR Jan 31 '18 at 7:12
1

Looks like your call to helper is alright, the issue with you code is you helper method createMageOrder needs one argument $orderData, and in controller where you called the helper method $this->helperData->createMageOrder(); you missed to provide required parameter.

Provide the required parameter in hepler method call and you are good to go

$orderData = [...]; // Prepare $orderData and pass in helper method call 
$this->helperData->createMageOrder($orderData);
2
  • what to give inside $orderdata[???] pls help me – Ramesh KR Jan 31 '18 at 7:23
  • create $orderdata array with all the data you need. – Arun Karnawat Jan 31 '18 at 9:24
0
    /**
     * @var Vendor\Module\Helper\Data
     */
    protected $_Customhelper;

public function __construct(\Vendor\Module\Helper\Data $Customhelper)
{
     $this->_Customhelper = $Customhelper;
}

user by this syntax::

$this->_Customhelper->getStatusLabel()
2
  • My helper has an argument public function createMageOrder($orderData) {} it returns argument missing error – Ramesh KR Jan 31 '18 at 7:14
  • You can call function with argument like $this->_Customhelper->createMageOrder($orderData); – Birjitsinh Zala Jan 31 '18 at 8:37
0

try this

<?php
namespace Rk\Wallet\Controller\Index;

use \Magento\Framework\App\Action\Action;
use \Magento\Framework\App\Action\Context;
use \Rk\Wallet\Helper\Data;

class Help extends Action
{
    protected $helper;

    public function __construct(
        Context $context,
        Data $helper
    ) {
        $this->helper = $helper;
        parent::__construct($context);
    }
    public function execute()
    {
       $this->helper->createMageOrder($orderData);
    }
}
2
  • My helper has an argument public function createMageOrder($orderData) {} it returns argument missing error – Ramesh KR Jan 31 '18 at 7:13
  • now check updated code – Ravindrasinh Zala Jan 31 '18 at 7:54

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.