diff --git a/modules/ppcp-applepay/resources/js/ApplepayButton.js b/modules/ppcp-applepay/resources/js/ApplepayButton.js index 8abce812b..08addabfa 100644 --- a/modules/ppcp-applepay/resources/js/ApplepayButton.js +++ b/modules/ppcp-applepay/resources/js/ApplepayButton.js @@ -46,10 +46,20 @@ class ApplepayButton { return; } this.addButton(); - document.querySelector('#btn-appl').addEventListener('click', (evt) => { - evt.preventDefault(); - this.onButtonClick(); - }); + const id_minicart = "#apple-" + this.buttonConfig.button.mini_cart_wrapper; + const id = "#apple-" + this.buttonConfig.button.wrapper; + + if(this.context === 'mini-cart') { + document.querySelector(id_minicart).addEventListener('click', (evt) => { + evt.preventDefault(); + this.onButtonClick(); + }); + } else { + document.querySelector(id).addEventListener('click', (evt) => { + evt.preventDefault(); + this.onButtonClick(); + }); + } }); } console.log('[ApplePayButton] init done', this.buttonConfig.ajax_url); @@ -119,24 +129,29 @@ class ApplepayButton { * Add a Apple Pay purchase button */ addButton() { - const appleContainer = document.getElementById("applepay-container"); - const type = this.buttonConfig.button.type; - const language = this.buttonConfig.button.lang; - const color = this.buttonConfig.button.color; - appleContainer.innerHTML = ``; - + console.log('[GooglePayButton] addButton', this.context); const wrapper = (this.context === 'mini-cart') ? this.buttonConfig.button.mini_cart_wrapper : this.buttonConfig.button.wrapper; - const shape = + /*const shape = (this.context === 'mini-cart') ? this.ppcpConfig.button.mini_cart_style.shape - : this.ppcpConfig.button.style.shape; + : this.ppcpConfig.button.style.shape;*/ + const appleContainer = this.context === 'mini-cart' ? document.getElementById("applepay-container-minicart") : document.getElementById("applepay-container"); + console.log('[ApplePayButton] addButton', appleContainer) + const type = this.buttonConfig.button.type; + const language = this.buttonConfig.button.lang; + const color = this.buttonConfig.button.color; + const id = "apple-" + wrapper; + appleContainer.innerHTML = ``; - jQuery(wrapper).addClass('ppcp-button-' + shape); + + + //jQuery(wrapper).addClass('ppcp-button-' + shape); jQuery(wrapper).append(appleContainer); + console.log('[ApplePayButton] addButton', wrapper, appleContainer); } //------------------------ @@ -302,6 +317,7 @@ class ApplepayButton { case 'checkout': case 'cart-block': case 'checkout-block': + case 'mini-cart': return { action: 'ppcp_update_shipping_contact', simplified_contact: event.shippingContact, @@ -327,6 +343,7 @@ class ApplepayButton { case 'checkout': case 'cart-block': case 'checkout-block': + case 'mini-cart': return { action: 'ppcp_update_shipping_method', shipping_method: event.shippingMethod, diff --git a/modules/ppcp-applepay/resources/js/boot.js b/modules/ppcp-applepay/resources/js/boot.js index e1930e154..e7185a8d1 100644 --- a/modules/ppcp-applepay/resources/js/boot.js +++ b/modules/ppcp-applepay/resources/js/boot.js @@ -23,10 +23,11 @@ import ApplepayManager from "./ApplepayManager"; console.error('PayPal button could not be configured.'); return; } - console.log(buttonConfig.button.wrapper) - console.log(jQuery(buttonConfig.button.wrapper).length) + const isMiniCart = ppcpConfig.mini_cart_buttons_enabled; + const isButton = jQuery(buttonConfig.button.cart_wrapper).length > 0; // If button wrapper is not present then there is no need to load the scripts. - if (!jQuery(buttonConfig.button.wrapper).length) { + // minicart loads later? + if (!isMiniCart && !isButton) { return; } diff --git a/modules/ppcp-applepay/src/Assets/ApplePayButton.php b/modules/ppcp-applepay/src/Assets/ApplePayButton.php index a4b58f584..8c093b532 100644 --- a/modules/ppcp-applepay/src/Assets/ApplePayButton.php +++ b/modules/ppcp-applepay/src/Assets/ApplePayButton.php @@ -597,10 +597,10 @@ class ApplePayButton implements ButtonInterface { /** * Add shipping methods to cart to perform correct calculations * - * @param WC_Cart $cart WC Cart instance. - * @param array $customer_address Customer address. - * @param array|null $shipping_method Shipping method. - * @param string $shipping_method_id Shipping method id. + * @param WC_Cart $cart WC Cart instance. + * @param array $customer_address Customer address. + * @param array|null $shipping_method Shipping method. + * @param string $shipping_method_id Shipping method id. */ protected function cart_shipping_methods( $cart, @@ -728,13 +728,14 @@ class ApplePayButton implements ButtonInterface { } if ( $cart->needs_shipping() ) { + $shipping_method_id = $shipping_method['identifier'] ?? ''; list( $shipping_methods_array, $selected_shipping_method ) = $this->cart_shipping_methods( $cart, $customer_address, $shipping_method, - $shipping_method['identifier'] + $shipping_method_id ); } $cart->calculate_shipping(); @@ -967,8 +968,6 @@ class ApplePayButton implements ButtonInterface { ); } - /* - TODO if ( $button_enabled_minicart ) { $default_hook_name = 'woocommerce_paypal_payments_minicart_button_render'; $render_placeholder = apply_filters( 'woocommerce_paypal_payments_googlepay_minicart_button_render_hook', $default_hook_name ); @@ -976,12 +975,11 @@ class ApplePayButton implements ButtonInterface { add_action( $render_placeholder, function () { - echo ''; + echo ''; }, 21 ); } - */ return true; } @@ -990,10 +988,8 @@ class ApplePayButton implements ButtonInterface { */ protected function applepay_button(): void { ?> -
-
- -
+
+
settings->has( 'applepay_button_enabled' ) && $this->settings->get( 'applepay_button_enabled' ); } catch ( Exception $e ) { return false; diff --git a/modules/ppcp-applepay/src/Assets/DataToAppleButtonScripts.php b/modules/ppcp-applepay/src/Assets/DataToAppleButtonScripts.php index c8626e2a1..f25f312c3 100644 --- a/modules/ppcp-applepay/src/Assets/DataToAppleButtonScripts.php +++ b/modules/ppcp-applepay/src/Assets/DataToAppleButtonScripts.php @@ -129,7 +129,7 @@ class DataToAppleButtonScripts { 'sdk_url' => $this->sdk_url, 'button' => array( 'wrapper' => '#applepay-container', - 'mini_cart_wrapper' => '#applepay-container', + 'mini_cart_wrapper' => '#applepay-container-minicart', 'type' => $type, 'color' => $color, 'lang' => $lang, @@ -174,8 +174,8 @@ class DataToAppleButtonScripts { return array( 'sdk_url' => $this->sdk_url, 'button' => array( - 'wrapper' => '#applepay-container', - 'mini_cart_wrapper' => '#applepay-container', + 'wrapper' => 'applepay-container', + 'mini_cart_wrapper' => 'applepay-container-minicart', ), 'product' => array( 'needShipping' => $cart->needs_shipping(),