| 1 |
<?php namespace TierPricingTable\Services; |
| 2 |
|
| 3 |
/* |
| 4 |
* Class PricingService |
| 5 |
* |
| 6 |
* Service adjusts regular and sale price based on data in Pricing Rule. |
| 7 |
* Built-in addons like role-based and global rules can modify regular and sale prices. |
| 8 |
* |
| 9 |
* @package TierPricingTable\Services |
| 10 |
*/ |
| 11 |
|
| 12 |
use TierPricingTable\CalculationLogic; |
| 13 |
use TierPricingTable\Forms\Form; |
| 14 |
use TierPricingTable\PriceManager; |
| 15 |
use TierPricingTable\TierPricingTablePlugin; |
| 16 |
use WC_Product; |
| 17 |
|
| 18 |
class RegularPricingService { |
| 19 |
|
| 20 |
protected $cachedPrices = array( |
| 21 |
'regular' => array(), |
| 22 |
'sale' => array(), |
| 23 |
'price' => array(), |
| 24 |
); |
| 25 |
|
| 26 |
public function __construct() { |
| 27 |
|
| 28 |
// The service should be enabled by addons. This is useful to do not run those services when all pricing addons are disabled |
| 29 |
if ( ! apply_filters( 'tiered_pricing_table/services/pricing_service_enabled', false ) ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
add_filter( 'woocommerce_product_get_regular_price', array( |
| 34 |
$this, |
| 35 |
'adjustRegularPrice', |
| 36 |
), 99, 2 ); |
| 37 |
add_filter( 'woocommerce_product_get_sale_price', array( |
| 38 |
$this, |
| 39 |
'adjustSalePrice', |
| 40 |
), 99, 2 ); |
| 41 |
add_filter( 'woocommerce_product_get_price', array( $this, 'adjustPrice' ), 99, 2 ); |
| 42 |
|
| 43 |
// Variations |
| 44 |
add_filter( 'woocommerce_product_variation_get_regular_price', array( |
| 45 |
$this, |
| 46 |
'adjustRegularPrice', |
| 47 |
), 99, 2 ); |
| 48 |
add_filter( 'woocommerce_product_variation_get_sale_price', array( |
| 49 |
$this, |
| 50 |
'adjustSalePrice', |
| 51 |
), 99, 2 ); |
| 52 |
add_filter( 'woocommerce_product_variation_get_price', array( |
| 53 |
$this, |
| 54 |
'adjustPrice', |
| 55 |
), 99, 2 ); |
| 56 |
|
| 57 |
// Variable (price range) |
| 58 |
add_filter( 'woocommerce_variation_prices_price', array( $this, 'adjustPrice' ), 99, 3 ); |
| 59 |
// Variation |
| 60 |
add_filter( 'woocommerce_variation_prices_regular_price', array( |
| 61 |
$this, |
| 62 |
'adjustRegularPrice', |
| 63 |
), 99, 3 ); |
| 64 |
add_filter( 'woocommerce_variation_prices_sale_price', array( |
| 65 |
$this, |
| 66 |
'adjustSalePrice', |
| 67 |
), 99, 3 ); |
| 68 |
|
| 69 |
// Price caching |
| 70 |
add_filter( 'woocommerce_get_variation_prices_hash', function ( $hash, $product ) { |
| 71 |
|
| 72 |
$hash[] = md5( serialize( TierPricingTablePlugin::getCurrentUserRoles() ) ); |
| 73 |
|
| 74 |
return $hash; |
| 75 |
}, 99, 2 ); |
| 76 |
} |
| 77 |
|
| 78 |
protected function getPrice( ?WC_Product $product, $specific = false, $originalPrice = false ) { |
| 79 |
|
| 80 |
$newPrice = $this->_getPrice( $product, $specific, $originalPrice ); |
| 81 |
|
| 82 |
return apply_filters( 'tiered_pricing_table/services/regular_pricing/price', $newPrice, $product, $specific, |
| 83 |
$originalPrice ); |
| 84 |
} |
| 85 |
|
| 86 |
protected function _getPrice( ?WC_Product $product, $specific = false, $originalPrice = false ) { |
| 87 |
|
| 88 |
if ( ! $product ) { |
| 89 |
return null; |
| 90 |
} |
| 91 |
|
| 92 |
$pricingRule = PriceManager::getPricingRule( $product->get_id() ); |
| 93 |
|
| 94 |
$pricingType = $pricingRule->pricingData['pricing_type'] ?? null; |
| 95 |
$discount = $pricingRule->pricingData['discount'] ?? null; |
| 96 |
$discountType = $pricingRule->pricingData['discount_type'] ?? 'sale_price'; |
| 97 |
$salePrice = $pricingRule->pricingData['sale_price'] ?? null; |
| 98 |
$regularPrice = $pricingRule->pricingData['regular_price'] ?? null; |
| 99 |
|
| 100 |
if ( is_null( $discount ) && 'percentage' === $pricingType ) { |
| 101 |
return null; |
| 102 |
} |
| 103 |
|
| 104 |
if ( is_null( $salePrice ) && is_null( $regularPrice ) && 'flat' === $pricingType ) { |
| 105 |
return null; |
| 106 |
} |
| 107 |
|
| 108 |
if ( 'percentage' === $pricingType ) { |
| 109 |
|
| 110 |
// Do not modify the regular price if discount type is "sale_price". Adjust the sale price only. |
| 111 |
if ( 'sale_price' === $discountType && 'regular' === $specific ) { |
| 112 |
return null; |
| 113 |
} |
| 114 |
|
| 115 |
if ( CalculationLogic::calculateDiscountBasedOnRegularPrice() ) { |
| 116 |
$originalPrice = $product->get_regular_price( 'edit' ); |
| 117 |
} else { |
| 118 |
$originalPrice = $originalPrice ? $originalPrice : $product->get_price( 'edit' ); |
| 119 |
} |
| 120 |
|
| 121 |
// Calculate price based on percentage discount |
| 122 |
if ( $discount && $originalPrice > 0 ) { |
| 123 |
$discountedPrice = $originalPrice - ( ( $originalPrice / 100 ) * $discount ); |
| 124 |
|
| 125 |
if ( CalculationLogic::roundPrice() ) { |
| 126 |
$discountedPrice = round( $discountedPrice, max( 2, wc_get_price_decimals() ) ); |
| 127 |
} |
| 128 |
|
| 129 |
return $discountedPrice; |
| 130 |
} |
| 131 |
} else { |
| 132 |
if ( $specific ) { |
| 133 |
if ( 'sale' === $specific && ! Form::isEmpty( $salePrice ) ) { |
| 134 |
return $salePrice; |
| 135 |
} elseif ( 'regular' === $specific && ! Form::isEmpty( $regularPrice ) ) { |
| 136 |
return $regularPrice; |
| 137 |
} |
| 138 |
} else { |
| 139 |
if ( ! Form::isEmpty( $salePrice ) ) { |
| 140 |
return $salePrice; |
| 141 |
} elseif ( ! Form::isEmpty( $regularPrice ) ) { |
| 142 |
return $regularPrice; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
return null; |
| 148 |
} |
| 149 |
|
| 150 |
public function adjustPrice( $originalPrice, ?WC_Product $product ) { |
| 151 |
|
| 152 |
if ( ! $product ) { |
| 153 |
return $originalPrice; |
| 154 |
} |
| 155 |
|
| 156 |
if ( ! TierPricingTablePlugin::isSimpleProductSupported( $product ) ) { |
| 157 |
return $originalPrice; |
| 158 |
} |
| 159 |
|
| 160 |
if ( $product->get_meta( 'tiered_pricing_cart_price_calculated' ) === 'yes' ) { |
| 161 |
return $originalPrice; |
| 162 |
} |
| 163 |
|
| 164 |
if ( ! $originalPrice && ! apply_filters( 'tiered_pricing_table/services/pricing/override_zero_prices', |
| 165 |
true ) ) { |
| 166 |
return $originalPrice; |
| 167 |
} |
| 168 |
|
| 169 |
$newPrice = $this->getPrice( $product, null, $originalPrice ); |
| 170 |
|
| 171 |
return ! is_null( $newPrice ) ? $newPrice : $originalPrice; |
| 172 |
} |
| 173 |
|
| 174 |
public function adjustSalePrice( $price, ?WC_Product $product ) { |
| 175 |
|
| 176 |
if ( ! $product ) { |
| 177 |
return $price; |
| 178 |
} |
| 179 |
|
| 180 |
$newPrice = $this->getPrice( $product, 'sale' ); |
| 181 |
|
| 182 |
return ! is_null( $newPrice ) ? (float) $newPrice : $price; |
| 183 |
} |
| 184 |
|
| 185 |
public function adjustRegularPrice( $price, ?WC_Product $product ) { |
| 186 |
|
| 187 |
if ( ! $product ) { |
| 188 |
return $price; |
| 189 |
} |
| 190 |
|
| 191 |
$newPrice = $this->getPrice( $product, 'regular' ); |
| 192 |
|
| 193 |
return ! is_null( $newPrice ) ? (float) $newPrice : $price; |
| 194 |
} |
| 195 |
} |
| 196 |
|