0

I got following code:

<?php
namespace UV\PrintBestellschein\Block\Adminhtml;
use Magento\Framework\App\Filesystem\DirectoryList;
class PrintBestellscheinButton extends \Magento\Backend\Block\Widget\Container
{

    protected $coreRegistry = null;
    public function __construct(
        \Magento\Backend\Block\Widget\Context $context,
        \Magento\Framework\Registry $registry,
        \UV\Bestellung\Model\Order\Pdf\Shipment $pdfGenerator,
        array $data = []
    ) {
        $this->pdfGenerator = $pdfGenerator;
        $this->coreRegistry = $registry;
        parent::__construct($context, $data);
    }

    protected function _construct()
    {
        $this->addButton(
            'uv_printbestellschein',
            [
                'label'   => 'Bestellschein drucken',
                'class'   => 'print',
                'onclick' => 'setLocation(\'' . $this->startDownload() . '\')'
            ]
        );

        parent::_construct();
    }

    public function startDownload() {

        $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
        $order = $objectManager->create('Magento\Sales\Model\Order')->load($this->getOrderId());

        if($order->hasShipments()) {
            #Sendungen vorhanden
            $shipments = $order->getShipmentsCollection();
            $content = $this->pdfGenerator->getPdf($shipments);
            $fileFactory = $objectManager->get('\Magento\Framework\App\Response\Http\FileFactory');
            $fileFactory->create('packing-slip.pdf', $content->render(), DirectoryList::VAR_DIR , 'application/octet-stream'); 

        } else {
            #Keine Sendungen vorhanden
        }
    }

    public function getOrderId()
    {
       return $this->coreRegistry->registry('sales_order')->getId();
    }
}

The button is successfully added to

sales_order_grid

When I click on an order it starts download, which only should start when I press the button.

Why is that happening? Thanks!

  • have you created any action for this? – Abdul Apr 19 '18 at 10:45
1

In your button definition replace onclick with on_click

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.