| 1 |
<?php namespace TierPricingTable\Frontend; |
| 2 |
|
| 3 |
use TierPricingTable\Settings\Settings; |
| 4 |
use WC_Product; |
| 5 |
use TierPricingTable\PriceManager; |
| 6 |
use WC_Product_Variable; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class CatalogPriceManager |
| 10 |
* |
| 11 |
* @package TierPricingTable |
| 12 |
*/ |
| 13 |
class CatalogPriceManager { |
| 14 |
|
| 15 |
/** |
| 16 |
* @var Settings |
| 17 |
*/ |
| 18 |
private $settings; |
| 19 |
|
| 20 |
/** |
| 21 |
* CatalogPriceManager constructor. |
| 22 |
* |
| 23 |
* @param Settings $settings |
| 24 |
*/ |
| 25 |
public function __construct( Settings $settings ) { |
| 26 |
$this->settings = $settings; |
| 27 |
|
| 28 |
if ( $this->isEnable() && ! is_admin() ) { |
| 29 |
add_filter( 'woocommerce_get_price_html', [ $this, 'formatPrice' ], 999, 2 ); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Change logic showing prince at catalog for product with tiered price rules |
| 35 |
* |
| 36 |
* @param string $priceHtml |
| 37 |
* @param WC_Product $product |
| 38 |
* |
| 39 |
* @return string |
| 40 |
*/ |
| 41 |
public function formatPrice( $priceHtml, $product ) { |
| 42 |
|
| 43 |
$product_id = $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id(); |
| 44 |
|
| 45 |
$formatCondition = $this->settings->get( 'tiered_price_at_product_page', |
| 46 |
'no' ) === 'yes' || get_queried_object_id() != $product_id; |
| 47 |
|
| 48 |
if ( $formatCondition ) { |
| 49 |
$displayPriceType = $this->getDisplayType(); |
| 50 |
|
| 51 |
if ( $product instanceof WC_Product_Variable && $this->useForVariable() === 'yes' ) { |
| 52 |
$price = $this->formatPriceForVariableProduct( $product ); |
| 53 |
|
| 54 |
if ( $price ) { |
| 55 |
return $price; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if ( in_array( $product->get_type(), [ 'simple', 'variation' ] ) ) { |
| 60 |
$rules = PriceManager::getPriceRules( $product->get_id() ); |
| 61 |
|
| 62 |
if ( ! empty( $rules ) ) { |
| 63 |
if ( $displayPriceType === 'range' ) { |
| 64 |
return $this->getRange( $rules, $product ); |
| 65 |
} else { |
| 66 |
return $this->getLowestPrice( $rules, $product ); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
return $priceHtml; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Format price for variable product. Range uses lowest and high prices from all variations |
| 79 |
* |
| 80 |
* @param WC_Product_Variable $product |
| 81 |
* |
| 82 |
* @return bool|string |
| 83 |
*/ |
| 84 |
protected function formatPriceForVariableProduct( WC_Product_Variable $product ) { |
| 85 |
|
| 86 |
$prices = $product->get_variation_prices( true ); |
| 87 |
|
| 88 |
$maxPrice = end( $prices['price'] ); |
| 89 |
$minPrices = []; |
| 90 |
|
| 91 |
foreach ( $product->get_available_variations() as $variation ) { |
| 92 |
$rules = PriceManager::getPriceRules( $variation['variation_id'] ); |
| 93 |
if ( ! empty( $rules ) ) { |
| 94 |
|
| 95 |
$minPrices[] = $this->getLowestPrice( $rules, wc_get_product( $variation['variation_id'] ), false ); |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
if ( ! empty( $minPrices ) ) { |
| 102 |
if ( $this->getDisplayType() === 'range' ) { |
| 103 |
return wc_price( min( $minPrices ) ) . ' - ' . wc_price( $maxPrice ); |
| 104 |
} else { |
| 105 |
return $this->getLowestPrefix() . ' ' . wc_price( min( $minPrices ) ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get range from lowest to highest price from price rules |
| 114 |
* |
| 115 |
* @param array $rules |
| 116 |
* @param WC_Product $product |
| 117 |
* |
| 118 |
* @return string |
| 119 |
*/ |
| 120 |
protected function getRange( $rules, $product ) { |
| 121 |
|
| 122 |
$lowest = array_pop( $rules ); |
| 123 |
|
| 124 |
$highest_html = wc_price( PriceManager::getPriceWithTaxes( $product->get_price(), $product ) ); |
| 125 |
|
| 126 |
if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) { |
| 127 |
|
| 128 |
$price = PriceManager::getPriceByPercentDiscount( $product->get_price(), $lowest ); |
| 129 |
|
| 130 |
$lowest_html = wc_price( PriceManager::getPriceWithTaxes( $price, $product ) ); |
| 131 |
|
| 132 |
return $lowest_html . ' - ' . $highest_html; |
| 133 |
} |
| 134 |
|
| 135 |
$lowest_html = wc_price( PriceManager::getPriceWithTaxes( $lowest, $product ) ); |
| 136 |
|
| 137 |
return $lowest_html . ' - ' . $highest_html; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Get lowest price from price rules |
| 142 |
* |
| 143 |
* @param array $rules |
| 144 |
* @param WC_Product $product |
| 145 |
* |
| 146 |
* @param bool $html |
| 147 |
* |
| 148 |
* @return string|float |
| 149 |
*/ |
| 150 |
protected function getLowestPrice( $rules, $product, $html = true ) { |
| 151 |
if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) { |
| 152 |
$lowest = PriceManager::getPriceByPercentDiscount( $product->get_price(), |
| 153 |
array_pop( $rules ) ); |
| 154 |
} else { |
| 155 |
$lowest = array_pop( $rules ); |
| 156 |
} |
| 157 |
|
| 158 |
if ( ! $html ) { |
| 159 |
return PriceManager::getPriceWithTaxes( $lowest, $product, 'shop' ); |
| 160 |
} |
| 161 |
|
| 162 |
return $this->getLowestPrefix() . ' ' . wc_price( PriceManager::getPriceWithTaxes( $lowest, $product ) ); |
| 163 |
} |
| 164 |
|
| 165 |
public function getLowestPrefix() { |
| 166 |
return $this->settings->get( 'lowest_prefix', __( 'From', 'tier-pricing-table' ) ); |
| 167 |
} |
| 168 |
|
| 169 |
public function isEnable() { |
| 170 |
return $this->settings->get( 'tiered_price_at_catalog', 'yes' ) === 'yes'; |
| 171 |
} |
| 172 |
|
| 173 |
public function getDisplayType() { |
| 174 |
return $this->settings->get( 'tiered_price_at_catalog_type', 'range' ); |
| 175 |
} |
| 176 |
|
| 177 |
public function useForVariable() { |
| 178 |
return $this->settings->get( 'tiered_price_at_catalog_for_variable', 'yes' ); |
| 179 |
} |
| 180 |
|
| 181 |
} |