0

I am using Magento 2.2.3

enter image description here

I want to add a particular product with particular option into cart when I click on "Test" button.I want to add below product to cart

refer above screenshot

When I click on "Test" button besides product "Test Downloadable product" it will add below product "Test" with it's first option "support for test downloadable product" into cart.

When I click on "Test" button besides product "Test Downloadable product 2" it will add below product "Test" with it's second option "support for test downloadable product 2" into cart.

enter image description here

I have edited below file:

app/design/frontend/my_vendor/my_theme/Magento_Downloadable/templates/customer/products/list.phtml

 <?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php
/**
 * @var $block \Magento\Downloadable\Block\Customer\Products\ListProducts
 */
?>

<?php $_items = $block->getItems(); ?>
<?php 


/*$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objDate = $objectManager->create('Magento\Framework\Stdlib\DateTime\DateTime');
$date = $objDate->gmtDate();*/
?>

<?php if (count($_items)): ?>
    <div class="table-wrapper downloadable-products">
        <table id="my-downloadable-products-table" class="data table table-downloadable-products">
            <caption class="table-caption"><?= /* @escapeNotVerified */ __('Downloadable Products') ?></caption>
            <thead>
                <tr>
                    <th scope="col" class="col id"><?= /* @escapeNotVerified */ __('Order #') ?></th>
                    <th scope="col" class="col date"><?= /* @escapeNotVerified */ __('Date') ?></th>
                    <th scope="col" class="col title"><?= /* @escapeNotVerified */ __('Title') ?></th>
                    <th scope="col" class="col id"><?= /* @escapeNotVerified */ __('Download Link') ?></th>
                    <th scope="col" class="col id"><?= /* @escapeNotVerified */ __('Support') ?></th>
                    <?php /*?><th scope="col" class="col status"><?=  __('Status') ?></th>
                    <th scope="col" class="col remaining"><?=  __('Remaining Downloads') ?></th><?php */?>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($_items as $_item): ?>
                <tr>
                    <td data-th="<?= $block->escapeHtml(__('Order #')) ?>" class="col id">
                        <a href="<?= /* @escapeNotVerified */ $block->getOrderViewUrl($_item->getPurchased()->getOrderId()) ?>"
                            title="<?= $block->escapeHtml(__('View Order')) ?>">
                            <?= /* @escapeNotVerified */ $_item->getPurchased()->getOrderIncrementId() ?>
                        </a>
                    </td>
                    <td data-th="<?= $block->escapeHtml(__('Date')) ?>" class="col date"><?= /* @escapeNotVerified */ $block->formatDate($_item->getPurchased()->getCreatedAt()) ?></td>
                    <td data-th="<?= $block->escapeHtml(__('Title')) ?>" class="col title">
                        <strong class="product-name"><?= $block->escapeHtml($_item->getPurchased()->getProductName()) ?></strong>
                    </td>
                    <td data-th="<?= $block->escapeHtml(__('Download Link')) ?>" class="col id">
                        <?php if ($_item->getStatus() == \Magento\Downloadable\Model\Link\Purchased\Item::LINK_STATUS_AVAILABLE): ?>
                        <a href="<?= /* @escapeNotVerified */ $block->getDownloadUrl($_item) ?>" title="<?= $block->escapeHtml(__('Start Download')) ?>" class="action download" <?= $block->getIsOpenInNewWindow() ? 'onclick="this.target=\'_blank\'"' : '' ?>><?= $block->escapeHtml($_item->getLinkTitle()) ?></a>
                        <?php endif; ?>
                    </td>
                    <td data-th="<?= $block->escapeHtml(__('Support')) ?>" class="col id">
                        <?php
                            //$orderDate = $block->formatDate($_item->getPurchased()->getCreatedAt());
                            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

                            $id = 63;
                            $product = $objectManager->get('\Magento\Catalog\Model\Product')->load($id);
                            //$customOptions = $objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product);
                            $sku = $objectManager->get('\Magento\Catalog\Model\Product')->getSku($_item->getPurchased());
                            $listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');
                            $option = array('9'=>18);
                            $params = array(
                                                'product' => $id, //product Id               
                                                'options' => $option
                                                );
                            $postDataHelper = $this->helper('Magento\Framework\Data\Helper\PostHelper');
                            //$addToCartUrl =  $listBlock->getAddToCartUrl($product);
                            $postData = $postDataHelper->getPostData($listBlock->getAddToCartUrl($product), [$params]);

                        /*  $item_id = 63; //product id
                            $_product = $objectManager->get('\Magento\Catalog\Model\Product')->load($item_id);
                            $postDataHelper = $this->helper('Magento\Framework\Data\Helper\PostHelper');
                            $postData = $postDataHelper->getPostData($block->getAddToCartUrl($_product), ['product' => $item_id, 'options' => array('9'=>18)]);*/
                        ?>

                        <?php 

                            /*$productId = 63;
                            $postDataHelper = $this->helper('Magento\Framework\Data\Helper\PostHelper');
                            $pro = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
                            $cart = $objectManager->create('Magento\Checkout\Model\Cart');  
                            $formKey = $objectManager->create('\Magento\Framework\Data\Form\FormKey')->getFormKey();  
                            $option = array('9'=>18);

                            $params = array(
                                                'form_key' => $formKey,
                                                'product' => $productId, //product Id               
                                                'options' => $option
                                                );
                            $postData = $postDataHelper->getPostData($block->getAddToCartUrl($pro, $params));*/

                            //$cart->addProduct
                        ?>

                        <form data-role="tocart-form" action="<?php echo $postData;  ?>" method="post"> 
                        <?php echo $block->getBlockHtml('formkey')?>



                            <?php /*?><a href="#" data-post="<?php ?>"><span>Extend Support</span></a><?php */?>                    
                            <button class="" type="button" title="" ><span>Test</span></button>




                        </form>
                    </td>
                    <?php /*?><td data-th="<?= $block->escapeHtml(__('Status')) ?>" class="col status"><?=  __(ucfirst($_item->getStatus())) ?></td><?php */?>
                    <?php /*?><td data-th="<?= $block->escapeHtml(__('Remaining Downloads')) ?>" class="col remaining"><?=  $block->getRemainingDownloads($_item) ?></td><?php */?>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php if ($block->getChildHtml('pager')): ?>
        <div class="toolbar downloadable-products-toolbar bottom">
            <?= $block->getChildHtml('pager') ?>
        </div>
    <?php endif; ?>
<?php else: ?>
    <div class="message info empty"><span><?= /* @escapeNotVerified */ __('You have not purchased any downloadable products yet.') ?></span></div>
<?php endif; ?>

<div class="actions-toolbar">
    <div class="secondary">
        <a href="<?= $block->escapeUrl($block->getBackUrl()) ?>" class="action back">
            <span><?= /* @escapeNotVerified */ __('Back') ?></span>
        </a>
    </div>
</div>

I have added some code in above file but still it is not working so anyone have any idea how can i achieve this?

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.