Skip to content

Commit 98edb4e

Browse files
ENGCOM-8400: #24730: Fix Frontend Invoice PDF configured Logo image #30632
- Merge Pull Request #30632 from mozok/magento2:fix-for-issue-24730 - Merged commits: 1. 2389da7 2. 6827503 3. 9181adc 4. 4d0debd 5. 7b7b83a 6. 182a65c 7. 0287723 8. 65bb095 9. f229c75 10. 5855832
2 parents c644b3d + 5855832 commit 98edb4e

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Model\Order\Invoice;
9+
10+
use Magento\Store\Model\ScopeInterface;
11+
use Magento\Framework\UrlInterface;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
14+
/**
15+
* Get Custom Logo File for Invoice HTML print
16+
*/
17+
class GetLogoFile
18+
{
19+
private const XML_PATH_SALES_IDENTITY_LOGO_HTML = 'sales/identity/logo_html';
20+
private const LOGO_BASE_DIR = 'sales/store/logo_html/';
21+
22+
/**
23+
* @var ScopeConfigInterface
24+
*/
25+
private $scopeConfig;
26+
27+
/**
28+
* @var UrlInterface
29+
*/
30+
private $urlBuilder;
31+
32+
/**
33+
* @param ScopeConfigInterface $scopeConfig
34+
* @param UrlInterface $urlBuilder
35+
*/
36+
public function __construct(
37+
ScopeConfigInterface $scopeConfig,
38+
UrlInterface $urlBuilder
39+
) {
40+
$this->scopeConfig = $scopeConfig;
41+
$this->urlBuilder = $urlBuilder;
42+
}
43+
44+
/**
45+
* Return Custom Invoice Logo file url if configured in admin
46+
*
47+
* @return string|null
48+
*/
49+
public function execute(): ?string
50+
{
51+
$invoiceLogoPath = $this->getIdentityLogoHtml();
52+
if (!$invoiceLogoPath) {
53+
return null;
54+
}
55+
56+
return sprintf(
57+
"%s%s%s",
58+
$this->urlBuilder->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]),
59+
self::LOGO_BASE_DIR,
60+
$invoiceLogoPath
61+
);
62+
}
63+
64+
/**
65+
* Get Admin Configuration for Invoice Logo HTML
66+
*
67+
* @return null|string
68+
*/
69+
private function getIdentityLogoHtml(): ?string
70+
{
71+
return $this->scopeConfig->getValue(
72+
self::XML_PATH_SALES_IDENTITY_LOGO_HTML,
73+
ScopeInterface::SCOPE_STORE,
74+
null
75+
);
76+
}
77+
}

app/code/Magento/Sales/view/frontend/layout/sales_order_printinvoice.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
<update handle="print" />
1212
<body>
1313
<attribute name="class" value="account"/>
14+
<referenceContainer name="header-wrapper">
15+
<referenceBlock name="logo">
16+
<arguments>
17+
<argument name="logo_src" xsi:type="helper" helper="Magento\Sales\Model\Order\Invoice\GetLogoFile::execute"/>
18+
</arguments>
19+
</referenceBlock>
20+
</referenceContainer>
1421
<referenceContainer name="page.main.title">
1522
<block class="Magento\Sales\Block\Order\PrintOrder\Invoice" name="order.status" template="Magento_Sales::order/order_status.phtml" />
1623
<block class="Magento\Sales\Block\Order\PrintOrder\Invoice" name="order.date" template="Magento_Sales::order/order_date.phtml" />
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Model\Order\Invoice;
9+
10+
use Magento\Store\Model\ScopeInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\ObjectManager;
13+
use Magento\Framework\App\Config\MutableScopeConfigInterface;
14+
15+
/**
16+
* Test class for \Magento\Sales\Model\Order\Invoice\GetLogoFile
17+
*/
18+
class GetLogoFileTest extends \PHPUnit\Framework\TestCase
19+
{
20+
private const XML_PATH_SALES_IDENTITY_LOGO_HTML = 'sales/identity/logo_html';
21+
private const DUMP_IMAGE = 'my_dump_logo.png';
22+
23+
/**
24+
* @var ObjectManager
25+
*/
26+
private $objectManager;
27+
28+
/**
29+
* @var MutableScopeConfigInterface
30+
*/
31+
private $scopeConfig;
32+
33+
/**
34+
* @var GetLogoFile
35+
*/
36+
private $getLogoFile;
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
protected function setUp(): void
42+
{
43+
$this->objectManager = Bootstrap::getObjectManager();
44+
$this->scopeConfig = $this->objectManager->get(MutableScopeConfigInterface::class);
45+
$this->getLogoFile = $this->objectManager->get(GetLogoFile::class);
46+
}
47+
48+
/**
49+
* Check that GetLogoFile return image after Admin configuration is changed
50+
*
51+
* @return void
52+
*/
53+
public function testExecute(): void
54+
{
55+
$this->assertNull($this->getLogoFile->execute());
56+
57+
$this->applyImage();
58+
59+
$this->assertIsString($this->getLogoFile->execute());
60+
$this->assertStringContainsString(self::DUMP_IMAGE, $this->getLogoFile->execute());
61+
}
62+
63+
/**
64+
* Set Invoice Custom Logo HTML Image configuration
65+
*
66+
* @return void
67+
*/
68+
private function applyImage(): void
69+
{
70+
$this->scopeConfig->setValue(
71+
self::XML_PATH_SALES_IDENTITY_LOGO_HTML,
72+
self::DUMP_IMAGE,
73+
ScopeInterface::SCOPE_STORE
74+
);
75+
}
76+
}

0 commit comments

Comments
 (0)