2

So I have added a custom button and onclick function to the edit product page, the button appears fine. I have set an onclick function, and the code for this runs when the edit product page is loaded, but not when I click the button. I only need it to run if the button is clicked. I have overridden the Mage/Adminhtml/Block/Catalog/Product/Edit.php and added the following code:

Add to the end of the peraperlayout function:

$this->_product = $this->getProduct();
            $this->setChild('printbarcode_button',
            $this->getLayout()->createBlock('adminhtml/widget_button')
                ->setData(array(
                'label'     => 'Print Barcode',
                'onclick'   => $this->getPrintLabelUrl()
            ))
        );

added:

public function getBarcodeButtonHtml()
    {
        return $this->getChildHtml('printbarcode_button');
    }

amended:

public function getDuplicateButtonHtml()
    {
        return $this->getChildHtml('duplicate_button') . $this->getChildHtml('printbarcode_button');
    }

and the function I have wrote that I want to run on click, and not on load, as it is doing now:

public function getPrintLabelUrl()
    {
        //This downloads a PDF on demand        

        try {
            //get param
            $productId = $this->getProductId();
            $qty = 1;
            Mage::log('Trying to autoprint barcodes, SKU: ' . $productId . " Qty: " . $qty, null, 'custom_log.log');
            //create pdf & download
            $obj = mage::getModel('AdvancedStock/Pdf_BarcodeLabels');

            $pdf = $obj->getPdf(array($productId => $qty));

            $fileName = 'barcode.pdf';
            mage::helper('ClientComputer')->printDocument($pdf->render(), $fileName, 'Barcodes : print barcode');
        } catch (Exception $ex) {
            die("Error auto sending PDF to printer: " . $ex->getMessage() . '<p>' . $ex->getTraceAsString());
        }
    }

Any help/guidance would be appreciated

Thanks

0

The function getPrintLabelUrl should actually return an Url, but not do any actions. So you have to create a controller, then move Pdf generation logic to the controller and make function getPrintLabelUrl to return url path of your new controller

0

Thanks Neklo,

Yeah, that is the correct way of doing it.

However, I ended up doing it in a bit of a rough way. I put the PDF creation code inside an if statement on if a GET parameter has been set, and changed the onclick function to put the GET parameter onto the end of the current url and reload the page. It works for what I need. As far as I can tell, it doesn't have any adverse effects

Thanks for your help! Will probably re-write it in the above way in the future.

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.