mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Add minicart button
This commit is contained in:
parent
6949064cdb
commit
0a7bd32f80
4 changed files with 47 additions and 33 deletions
|
@ -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 = `<apple-pay-button id="btn-appl" buttonstyle="${color}" type="${type}" locale="${language}">`;
|
||||
|
||||
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 = `<apple-pay-button id="${id}" buttonstyle="${color}" type="${type}" locale="${language}">`;
|
||||
|
||||
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,
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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 '<span id="applepay-container" class="ppcp-button-applepay ppcp-button-minicart"></span>';
|
||||
echo '<span id="applepay-container-minicart" class="ppcp-button-applepay ppcp-button-minicart"></span>';
|
||||
},
|
||||
21
|
||||
);
|
||||
}
|
||||
*/
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -990,10 +988,8 @@ class ApplePayButton implements ButtonInterface {
|
|||
*/
|
||||
protected function applepay_button(): void {
|
||||
?>
|
||||
<div class="ppc-button-wrapper">
|
||||
<div id="applepay-container">
|
||||
<?php wp_nonce_field( 'woocommerce-process_checkout', 'woocommerce-process-checkout-nonce' ); ?>
|
||||
</div>
|
||||
<div id="applepay-container">
|
||||
<?php wp_nonce_field( 'woocommerce-process_checkout', 'woocommerce-process-checkout-nonce' ); ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
@ -1053,7 +1049,7 @@ class ApplePayButton implements ButtonInterface {
|
|||
*/
|
||||
public function is_enabled(): bool {
|
||||
try {
|
||||
//todo add also onboarded apple and enabled buttons
|
||||
// todo add also onboarded apple and enabled buttons.
|
||||
return $this->settings->has( 'applepay_button_enabled' ) && $this->settings->get( 'applepay_button_enabled' );
|
||||
} catch ( Exception $e ) {
|
||||
return false;
|
||||
|
|
|
@ -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(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue