2017-05-29 1 views
1

Je veux remplacer la fonction protégée de classe abstraite dans magento2Magento 2 Comment remplacer une fonction protégée dans une classe abstraite?

ici est mon code

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 (Personnalisé/Ventes/Modèle/commande/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; 
    } 

} 

J'ai passer outre le modèle de base avec le fichier mentionné ci-dessus, mais je ne peux pas obtenir la solution.

S'il vous plaît aidez-moi à résoudre ce problème.

Répondre

2

Vous ne pouvez pas remplacer une fonction protégée. Cependant, vous pouvez remplacer la méthode publique qui appelle cette méthode protégée. Dans mon cas, je devais remplacer la méthode appelée insertLogo. Cependant, ceci étant la méthode protégée que je ne pouvais pas remplacer. Donc, je remplace le Invoice.php qui appelait insertLogo dans la méthode getPdf. Dans le même fichier que je redéfini le code pour insertLogo

MISE À JOUR

Voici le fichier

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

code

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; 
      } 
     } 
    } 

} 

Hope it helps!

+0

Merci beaucoup @Sejal shah, son écrasé avec succès. – Rana

+0

Salut tous, Comment nous pouvons remplacer ce modèle dans mangento 2 fournisseur \ magento \ module-sales \ Modèle \ Order \ Email \ Sender.php Dans ce fichier, je veux remplacer la méthode prepareTemplate(). –

+0

@Pramod Kharade Veuillez soulever une nouvelle question. –