Showing posts with label custom module. Show all posts
Showing posts with label custom module. Show all posts

Wednesday, April 15, 2020

Add custom tab in logged user account in magento 2

Hello All,

Today I saw you How to create custom tab in logged in user account in magento 2.

Custom tab will display like this:



app\code\Chirag\Customproduct\registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Chirag_Customproduct',
    __DIR__
);


app\code\Chirag\Customproduct\etc\module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Chirag_Customproduct" setup_version="1.0.0" >
<sequence>
        <module name="Magento_Customer"/>
    </sequence>
</module>
</config>


app\code\Chirag\Customproduct\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\Order\Pdf\Invoice"
                type="Czargroup\Pdfinvoice\Model\Order\Pdf\Invoice"/>
</config>


app\code\Chirag\Customproduct\etc\frontend\routes.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="customproduct" frontName="customproduct">
            <module name="Chirag_Customproduct" />
        </route>
    </router>
</config>


app\code\Chirag\Customproduct\Controller\Customer\Index.php

<?php
namespace Chirag\Customproduct\Controller\Customer; 
class Index extends \Magento\Framework\App\Action\Action {
 
 public function execute() {
 
    $this->_view->loadLayout();
    $this->_view->renderLayout();
  }
 
}
?> 


app\code\Chirag\Customproduct\view\frontend\layout\customer_account.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <body>
      <referenceBlock name="customer_account_navigation">
         <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-customproduct">
            <arguments>
               <argument name="path" xsi:type="string">customproduct/customer/index</argument>
               <argument name="label" xsi:type="string">Custom Product</argument>
            </arguments>
         </block>
      </referenceBlock>
   </body>
</page> 


app\code\Chirag\Customproduct\view\frontend\layout\customproduct_customer_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<update handle="customer_account"/>
<body>
    <referenceBlock name="page.main.title">
            <action method="setPageTitle">
                <argument translate="true" name="title" xsi:type="string">Custom Product</argument>
            </action>
     </referenceBlock>
     <referenceContainer name="content">
        <block class="Magento\Framework\View\Element\Template" name="customproduct" template="Chirag_Customproduct::customproduct.phtml">
        </block>
    </referenceContainer>
</body>
</page>   


app\code\Chirag\Customproduct\view\frontend\templates\customproduct.phtml

<?php
 // Add Some Code Here 
echo "This is custom Product List";
?> 


Tuesday, April 14, 2020

Add footer and change label in PDF Invoice in magento 2

Hello

Today I saw you how to change label and add footer to PDF Invoice.

For doing this, we have to make module and override invoice file.

Create extension like this.

app/code/Czargroup/Pdfinvoice/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Czargroup_Pdfinvoice',
    __DIR__
);


app/code/Czargroup/Pdfinvoice/etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Czargroup_Pdfinvoice" setup_version="1.0.0" />
</config>


app/code/Czargroup/Pdfinvoice/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\Order\Pdf\Invoice"
                type="Czargroup\Pdfinvoice\Model\Order\Pdf\Invoice"/>
</config>


app/code/Czargroup/Pdfinvoice/Model/Order/Pdf/Invoice.php

<?php
namespace Czargroup\Pdfinvoice\Model\Order\Pdf;

class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice
{
  
