| 1 |
<?php namespace TierPricingTable\Addons\NonLoggedInUsers\CartRules; |
| 2 |
|
| 3 |
use TierPricingTable\TierPricingTablePlugin; |
| 4 |
use WP_Error; |
| 5 |
|
| 6 |
/** |
| 7 |
* Applies the cart rules on the storefront: minimum order per role in the cart and at checkout (classic |
| 8 |
* pages and blocks), payment methods and shipping rates limited to roles. |
| 9 |
*/ |
| 10 |
class CartRulesService { |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
add_action( 'woocommerce_check_cart_items', array( $this, 'checkMinimums' ) ); |
| 14 |
add_action( 'woocommerce_store_api_cart_errors', array( $this, 'addStoreApiErrors' ), 10, 2 ); |
| 15 |
add_filter( 'woocommerce_available_payment_gateways', array( $this, 'filterGateways' ) ); |
| 16 |
add_filter( 'woocommerce_package_rates', array( $this, 'filterRates' ), 10, 2 ); |
| 17 |
} |
| 18 |
|
| 19 |
protected function isGuest(): bool { |
| 20 |
return ! is_user_logged_in(); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* @return string[] |
| 25 |
*/ |
| 26 |
protected function getRoles(): array { |
| 27 |
return $this->isGuest() ? array() : array_values( array_map( 'strval', TierPricingTablePlugin::getCurrentUserRoles() ) ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* The admin edits orders with every method available; the rules are for the storefront. |
| 32 |
*/ |
| 33 |
protected function isStorefront(): bool { |
| 34 |
return ! is_admin() || wp_doing_ajax(); |
| 35 |
} |
| 36 |
|
| 37 |
/* --- minimum order --------------------------------------------------------------------------- */ |
| 38 |
|
| 39 |
/** |
| 40 |
* @return string[] error messages, empty when the cart passes |
| 41 |
*/ |
| 42 |
public function getMinimumErrors( $cart ): array { |
| 43 |
if ( ! $cart || $cart->is_empty() ) { |
| 44 |
return array(); |
| 45 |
} |
| 46 |
|
| 47 |
$picked = CartRules::pickMinimums( CartRulesSettings::getMinimums(), $this->getRoles(), $this->isGuest() ); |
| 48 |
|
| 49 |
if ( $picked['amount'] <= 0 && $picked['quantity'] <= 0 ) { |
| 50 |
return array(); |
| 51 |
} |
| 52 |
|
| 53 |
$subtotal = (float) $cart->get_subtotal(); |
| 54 |
|
| 55 |
if ( $cart->display_prices_including_tax() ) { |
| 56 |
$subtotal += (float) $cart->get_subtotal_tax(); |
| 57 |
} |
| 58 |
|
| 59 |
$count = (int) $cart->get_cart_contents_count(); |
| 60 |
$missed = CartRules::missedMinimums( $picked, $subtotal, $count ); |
| 61 |
$errors = array(); |
| 62 |
|
| 63 |
if ( isset( $missed['amount'] ) ) { |
| 64 |
$errors['amount'] = str_replace( |
| 65 |
array( '{minimum}', '{subtotal}' ), |
| 66 |
array( $this->plainPrice( $missed['amount'] ), $this->plainPrice( $subtotal ) ), |
| 67 |
CartRulesSettings::getAmountMessage() |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
if ( isset( $missed['quantity'] ) ) { |
| 72 |
$errors['quantity'] = str_replace( |
| 73 |
array( '{minimum}', '{count}' ), |
| 74 |
array( number_format_i18n( $missed['quantity'] ), number_format_i18n( $count ) ), |
| 75 |
CartRulesSettings::getQuantityMessage() |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
return $errors; |
| 80 |
} |
| 81 |
|
| 82 |
protected function plainPrice( float $amount ): string { |
| 83 |
return html_entity_decode( wp_strip_all_tags( wc_price( $amount ) ), ENT_QUOTES, 'UTF-8' ); |
| 84 |
} |
| 85 |
|
| 86 |
public function checkMinimums() { |
| 87 |
foreach ( $this->getMinimumErrors( WC()->cart ) as $message ) { |
| 88 |
if ( ! wc_has_notice( $message, 'error' ) ) { |
| 89 |
wc_add_notice( $message, 'error' ); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @param WP_Error $errors |
| 96 |
* @param \WC_Cart $cart |
| 97 |
*/ |
| 98 |
public function addStoreApiErrors( $errors, $cart ) { |
| 99 |
if ( ! $errors instanceof WP_Error ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
foreach ( $this->getMinimumErrors( $cart ) as $key => $message ) { |
| 104 |
$errors->add( 'tpt_minimum_' . $key, $message ); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/* --- payment and shipping methods ----------------------------------------------------------- */ |
| 109 |
|
| 110 |
public function filterGateways( $gateways ) { |
| 111 |
if ( ! is_array( $gateways ) || ! $this->isStorefront() ) { |
| 112 |
return $gateways; |
| 113 |
} |
| 114 |
|
| 115 |
foreach ( array_keys( $gateways ) as $id ) { |
| 116 |
if ( ! CartRules::isAllowed( CartRulesSettings::getGatewayRoles( (string) $id ), $this->getRoles(), $this->isGuest() ) ) { |
| 117 |
unset( $gateways[ $id ] ); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return $gateways; |
| 122 |
} |
| 123 |
|
| 124 |
public function filterRates( $rates, $package ) { |
| 125 |
if ( ! is_array( $rates ) || ! $this->isStorefront() ) { |
| 126 |
return $rates; |
| 127 |
} |
| 128 |
|
| 129 |
foreach ( $rates as $rateId => $rate ) { |
| 130 |
$instanceId = is_object( $rate ) && method_exists( $rate, 'get_instance_id' ) ? (int) $rate->get_instance_id() : 0; |
| 131 |
|
| 132 |
if ( $instanceId && ! CartRules::isAllowed( CartRulesSettings::getShippingRoles( $instanceId ), $this->getRoles(), $this->isGuest() ) ) { |
| 133 |
unset( $rates[ $rateId ] ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
return $rates; |
| 138 |
} |
| 139 |
} |
| 140 |
|