Skip to content

Commit

Permalink
Merge pull request #646 from woocommerce/refactor-filter
Browse files Browse the repository at this point in the history
Refactor product_supports_payment_request_button filter
  • Loading branch information
Dinamiko authored May 12, 2022
2 parents c7801bc + f7d0f60 commit c3887be
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
34 changes: 27 additions & 7 deletions modules/ppcp-button/src/Assets/SmartButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Exception;
use Psr\Log\LoggerInterface;
use WC_Product;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
Expand Down Expand Up @@ -524,9 +525,8 @@ public function button_renderer() {
$product = wc_get_product();

if (
! is_checkout() && is_a( $product, \WC_Product::class )
&& ( ! apply_filters( 'woocommerce_paypal_payments_product_supports_payment_request_button', ! $product->is_type( array( 'external', 'grouped' ) ) && $product->is_in_stock(), $product )
)
! is_checkout() && is_a( $product, WC_Product::class )
&& ! $this->product_supports_payment( $product )
) {

return;
Expand All @@ -550,9 +550,11 @@ public function message_renderer() {
$product = wc_get_product();

if (
! is_checkout() && is_a( $product, \WC_Product::class )
&& ( ! apply_filters( 'woocommerce_paypal_payments_product_supports_payment_request_button', ! $product->is_type( array( 'external', 'grouped' ) ) && $product->is_in_stock(), $product )
)
! is_checkout() && is_a( $product, WC_Product::class )
/**
* The filter returning true if PayPal buttons can be rendered, or false otherwise.
*/
&& ! $this->product_supports_payment( $product )
) {
return;
}
Expand All @@ -576,7 +578,7 @@ private function message_values(): array {
}
$placement = 'product';
$product = wc_get_product();
$amount = ( is_a( $product, \WC_Product::class ) ) ? wc_get_price_including_tax( $product ) : 0;
$amount = ( is_a( $product, WC_Product::class ) ) ? wc_get_price_including_tax( $product ) : 0;
$layout = $this->settings->has( 'message_product_layout' ) ?
$this->settings->get( 'message_product_layout' ) : 'text';
$logo_type = $this->settings->has( 'message_product_logo' ) ?
Expand Down Expand Up @@ -1235,6 +1237,24 @@ protected function is_cart_price_total_zero(): bool {
return WC()->cart->get_cart_contents_total() == 0;
}

/**
* Checks if PayPal buttons/messages can be rendered for the given product.
*
* @param WC_Product $product The product.
*
* @return bool
*/
protected function product_supports_payment( WC_Product $product ): bool {
/**
* The filter returning true if PayPal buttons/messages can be rendered for this product, or false otherwise.
*/
return apply_filters(
'woocommerce_paypal_payments_product_supports_payment_request_button',
! $product->is_type( array( 'external', 'grouped' ) ) && $product->is_in_stock(),
$product
);
}

/**
* Retrieves all payment tokens for the user, via API or cached if already queried.
*
Expand Down
6 changes: 6 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,11 @@
<MixedStringOffsetAssignment errorLevel="info"/>
<ParamNameMismatch errorLevel="info"/>
<RedundantCastGivenDocblockType errorLevel="info"/>

<TooManyArguments>
<errorLevel type="suppress">
<referencedFunction name="apply_filters" />
</errorLevel>
</TooManyArguments>
</issueHandlers>
</psalm>

0 comments on commit c3887be

Please sign in to comment.