    /**
     * Draw header for item table
     *
     * @param \Zend_Pdf_Page $page
     * @return void
     */
    protected function _drawHeader(\Zend_Pdf_Page $page)
    {
        /* Add table head */
        $this->_setFontRegular($page, 10);
        $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
        $page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.5));
        $page->setLineWidth(0.5);
        $page->drawRectangle(25, $this->y, 570, $this->y - 15);
        $this->y -= 10;
        $page->setFillColor(new \Zend_Pdf_Color_Rgb(0, 0, 0));

        //columns headers
        $lines[0][] = ['text' => __('Products'), 'feed' => 35];

        $lines[0][] = ['text' => __('SKU'), 'feed' => 290, 'align' => 'right'];

        $lines[0][] = ['text' => __('Qty'), 'feed' => 435, 'align' => 'right'];

        $lines[0][] = ['text' => __('Price'), 'feed' => 360, 'align' => 'right'];

        $lines[0][] = ['text' => __('VAT'), 'feed' => 495, 'align' => 'right'];

        $lines[0][] = ['text' => __('Subtotal'), 'feed' => 565, 'align' => 'right'];

        $lineBlock = ['lines' => $lines, 'height' => 5];

        $this->drawLineBlocks($page, [$lineBlock], ['table_header' => true]);
        $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
        $this->y -= 20;
    }

    /**
     * Return PDF document
     *
     * @param array|Collection $invoices
     * @return \Zend_Pdf
     */
    public function getPdf($invoices = [])
    {
        $this->_beforeGetPdf();
        $this->_initRenderer('invoice');

        $pdf = new \Zend_Pdf();
        $this->_setPdf($pdf);
        $style = new \Zend_Pdf_Style();
        $this->_setFontBold($style, 10);

        foreach ($invoices as $invoice) {
            if ($invoice->getStoreId()) {
                $this->_localeResolver->emulate($invoice->getStoreId());
                $this->_storeManager->setCurrentStore($invoice->getStoreId());
            }
            $page = $this->newPage();
            $order = $invoice->getOrder();
            /* Add image */
            $this->insertLogo($page, $invoice->getStore());
            /* Add address */
            $this->insertAddress($page, $invoice->getStore());
            /* Add head */
            $this->insertOrder(
                $page,
                $order,
                $this->_scopeConfig->isSetFlag(
                    self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID,
                    \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
                    $order->getStoreId()
                )
            );
            /* Add document text and number */
            $this->insertDocumentNumber($page, __('Invoice # ') . $invoice->getIncrementId());
            /* Add table */
            $this->_drawHeader($page);
            /* Add body */
            foreach ($invoice->getAllItems() as $item) {
                if ($item->getOrderItem()->getParentItem()) {
                    continue;
                }
                /* Draw item */
                $this->_drawItem($item, $page, $order);
                $page = end($pdf->pages);
            }
            /* Add totals */
            $this->insertTotals($page, $invoice);
            if ($invoice->getStoreId()) {
                $this->_localeResolver->revert();
            }
        }
        $this->_drawFooter($page); // the custom text is added here
        $this->_afterGetPdf();
        return $pdf;
    }

    /**
     * Create new page and assign to PDF object
     *
     * @param  array $settings
     * @return \Zend_Pdf_Page
     */
    public function newPage(array $settings = [])
    {
        /* Add new table head */
        $page = $this->_getPdf()->newPage(\Zend_Pdf_Page::SIZE_A4);
        $this->_getPdf()->pages[] = $page;
        $this->y = 800;
        if (!empty($settings['table_header'])) {
            $this->_drawHeader($page);
        }
        return $page;
    }
    public function _drawFooter(\Zend_Pdf_Page $page)
        {
            $this->y =50;    
            //$page->setFillColor(new \Zend_Pdf_Color_RGB(1, 1, 1));
            $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
            //$page->setLineWidth(0.5);
            //$page->drawRectangle(60, $this->y, 510, $this->y -30);

            $page->setFillColor(new \Zend_Pdf_Color_RGB(0.1, 0.1, 0.1));
            $page->setFont(\Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_HELVETICA), 11);
            //$this->y -=0;
            $page->drawText("OutdoorLivingUK Products Ltd, Unit C Oaklea House, Limesquare Estate, Peterborough PE3 8YQ", 70, $this->y, 'UTF-8');
            $this->y -=15;
            $page->drawText("Company Reg 07807363", 70, $this->y, 'UTF-8');
            //$page->drawText("Tel: +123 456 676", 230, $this->y, 'UTF-8');
            $page->drawText("VAT 169691651", 470, $this->y, 'UTF-8');
        }
}

It will display result like this: