Add product validation for pui

This commit is contained in:
dinamiko 2022-11-03 12:03:03 +01:00
parent 7b76a9f653
commit 5fa1907d97

View file

@ -10,6 +10,7 @@ declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Helper;
use WC_Order;
use WC_Order_Item_Product;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
@ -59,6 +60,10 @@ class PayUponInvoiceHelper {
return false;
}
if ( ! $this->is_valid_product() ) {
return false;
}
if ( ! $this->is_valid_currency() ) {
return false;
}
@ -70,6 +75,54 @@ class PayUponInvoiceHelper {
return true;
}
/**
* Checks whether PUI gateway is enabled.
*
* @return bool True if PUI gateway is enabled, otherwise false.
*/
public function is_pui_gateway_enabled(): bool {
$gateway_settings = get_option( 'woocommerce_ppcp-pay-upon-invoice-gateway_settings' );
return isset( $gateway_settings['enabled'] ) && $gateway_settings['enabled'] === 'yes' && 'DE' === $this->api_shop_country;
}
/**
* Checks if product is valid for PUI.
*
* @return bool
*/
private function is_valid_product(): bool {
$cart = WC()->cart ?? null;
if ( $cart && ! is_checkout_pay_page() ) {
$items = $cart->get_cart_contents();
foreach ( $items as $item ) {
$product = wc_get_product( $item['product_id'] );
if ( ! $this->checkout_helper->is_physical_product( $product ) ) {
return false;
}
}
}
if ( is_wc_endpoint_url( 'order-pay' ) ) {
global $wp;
if ( isset( $wp->query_vars['order-pay'] ) && absint( $wp->query_vars['order-pay'] ) > 0 ) {
$order_id = absint( $wp->query_vars['order-pay'] );
$order = wc_get_order( $order_id );
if ( is_a( $order, WC_Order::class ) ) {
foreach ( $order->get_items() as $item_id => $item ) {
if ( is_a( $item, WC_Order_Item_Product::class ) ) {
$product = wc_get_product( $item->get_product_id() );
if ( ! $this->checkout_helper->is_physical_product( $product ) ) {
return false;
}
}
}
}
}
}
return true;
}
/**
* Checks if currency is allowed for PUI.
*
@ -89,14 +142,4 @@ class PayUponInvoiceHelper {
return false;
}
/**
* Checks whether PUI gateway is enabled.
*
* @return bool True if PUI gateway is enabled, otherwise false.
*/
public function is_pui_gateway_enabled(): bool {
$gateway_settings = get_option( 'woocommerce_ppcp-pay-upon-invoice-gateway_settings' );
return isset( $gateway_settings['enabled'] ) && $gateway_settings['enabled'] === 'yes' && 'DE' === $this->api_shop_country;
}
}