Magento 2.4 – Customize PayPal Smart Button Style

checkoutmagento2modulepaypalPHP

I want to change the button style of the PayPal Express Checkout, but is not work, I have create a module to rewrite the PayPal but how I can add my changes there?

  1. I create di.xml file on following location

app\code\Vendor\Extension\etc\frontend\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\Paypal\Model\SmartButtonConfig" type="Vendor\Extension\Model\SmartButtonConfig" />
</config>
  1. create SmartButtonConfig.php on following location

app\code\Vendor\Extension\Model\SmartButtonConfig.php

namespace Vendor\Extension\Model;

class SmartButtonConfig extends \Magento\Paypal\Model\SmartButtonConfig
{
   //do the code
}

and I have this function:

Public function:

private function getButtonStyles(string $page): array
{
    $styles = $this->defaultStyles[$page];
    if ((boolean)$this->config->getValue("{$page}_page_button_customize")) {
        $styles['layout'] = $this->config->getValue("{$page}_page_button_layout");
        $styles['size'] = $this->config->getValue("{$page}_page_button_size");
        $styles['color'] = $this->config->getValue("{$page}_page_button_color");
        $styles['shape'] = $this->config->getValue("{$page}_page_button_shape");
        $styles['label'] = $this->config->getValue("{$page}_page_button_label");
        $styles = $this->updateStyles($styles, $page);
    }
    return $styles;
}

I want to add only this, this are my changes:

$styles['height'] = 55;
$styles['tagline'] = false;
$styles['fundingicons'] = false;

in the private function getButtonStyles

so what I want to do is to not overwrite the vendor file. so this function private function getButtonStyles exist already in the vendor/magento/module-paypal/Model/SmartButtonConfig.php,

I just want to add in this function

$styles['height'] = 55;
$styles['tagline'] = false;
$styles['fundingicons'] = false; 

and to be in a seperate module, because I don't want to lose the modifications when I'll upgrade the store .

Thank you

Best Answer

No need to create preference, we can use type

create:

app/code/Vendor/Extension/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Paypal\Model\SmartButtonConfig">
        <arguments>
            <argument name="defaultStyles" xsi:type="array">
                <item name="checkout" xsi:type="array">
                    <item name="height" xsi:type="number">55</item>
                    <item name="tagline" xsi:type="boolean">false</item>
                    <item name="fundingicons" xsi:type="boolean">false</item>
                </item>
                <item name="cart" xsi:type="array">
                    <item name="height" xsi:type="number">55</item>
                    <item name="tagline" xsi:type="boolean">false</item>
                    <item name="fundingicons" xsi:type="boolean">false</item>
                </item>
                <item name="mini_cart" xsi:type="array">
                    <item name="height" xsi:type="number">55</item>
                    <item name="tagline" xsi:type="boolean">false</item>
                    <item name="fundingicons" xsi:type="boolean">false</item>
                </item>
                <item name="product" xsi:type="array">
                    <item name="height" xsi:type="number">55</item>
                    <item name="tagline" xsi:type="boolean">false</item>
                    <item name="fundingicons" xsi:type="boolean">false</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

Note: you can remove any node if don't require (i.e.checkout,cart,mini_cart,product)

Related Topic