mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Merge pull request #500 from woocommerce/PCP-500-hide-smart-buttons-for-free-product
Check if price total is not zero before showing buttons
This commit is contained in:
commit
ca517e4f50
2 changed files with 69 additions and 24 deletions
|
@ -9,21 +9,51 @@ class SingleProductBootstap {
|
||||||
this.messages = messages;
|
this.messages = messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
|
||||||
|
handleChange() {
|
||||||
if (!this.shouldRender()) {
|
if (!this.shouldRender()) {
|
||||||
this.renderer.hideButtons(this.gateway.hosted_fields.wrapper);
|
this.renderer.hideButtons(this.gateway.hosted_fields.wrapper);
|
||||||
|
this.renderer.hideButtons(this.gateway.button.wrapper);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.render();
|
this.render();
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldRender() {
|
init() {
|
||||||
if (document.querySelector('form.cart') === null) {
|
|
||||||
return false;
|
document.querySelector('form.cart').addEventListener('change', this.handleChange.bind(this))
|
||||||
|
|
||||||
|
if (!this.shouldRender()) {
|
||||||
|
this.renderer.hideButtons(this.gateway.hosted_fields.wrapper);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
this.render();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
shouldRender() {
|
||||||
|
|
||||||
|
return document.querySelector('form.cart') !== null && !this.priceAmountIsZero();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
priceAmountIsZero() {
|
||||||
|
|
||||||
|
let priceText = "0";
|
||||||
|
if (document.querySelector('form.cart ins .woocommerce-Price-amount')) {
|
||||||
|
priceText = document.querySelector('form.cart ins .woocommerce-Price-amount').innerText;
|
||||||
|
}
|
||||||
|
else if (document.querySelector('form.cart .woocommerce-Price-amount')) {
|
||||||
|
priceText = document.querySelector('form.cart .woocommerce-Price-amount').innerText;
|
||||||
|
}
|
||||||
|
else if (document.querySelector('.woocommerce-Price-amount')) {
|
||||||
|
priceText = document.querySelector('.woocommerce-Price-amount').innerText;
|
||||||
|
}
|
||||||
|
const amount = parseFloat(priceText.replace(/([^\d,\.\s]*)/g, ''));
|
||||||
|
return amount === 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
@ -62,4 +92,4 @@ class SingleProductBootstap {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default SingleProductBootstap;
|
export default SingleProductBootstap;
|
||||||
|
|
|
@ -336,22 +336,6 @@ class SmartButton implements SmartButtonInterface {
|
||||||
*/
|
*/
|
||||||
private function render_button_wrapper_registrar(): bool {
|
private function render_button_wrapper_registrar(): bool {
|
||||||
|
|
||||||
$not_enabled_on_cart = $this->settings->has( 'button_cart_enabled' ) &&
|
|
||||||
! $this->settings->get( 'button_cart_enabled' );
|
|
||||||
if (
|
|
||||||
is_cart()
|
|
||||||
&& ! $not_enabled_on_cart
|
|
||||||
) {
|
|
||||||
add_action(
|
|
||||||
$this->proceed_to_checkout_button_renderer_hook(),
|
|
||||||
array(
|
|
||||||
$this,
|
|
||||||
'button_renderer',
|
|
||||||
),
|
|
||||||
20
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$not_enabled_on_product_page = $this->settings->has( 'button_single_product_enabled' ) &&
|
$not_enabled_on_product_page = $this->settings->has( 'button_single_product_enabled' ) &&
|
||||||
! $this->settings->get( 'button_single_product_enabled' );
|
! $this->settings->get( 'button_single_product_enabled' );
|
||||||
if (
|
if (
|
||||||
|
@ -368,6 +352,26 @@ class SmartButton implements SmartButtonInterface {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( $this->is_cart_price_total_zero() ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$not_enabled_on_cart = $this->settings->has( 'button_cart_enabled' ) &&
|
||||||
|
! $this->settings->get( 'button_cart_enabled' );
|
||||||
|
if (
|
||||||
|
is_cart()
|
||||||
|
&& ! $not_enabled_on_cart
|
||||||
|
) {
|
||||||
|
add_action(
|
||||||
|
$this->proceed_to_checkout_button_renderer_hook(),
|
||||||
|
array(
|
||||||
|
$this,
|
||||||
|
'button_renderer',
|
||||||
|
),
|
||||||
|
20
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
$not_enabled_on_minicart = $this->settings->has( 'button_mini_cart_enabled' ) &&
|
$not_enabled_on_minicart = $this->settings->has( 'button_mini_cart_enabled' ) &&
|
||||||
! $this->settings->get( 'button_mini_cart_enabled' );
|
! $this->settings->get( 'button_mini_cart_enabled' );
|
||||||
if (
|
if (
|
||||||
|
@ -449,6 +453,7 @@ class SmartButton implements SmartButtonInterface {
|
||||||
|| ! $product->is_in_stock()
|
|| ! $product->is_in_stock()
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -586,7 +591,7 @@ class SmartButton implements SmartButtonInterface {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
|
// phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
|
||||||
$label = 'checkout' === $this->context() ? apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) ) : __( 'Pay for order', 'woocommerce' );
|
$label = 'checkout' === $this->context() ? apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) ) : __( 'Pay for order', 'woocommerce' );
|
||||||
|
|
||||||
printf(
|
printf(
|
||||||
|
@ -1106,4 +1111,14 @@ class SmartButton implements SmartButtonInterface {
|
||||||
*/
|
*/
|
||||||
return (string) apply_filters( 'woocommerce_paypal_payments_single_product_renderer_hook', 'woocommerce_single_product_summary' );
|
return (string) apply_filters( 'woocommerce_paypal_payments_single_product_renderer_hook', 'woocommerce_single_product_summary' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if cart product price total is 0.
|
||||||
|
*
|
||||||
|
* @return bool true if is 0, otherwise false.
|
||||||
|
*/
|
||||||
|
protected function is_cart_price_total_zero(): bool {
|
||||||
|
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
|
||||||
|
return WC()->cart->get_cart_contents_total() == 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue