2

I want to set custom shipping price irrespective of customer address.

Below is the code.

I tried to change it by adding - $cart->setShippingAmount('800');

but its not working. Is there any way to add custom shipping amount with custom_shipping_method

    <?php
    namespace Namespace\Crm\Controller\Crmcheckout;

    class Testorder extends \Magento\Framework\App\Action\Action
    {
        protected $_cacheTypeList;
        protected $_cacheState;
        protected $_cacheFrontendPool;
        protected $resultPageFactory;
        protected $_customer;
        protected $_customerSession;
        protected $_scopeConfig;
        protected $_storeManager;
        protected $_product;
        protected $productModel;
        protected $quote;
        protected $customerFactory;
        protected $customer;
        protected $customerRepository;
        protected $cartRepositoryInterface;
        protected $cartManagementInterface;
        protected $_resources;

        public function __construct(
           \Magento\Framework\App\Action\Context $context,
            \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
            \Magento\Framework\App\Cache\StateInterface $cacheState,
            \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory,
            \Magento\Customer\Model\Customer $customer,
            \Magento\Customer\Model\Session $customerSession,
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Catalog\Model\ProductFactory $productFactory,
            \Magento\Customer\Model\CustomerFactory $customerFactory,
            \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
            \Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface,
            \Magento\Quote\Api\CartManagementInterface $cartManagementInterface,
            \Magento\Quote\Model\Quote\Address\Rate $shippingRate,
            \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
            \Magento\Catalog\Api\ProductRepositoryInterface $product,
            \Magento\Catalog\Model\Product $productModel,
            \Magento\Quote\Model\QuoteFactory $quote,
            \Magento\Customer\Model\Customer $customer
        ) {
            parent::__construct($context);
            $this->_cacheTypeList = $cacheTypeList;
            $this->_cacheState = $cacheState;
            $this->_cacheFrontendPool = $cacheFrontendPool;
            $this->resultPageFactory = $resultPageFactory;
            $this->_customer = $customer;
            $this->_customerSession = $customerSession;
            $this->_storeManager            = $storeManager;
            $this->_productFactory          = $productFactory;
            $this->customerFactory          = $customerFactory;
            $this->customerRepository       = $customerRepository;
            $this->cartRepositoryInterface  = $cartRepositoryInterface;
            $this->cartManagementInterface  = $cartManagementInterface;
            $this->shippingRate             = $shippingRate;
            $this->_scopeConfig             = $scopeConfig;
            $this->_product                 = $product;
            $this->productModel             = $productModel;
            $this->quote                    = $quote;
            $this->customer                 = $customer;
        }


        public function execute()
        {
            $store = $this->_storeManager->getStore();
            $websiteId = $this->_storeManager->getStore()->getWebsiteId();
            $store_id = 1;

            $orderData=[
                        'currency_id'  => 'USD',
                        'email'        => 'abcdfdf@test.com',
                        'shipping_address' =>[
                                'firstname'     => 'abcdfdf',
                                'lastname'      => 'ytttetre',
                                'street'        => 'road no 3, dadafdd ',
                                'city'          => 'new york',
                                'country_id'    => 'US',
                                'region'        => '12',
                                'postcode'      => '900001',
                                'telephone'     => '454354353534',
                                'fax'           => '',
                                'save_in_address_book' => 1
                            ],
                        'billing_address' =>[
                                'firstname'     => 'abcdfdf',
                                'lastname'      => 'ytttetre',
                                'street'        => 'road no 3, dadafdd ',
                                'city'          => 'new york',
                                'country_id'    => 'US',
                                'region'        => '12',
                                'postcode'      => '900001',
                                'telephone'     => '454354353534',
                                'fax'           => '',
                                'save_in_address_book' => 1
                            ],
                        ];

            $orderData['items'] = array('product_id'=>'9471','qty'=>1,'price'=>'280.00','sku'=>'BCT3018-BM');

            //init the customer
            $customer=$this->customerFactory->create();
            $customer->setWebsiteId($websiteId);
            $customer->loadByEmail($orderData['email']);
            //check the customer
            if(!$customer->getEntityId()){
                $customer->setWebsiteId($websiteId)
                    ->setStore($store)
                    ->setFirstname($orderData['shipping_address']['firstname'])
                    ->setLastname($orderData['shipping_address']['lastname'])
                    ->setEmail($orderData['email'])
                    ->setPassword($orderData['email']);
                $customer->save();
            }

            $customer->loadByEmail($orderData['email']);

            $this->_customerSession->setCustomerAsLoggedIn($customer);

            $cart_id = $this->cartManagementInterface->createEmptyCart();
            $cart = $this->cartRepositoryInterface->get($cart_id);
            $cart->setStore($store);

            $customer= $this->customerRepository->getById($customer->getEntityId());
            $cart->setCurrency();
            $cart->assignCustomer($customer); //Assign quote to customer


            $cart_flag = 0;
            if($orderData['items']){
                if(isset($orderData['items']['product_id'])){
                    $item_product_id    = $orderData['items']['product_id'];
                    $item_price         = $orderData['items']['price'];
                    $item_qty           = $orderData['items']['qty'];

                    $product = $this->_productFactory->create()->load($item_product_id);
                    $product->setPrice($item_price);
                    $cart->addProduct(
                        $product,
                        intval($item_qty)
                    );
                    $cart_flag = 1;

                }                                     
            }   

            if($cart_flag == 1){
                $cart->getBillingAddress()->addData($orderData['shipping_address']);
                $cart->getShippingAddress()->addData($orderData['shipping_address']);

                // Collect Rates and Set Shipping & Payment Method
                $this->shippingRate
                    ->setCode('freeshipping_freeshipping')
                    ->getPrice(1);
                $shippingAddress = $cart->getShippingAddress();
                //@todo set in order data
                $shippingAddress->setCollectShippingRates(true)
                    ->collectShippingRates()
                    ->setShippingMethod('freeshipping_freeshipping'); 

                $cart->getShippingAddress()->addShippingRate($this->shippingRate);
                //$cart->setShippingAmount('800');
                //$cart->setPaymentMethod('checkmo'); //payment method
                //@todo insert a variable to affect the invetory
                $cart->setInventoryProcessed(false);
                // Set sales order payment
                //$cart->getPayment()->importData(['method' => 'checkmo']);
                // Collect total and saeve
                $cart->collectTotals();
                // Submit the quote and create the order
                $cart->save();  

                $this->_redirect('checkout/cart/');  

                //echo $cart->getId(); exit;
            }

        }

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.