Skip to content

Commit

Permalink
optimize sdk loading
Browse files Browse the repository at this point in the history
  • Loading branch information
empiricompany committed Jul 23, 2023
1 parent 96887ad commit f5f4123
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 34 deletions.
12 changes: 0 additions & 12 deletions app/code/local/MM/Paypalpaylater/Block/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@ class MM_Paypalpaylater_Block_Messages extends Mage_Core_Block_Template {
*/
protected $_paymentMethodCode = Mage_Paypal_Model_Config::METHOD_WPP_EXPRESS;

const SDK_SRC = 'https://www.paypal.com/sdk/js?client-id=%s&locale=%s&currency=%s&components=messages&enable-funding=paylater';

public function getSrcSdk() {

return sprintf( MM_Paypalpaylater_Block_Messages::SDK_SRC,

Mage::helper('mm_paypalpaylater')->getClientId(),
Mage::app()->getLocale()->getLocaleCode(),
Mage::app()->getStore()->getCurrentCurrencyCode()
);
}

public function getAmount() {
/** @var Mage_Catalog_Model_Product $currentProduct */
$currentProduct = Mage::registry('current_product');
Expand Down
19 changes: 19 additions & 0 deletions app/code/local/MM/Paypalpaylater/Helper/Data.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
<?php
class MM_Paypalpaylater_Helper_Data extends Mage_Core_Helper_Abstract {

const SDK_SRC = 'https://www.paypal.com/sdk/js?client-id=%s&locale=%s&currency=%s&components=messages&enable-funding=paylater';

public function getSrcSdk() {
$url = sprintf( MM_Paypalpaylater_Helper_Data::SDK_SRC,
$this->getClientId(),
Mage::app()->getLocale()->getLocaleCode(),
Mage::app()->getStore()->getCurrentCurrencyCode()
);
$return = "<![CDATA[<script>";
$return.= "var PAYPAL_SCRIPT = '".$url."';";
$return.= "var sp = document.createElement('script');";
$return.= "sp.setAttribute('src', PAYPAL_SCRIPT);";
$return.= "sp.setAttribute('defer', true);";
$return.= "document.head.appendChild(sp);";
$return.= "</script>]]>";
return $return;

}

public function getConfig($path = null) {
return Mage::getStoreConfig('mm_paypalpaylater/'.$path);
Expand Down Expand Up @@ -33,4 +51,5 @@ public function getColor() {
public function getTextsize() {
return $this->getConfig('style/textsize');
}

}
58 changes: 38 additions & 20 deletions app/design/frontend/base/default/layout/mm/paypalpaylater.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
<?xml version="1.0"?>
<layout version="0.1.0">
<checkout_cart_index>
<reference name="checkout.cart.methods">
<block type="mm_paypalpaylater/messages" name="checkout.cart.methods.paypalpaylater" before="-" template="mm/paypalpaylater/messages.phtml">
<action method="setIsQuoteAllowed"><value>1</value></action>
</block>
</reference>
</checkout_cart_index>

<catalog_product_view>
<reference name="product.info.addtocart">
<block type="page/html_wrapper" name="product.info.addtocart.paypalpaylater.wrapper" translate="label">
<label>PayPal Pay Later Banner Wrapper</label>
<block type="mm_paypalpaylater/messages" name="product.info.addtocart.paypalpaylater" template="mm/paypalpaylater/messages.phtml">
<action method="setIsInCatalogProduct"><value>1</value></action>
</block>
</block>
</reference>
</catalog_product_view>
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
<reference name="head">
<block type="core/text" name="mm_paypalpaylater_sdk" after="-">
<action method="setText">
<text><![CDATA[
<link rel="preconnect" href="//www.paypal.com />
]]> </text>
</action>
</block>
</reference>
<reference name="before_body_end">
<block type="core/text" name="mm_paypalpaylater_sdk" after="-">
<action method="setText">
<text helper="mm_paypalpaylater/getSrcSdk" />
</action>
</block>
</reference>
</default>
<checkout_cart_index>
<reference name="checkout.cart.methods">
<block type="mm_paypalpaylater/messages" name="checkout.cart.methods.paypalpaylater" before="-" template="mm/paypalpaylater/messages.phtml">
<action method="setIsQuoteAllowed"><value>1</value></action>
</block>
</reference>
</checkout_cart_index>

<catalog_product_view>
<reference name="product.info.addtocart">
<block type="page/html_wrapper" name="product.info.addtocart.paypalpaylater.wrapper" translate="label">
<label>PayPal Pay Later Banner Wrapper</label>
<block type="mm_paypalpaylater/messages" name="product.info.addtocart.paypalpaylater" template="mm/paypalpaylater/messages.phtml">
<action method="setIsInCatalogProduct"><value>1</value></action>
</block>
</block>
</reference>
</catalog_product_view>
</layout>
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@
data-pp-placement="<?php echo $this->getIsInCatalogProduct() ? 'product' : 'cart'; ?>"
data-pp-amount="<?php echo $this->getAmount(); ?>"
style="margin: 18px 0">
</div>
<script src="<?php echo $this->getSrcSdk(); ?>" async></script>
</div>

0 comments on commit f5f4123

Please sign in to comment.