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:
Emili Castells 2022-03-22 11:52:46 +01:00 committed by GitHub
commit ca517e4f50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 24 deletions

View file

@ -9,21 +9,51 @@ class SingleProductBootstap {
this.messages = messages;
}
init() {
handleChange() {
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;
}
this.render();
}
shouldRender() {
if (document.querySelector('form.cart') === null) {
return false;
init() {
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() {
@ -62,4 +92,4 @@ class SingleProductBootstap {
}
}
export default SingleProductBootstap;
export default SingleProductBootstap;

View file

@ -336,22 +336,6 @@ class SmartButton implements SmartButtonInterface {
*/
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' ) &&
! $this->settings->get( 'button_single_product_enabled' );
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' ) &&
! $this->settings->get( 'button_mini_cart_enabled' );
if (
@ -449,6 +453,7 @@ class SmartButton implements SmartButtonInterface {
|| ! $product->is_in_stock()
)
) {
return;
}
@ -586,7 +591,7 @@ class SmartButton implements SmartButtonInterface {
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' );
printf(
@ -1106,4 +1111,14 @@ class SmartButton implements SmartButtonInterface {
*/
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;
}
}