0

I create event with observer and execute it . In execute function I get API respond (PDF file) for each order request . How can save this pdf link with order id in table I create it (Install schema) ?

  • You want to save the link in an order? You may need to create an order attribute. If this is the way you go, I can answer you. – PY Yick Oct 23 '18 at 4:02
  • Great idea you mean Create EAV then ?? – magento2develop Oct 23 '18 at 17:32
  • Yes you're right. Are you sure you want to go this way? – PY Yick Oct 24 '18 at 2:44
  • yes why not ? you have another idea ? – magento2develop Oct 24 '18 at 21:30
0

I'll show you the answer into 3 parts:

  1. Create Order Attribute
  2. Set the order attribute
  3. Show the PDF link in admin order page

Create Order Attribute

For creating order attribute, you MUST use setup script with the following code:

<?php


namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Quote\Setup\QuoteSetupFactory;
use Magento\Sales\Setup\SalesSetupFactory;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var QuoteSetupFactory
     */
    protected $quoteSetupFactory;

    /**
     * @var SalesSetupFactory
     */
    protected $salesSetupFactory;

    /**
     * @var EavSetupFactory
     */
    protected $eavSetupFactory;

    /**
     * Constructor
     */
    public function __construct(
        QuoteSetupFactory $quoteSetupFactory,
        SalesSetupFactory $salesSetupFactory,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->quoteSetupFactory = $quoteSetupFactory;
        $this->salesSetupFactory = $salesSetupFactory;
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.0.1', '<')) {

            /** @var \Magento\Quote\Setup\QuoteSetup $quoteInstaller */
            $quoteInstaller = $this->quoteSetupFactory->create(['resourceName' => 'quote_setup', 'setup' => $setup]);

            /** @var \Magento\Sales\Setup\SalesSetup $salesInstaller */
            $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);

            //Add attributes to quote
            $entityAttributesCodes = [
                'pdf_file_link' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            ];

            foreach ($entityAttributesCodes as $code => $type) {
                $quoteInstaller->addAttribute('quote', $code, ['type' => $type, 'length' => 255, 'visible' => true, 'nullable' => true,]);
                $salesInstaller->addAttribute('order', $code, ['type' => $type, 'length' => 255, 'visible' => true, 'nullable' => true,]);
        }
        $setup->endSetup();
    }
}

Set the order attribute

You said you created an observer, so in your observer file, add this code:

$order->setData("pdf_file_link", $url);

Show the PDF link in admin order page

Create file

app/code/Vendor/Module/view/adminhtml/templates/order/view/info.phtml

Add the content below in anywhere you like:

<?= $block->escapeHtml($order->getData("pdf_file_link")) ?>
| improve this answer | |
  • okay let me test today and back to you . – magento2develop Oct 26 '18 at 11:23
  • *Order Attribute - you forget } for class. also something wrong i cant find attrbiute in database . – magento2develop Oct 26 '18 at 14:52
  • You can look at table sales_order. You should have a new attribute at the end. – PY Yick Oct 26 '18 at 17:53
  • Sure i already look at it – magento2develop Oct 26 '18 at 18:18
  • Did you enable the module after saving the setup script? – PY Yick Oct 27 '18 at 2:28

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.