php - Magento 2 如何覆盖抽象类中的 protected 函数?

标签 php abstract-class magento2

我想覆盖magento2中抽象类的 protected 函数

这是我的代码

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\AbstractPdf" type="Custom\Sales\Model\Order\Pdf\AbstractPdf" />
</config>

AbstractPdf.php(Custom/Sales/Model/Order/Pdf/AbstractPdf.php)

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

// @codingStandardsIgnoreFile

namespace Custom\Sales\Model\Order\Pdf;

// use Magento\Eav\Model\Entity\Attribute as EntityAttribute;
// use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
// use Magento\Framework\DB\Select;
// use Magento\Framework\Model\AbstractModel;
// use Magento\Framework\App\Filesystem\DirectoryList;
// use Magento\Framework\DataObject;

/**
 * Sales Order PDF abstract model
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class AbstractPdf extends \Magento\Sales\Model\Order\Pdf\AbstractPdf
{

    /**
     * Insert logo to pdf page
     *
     * @param \Zend_Pdf_Page &$page
     * @param null $store
     * @return void
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     */
    protected function insertLogo(&$page, $store = null)
    {
        echo "override";die;
    }

}

我已经用上面提到的文件覆盖了核心模型,但我无法得到解决方案。

请帮我解决这个问题。

最佳答案

您不能覆盖 protected 函数。但是,您可以覆盖调用该 protected 方法的公共(public)方法。在我的例子中,我需要覆盖名为 insertLogo 的方法。但是,这是我无法覆盖的 protected 方法。所以我覆盖了在 getPdf 方法中调用 insertLogo 的 Invoice.php。在同一个文件中,我重新定义了 insertLogo

的代码

更新

这是文件

app/code/Vendor/Modulename/Model/Order/Pdf/Invoice.php

代码

namespace Vendor\Modulename\Model\Order\Pdf;
class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice
{
    public function getPdf($invoices = [])
    {
            //some code

            $order = $invoice->getOrder();
            /* Add image */
            //$this->insertLogo($page, $invoice->getStore());
            /* Calling custom function*/
            $this->insertLogoCustom($page, $invoice->getStore());
            /* Add address */
            $this->insertAddress($page, $invoice->getStore());

            //some more code

        return $pdf;
    }
    protected function insertLogoCustom(&$page, $store = null)
    {
        $this->y = $this->y ? $this->y : 815;
        $image = $this->_scopeConfig->getValue(
            'sales/identity/logo',
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $store
        );
        if ($image) {
            $imagePath = '/sales/store/logo/' . $image;
            if ($this->_mediaDirectory->isFile($imagePath)) {
                $image = \Zend_Pdf_Image::imageWithPath($this->_mediaDirectory->getAbsolutePath($imagePath));
                $top = 830;
                //top border of the page
                $widthLimit = 270;
                //half of the page width
                $heightLimit = 270;
                //assuming the image is not a "skyscraper"
                /* Modified this code to convert pixel in points */
                $width = $image->getPixelWidth()* 72 / 96;
                $height = $image->getPixelHeight()* 72 / 96;

                //preserving aspect ratio (proportions)
                $ratio = $width / $height;

                if ($ratio > 1 && $width > $widthLimit) {
                    $width = $widthLimit;
                    $height = $width / $ratio;
                } elseif ($ratio < 1 && $height > $heightLimit) {
                    $height = $heightLimit;
                    $width = $height * $ratio;
                } elseif ($ratio == 1 && $height > $heightLimit) {
                    $height = $heightLimit;
                    $width = $widthLimit;
                }

                $y1 = $top - $height;
                $y2 = $top;
                $x1 = 25;
                $x2 = $x1 + $width;


                //coordinates after transformation are rounded by Zend
                $page->drawImage($image, $x1, $y1, $x2, $y2);

                $this->y = $y1 - 10;
            }
        }
    }

}

希望对您有所帮助!

关于php - Magento 2 如何覆盖抽象类中的 protected 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44241663/

相关文章:

php - 对我的号码下订单 natsort();效果不好

php - Doctrine2 多对多逆向查询生成器

php - 在 Laravel 的路由组中跳过特定路由的中间件

java - 如何以及何时使用抽象类

magento2 - Magento 2.2.0 - 无法添加捆绑产品

deployment - Magento2不自动生成静态文件

php - Magento 2 对齐添加到 new_grid.phtml 上的汽车按钮

php - password_verify() 不适用于数据库

c++ - 我怎样才能让一个类实现一个接口(interface)....(C++)

php - 当我的抽象类实现接口(interface)时,我应该创建抽象方法吗?