0

I am working on Quick Order Custom Module, for this I have shown the all products in to one page. So that the customer will add the multiple product to cart at a time. Here Products are adding to cart and showing messages in my form, but minicart is not showing. For this I have written below code.

quickorder.phtml

<?php

$list = $block->getProducts();

$cart_items = $block->getCartItems();

?>

<div style="width: 100%;">
    <h3>Quick Order Instructions</h3>
    <div>
        <p>The <b>Quick Order</b> page is designed to simplify the ordering process.
            All products are listed in the table below. A search feature has been provided to help find the desired product(s).
            In addition, the table headers are sortable on devices with a screen width greater than 550px.
        </p>
        <div >
            <h4 style="margin-bottom:5px">How to use the Quick Order Page</h4>
            <ol style="margin-left: 35px;">
                <li>Find the desired product(s), using the search feature if needed. </li>
                <li>Enter the amount into <b>QTY</b> box and press enter or select the <b>ADD TO CART</b> button.</li>
                <li>View the cart to place your order -or- continue shopping on the page.</li>
            </ol>
            <p>*<b>Best Price</b> is calculated on largest quantity discount.</p>
        </div>


    </div>

</div>
<br/>
<br/>
<?php

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->create('\Magento\Store\Model\StoreManagerInterface');
$baseURL_l = $social_image_url = $storeManager->getStore()->getBaseUrl();

$fav = "";
$sk = "";
$name = "";
$price = "";
$qty = "";
$cart = "";

if(count($list) > 0 ){
    /*$table = "";*/
    foreach($list as $items){

        $product_obj = $objectManager->create('Magento\Catalog\Model\Product')->load($items->getId());

        $sku = $product_obj->getSKU();
        $url = $baseURL_l . $sku;

        $fav .= "<p>";
        $fav .= "<center>&nbsp</center>";
        $fav .= "</p>";

        $sk .= "<p>";
        $sk .= "<a href='$url' target='_blank'>";
        $sk .= $sku;
        $sk .= "</a>";
        $sk .= "</p>";

        $name .= "<p>";
        $name .= $product_obj->getName();
        $name .= "</p>";

        $price_for = number_format($product_obj->getPrice(), 2, '.', '');

        $price .= "<p>";
        $price .= $price_for;
        $price .= "</p>";

        $qty .= "<p>";
        //$qty .= "<input type='text' style='text-align:center;padding-right:1px;padding-left:1px;width:40px;height: 20px;' maxLength='3' id='Quantity_$sku' name='Quantity' value=''>";
        $qty .= "<input type='number' maxLength='3' name='qty_$sku' id='qty' title='Qty' value='' class='input-text qty' style='text-align:center;padding-right:1px;padding-left:1px;width:40px;height: 20px;'/>";
        $qty .= "</p>";

        $cart .= "<p>";
        $cart .= "<center>&nbsp</center>";
        $cart .= "</p>";


    }
}

if(count($list) >0 ){
    $div = "";
    $div .= "<div class='row'>";
    $div .= "<div class='custom_fav' style='background-color:lavenderblush;'>";
    $div .= "<h3>Favorites</h3>";
    $div .= $fav;
    $div .= "</div>";
    $div .= "<div class='custom_sku' style='background-color:lavenderblush;'>";
    $div .= "<h3>SKU</h3>";
    $div .= $sk;
    $div .= "</div>";
    $div .= "<div class='custom_name' style='background-color:lavenderblush;'>";
    $div .= "<h3>Product Name</h3>";
    $div .= $name;
    $div .= "</div>";
    $div .= "<div class='custom_price' style='background-color:lavenderblush;'>";
    $div .= "<h3>Best Price</h3>";
    $div .= $price;
    $div .= "</div>";
    $div .= "<div class='custom_text' style='background-color:lavenderblush;'>";
    $div .= "<h3>Qty</h3>";
    $div .= $qty;
    $div .= "</div>";
    $div .= "<div class='custom_cart_qty' style='background-color:lavenderblush;'>";
    $div .= "<h3>In Cart</h3>";
    $div .= $cart;
    $div .= "</div>";

    $div .= "</div>";

   ?>

<form data-role="tocart-form" method="post" action="<?php /* @escapeNotVerified */ echo $baseURL_l . 'quickorder/index/add'; ?>">
    <div style="text-align: center;">
        <input type="submit" onclick="<?php /* @escapeNotVerified */ echo $baseURL_l . 'quickorder/index/add'; ?>" name="insert" value="Add to Cart" class="btn" />
        <button type="submit"
                title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                class="btn">
        </button>
    </div>
    <div style="text-align: right;">
        <input type='text' style='text-align:center;padding-right:1px;padding-left:1px;width:350px;height: 30px;' id='search' name='search' value=''>
        <input type="submit" name="insert" value="Product Search" class="btn" onclick="<?php /* @escapeNotVerified */ echo $baseURL_l . 'quickorder/index/search'; ?>"/>
    </div>
    <br/>
    <?php echo $div; ?>
    <br/>
    <div style="text-align: center;">
        <input type="submit" name="insert" value="Add to Cart" class="btn" onclick="<?php /* @escapeNotVerified */ echo $baseURL_l . 'quickorder/index/add'; ?>"/>
    </div>
</form>
    <?php
}else{
    $div = "";
    $div .= "<div class='row'>";
    $div .= "<div class='custom_fav' style='background-color:lavenderblush;'>";
    $div .= "<h3>Favorites</h3>";
    $div .= "<p>No Records Found!</p>";
    $div .= "</div>";
    $div .= "<div class='custom_sku' style='background-color:lavenderblush;'>";
    $div .= "<h3>SKU</h3>";
    $div .= "<p>!</p>";
    $div .= "</div>";
    $div .= "<div class='custom_name' style='background-color:lavenderblush;'>";
    $div .= "<h3>Product Name</h3>";
    $div .= "<p>!</p>";
    $div .= "</div>";
    $div .= "<div class='custom_price' style='background-color:lavenderblush;'>";
    $div .= "<h3>Best Price</h3>";
    $div .= "<p>!</p>";
    $div .= "</div>";
    $div .= "<div class='custom_text' style='background-color:lavenderblush;'>";
    $div .= "<h3>Qty</h3>";
    $div .= "<p>!</p>";
    $div .= "</div>";
    $div .= "<div class='custom_cart_qty' style='background-color:lavenderblush;'>";
    $div .= "<h3>In Cart</h3>";
    $div .= "<p>!</p>";
    $div .= "</div>";
    $div .= "</div>";

    echo $div;

}

?>

Add.php controller

/**
     * Add product to shopping cart action
     *
     * @return \Magento\Framework\Controller\Result\Redirect
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     */
    public function execute()
    {

        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();

        $request = $this->getRequest()->getParams();
        try {


            $sku_list = [];

            foreach($request as $k => $v){
                if (strpos($k, 'qty_') !== false) {
                    $sku = explode('_',$k);
                    if($v !="" || $v !=0)
                        $sku_list[$sku[1]] = $v;
                }
            }

            $product = $this->_productFactory->create();

            foreach($sku_list as $k => $v){

                $data = $product->loadByAttribute('sku',$k);
                $product = $this->_initProduct1($data->getId());

                /**
                 * Check product availability
                 */
                if (!$product) {
                    return $this->goBack();
                }

                $requests = array('product' => $product->getId(),'qty' => $v);
                $_product = $this->productRepository->getById($product->getId());
                $qty = $v;

                if($_product->getTypeId() == "bundle"){

                    //
                    $productTypeInstance = $product->getTypeInstance();
                    $requiredChildrenIds = $productTypeInstance->getChildrenIds($_product->getId(), true);

                    $par = array('product' => $product->getId(),
                        "bundle_options" => array(),
                        'qty' => $v);
                    $magento = [];
                    foreach($requiredChildrenIds as $child){
                        $i =0;
                        foreach($child as $k => $v){
                            $i++;
                            $prod = $this->productRepository->getById($v);
                            $isSaleable = $prod->isSalable();
                            if($isSaleable){
                                $magento[$v] = "$i";
                            }else{
                                $message = __('No stock for this Item',$_product->getName());
                                $this->messageManager->addErrorMessage($message);
                                return $this->goBack(null, $product);
                            }
                        }

                    }
                    $par = array(
                        'product' => $product->getId(),
                        'bundle_options' => array(  1 => $magento),
                        'qty' => $qty
                    );

                    $this->cart->addProduct($_product,$par);

                }else{
                    $this->cart->addProduct($_product,$requests);
                }



                $message = __('You added %1 to your shopping cart.',$product->getName());
                $this->messageManager->addSuccessMessage($message);

            }

            $this->cart->save();

            return $resultRedirect->setPath('quickorder/index/index');

        }  catch (\Exception $e) {
            $this->messageManager->addException($e, __('We can\'t add this item to your shopping cart right now.'));
            $this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
            return $this->goBack();
        }
    }

After adding product to shopping cart showing success message see below picenter image description here.

and not showing the red icon in minicart see above pic. When I navigate browser to cart page showing products in cart page. But I con is not showing.

Any suggestings or help?

1

Finally I solved my self by adding below code.

add section.xml to etc/frontend

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="[frontName]/[ActionPath]/[ActionName]">
        <section name="cart"/>
    </action>
</config>

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.