PluginProbe
Tiered Pricing Table for WooCommerce / 2.0
Tiered Pricing Table for WooCommerce v2.0
7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 All 90 releases
tier-pricing-table / src / Frontend / CatalogPriceManager.php

CatalogPriceManager.php in Tiered Pricing Table for WooCommerce 2.0, at src/Frontend/CatalogPriceManager.php

113 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Frontend;
2
3 use TierPricingTable\Settings\Settings;
4 use WC_Product;
5 use TierPricingTable\PriceManager;
6
7 /**
8 * Class CatalogPriceManager
9 *
10 * @package TierPricingTable
11 */
12 class CatalogPriceManager {
13
14 /**
15 * @var Settings
16 */
17 private $settings;
18
19 /**
20 * CatalogPriceManager constructor.
21 *
22 * @param Settings $settings
23 */
24 public function __construct( Settings $settings ) {
25 $this->settings = $settings;
26
27 if ( $this->isEnable() && ! is_admin() ) {
28 add_filter( 'woocommerce_get_price_html', [ $this, 'formatPrice' ], 999, 2 );
29 }
30 }
31
32 /**
33 * Change logic showing prince at catalog for product with tiered price rules
34 *
35 * @param string $priceHtml
36 * @param WC_Product $product
37 *
38 * @return string
39 */
40 public function formatPrice( $priceHtml, $product ) {
41 if ( ! is_product() ) {
42 if ( in_array( $product->get_type(), [ 'simple', 'variation' ] ) ) {
43 $rules = PriceManager::getPriceRules( $product->get_id() );
44
45 if ( ! empty( $rules ) ) {
46 $displayPriceType = $this->getDisplayType();
47
48 if ( $displayPriceType === 'range' ) {
49 return $this->getRange( $rules, $product );
50 } else {
51 return $this->getLowestPrice( $rules, $product );
52 }
53 }
54 }
55 }
56
57 return $priceHtml;
58 }
59
60 /**
61 * Get range from lowest to highest price from price rules
62 *
63 * @param array $rules
64 * @param WC_Product $product
65 *
66 * @return string
67 */
68 protected function getRange( $rules, $product ) {
69 if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) {
70
71 $lowest = wc_price( PriceManager::getPriceByPercentDiscount( $product->get_price(),
72 array_pop( $rules ) ) );
73
74 $highest = wc_price( $product->get_price() );
75
76 return $lowest . ' - ' . $highest;
77 }
78
79 return wc_price( array_pop( $rules ) ) . ' - ' . wc_price( $product->get_price() );
80 }
81
82 /**
83 * Get lowest price from price rules
84 *
85 * @param array $rules
86 * @param WC_Product $product
87 *
88 * @return string
89 */
90 protected function getLowestPrice( $rules, $product ) {
91 if ( PriceManager::getPricingType( $product->get_id() ) === 'percentage' ) {
92 $lowest = wc_price( PriceManager::getPriceByPercentDiscount( $product->get_price(),
93 array_shift( $rules ) ) );
94 } else {
95 $lowest = wc_price( array_pop( $rules ) );
96 }
97
98 return $this->getLowestPrefix() . ' ' . $lowest;
99 }
100
101 public function getLowestPrefix() {
102 return $this->settings->get( 'lowest_prefix', __( 'From', 'tier-pricing-table' ) );
103 }
104
105 public function isEnable() {
106 return $this->settings->get( 'tiered_price_at_catalog', 'yes' ) === 'yes';
107 }
108
109 public function getDisplayType() {
110 return $this->settings->get( 'tiered_price_at_catalog_type', 'range' );
111 }
112
113 }