PluginProbe
Tiered Pricing Table for WooCommerce / 7.1.1
Tiered Pricing Table for WooCommerce v7.1.1
8.0.2 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 All 91 releases
tier-pricing-table / src / Addons / CatalogPrices / CatalogPricesService.php

CatalogPricesService.php in Tiered Pricing Table for WooCommerce 7.1.1, at src/Addons/CatalogPrices/CatalogPricesService.php

109 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TierPricingTable\Addons\CatalogPrices;
4
5 use TierPricingTable\Core\ServiceContainerTrait;
6 use TierPricingTable\Managers\FormatPriceManager;
7 use TierPricingTable\Settings\Sections\GeneralSection\Subsections\ProductPagePriceSubsection;
8 use TierPricingTable\TierPricingTablePlugin;
9 use WC_Product;
10 use WC_Product_Variable;
11 /**
12 * Class CatalogPricesService
13 *
14 * @package TierPricingTable\Addons\CatalogPrices
15 */
16 class CatalogPricesService {
17 use ServiceContainerTrait;
18 /**
19 * CatalogPriceManager constructor.
20 */
21 public function __construct() {
22 if ( !$this->isEnabled() ) {
23 return;
24 }
25 if ( is_admin() && !defined( 'DOING_AJAX' ) ) {
26 return;
27 }
28 }
29
30 public function formatPrice( ?string $defaultPriceHTML, ?WC_Product $product ) : ?string {
31 if ( !$product ) {
32 return $defaultPriceHTML;
33 }
34 // Some themes use ->get_price_html() to show cart item price. Do not modify product price if we're in the cart
35 if ( is_cart() ) {
36 return $defaultPriceHTML;
37 }
38 // Do not modify price in quick edit and inline edit in admin
39 if ( isset( $_POST['action'] ) && in_array( $_POST['action'], ['inline-save'], true ) ) {
40 return $defaultPriceHTML;
41 }
42 if ( isset( $GLOBALS['TPT_DISABLE_CATALOG_PRICE_FORMAT'] ) ) {
43 return $defaultPriceHTML;
44 }
45 $currentProductPageProductId = get_queried_object_id();
46 $parentProductId = ( $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id() );
47 // Handle product page pricing
48 if ( $currentProductPageProductId === $parentProductId ) {
49 // Do not modify prices for variations on product page
50 if ( TierPricingTablePlugin::isVariationProductSupported( $product ) && !apply_filters(
51 'tiered_pricing_table/catalog_pricing/format_variation_price',
52 false,
53 $defaultPriceHTML,
54 $product
55 ) ) {
56 return $defaultPriceHTML;
57 }
58 $newPriceHTML = null;
59 if ( 'same_as_catalog' === ProductPagePriceSubsection::getFormatPriceType() ) {
60 // Format only if this is not a variable product or if it is enabled for variable products
61 if ( !TierPricingTablePlugin::isVariableProductSupported( $product ) || $this->useForVariable() ) {
62 $newPriceHTML = FormatPriceManager::getFormattedPrice( $product, array(
63 'html' => true,
64 'use_cache' => true,
65 'for_display' => true,
66 'with_suffix' => true,
67 'with_lowest_prefix' => true,
68 'with_default_price' => false,
69 ) );
70 }
71 }
72 } else {
73 // Formation can be disabled for variable products
74 if ( TierPricingTablePlugin::isVariableProductSupported( $product ) && !$this->useForVariable() ) {
75 $newPriceHTML = null;
76 } else {
77 $newPriceHTML = FormatPriceManager::getFormattedPrice( $product, array(
78 'html' => true,
79 'use_cache' => true,
80 'for_display' => true,
81 'with_suffix' => true,
82 'with_lowest_prefix' => true,
83 'with_default_price' => false,
84 ) );
85 }
86 }
87 $newPriceHtml = ( is_null( $newPriceHTML ) ? $defaultPriceHTML : $newPriceHTML );
88 return apply_filters(
89 'tiered_pricing_table/catalog_pricing/price_html',
90 $newPriceHtml,
91 $defaultPriceHTML,
92 $product
93 );
94 }
95
96 public function isEnabled() : bool {
97 return 'yes' === $this->getContainer()->getSettings()->get( 'tiered_price_at_catalog', 'yes' );
98 }
99
100 public function useForVariable() : bool {
101 return $this->getContainer()->getSettings()->get( 'tiered_price_at_catalog_for_variable', 'yes' ) === 'yes';
102 }
103
104 public function getDisplayType() : string {
105 return $this->getContainer()->getSettings()->get( 'tiered_price_at_catalog_type', 'range' );
106 }
107
108 }
109