| 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 |
* Price hash |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
private $variablePriceHash; |
| 24 |
|
| 25 |
/** |
| 26 |
* CatalogPriceManager constructor. |
| 27 |
*/ |
| 28 |
public function __construct() { |
| 29 |
if ( !$this->isEnabled() ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
if ( is_admin() && !defined( 'DOING_AJAX' ) ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public function formatPrice( ?string $defaultPriceHTML, ?WC_Product $product ) : ?string { |
| 38 |
if ( !$product ) { |
| 39 |
return $defaultPriceHTML; |
| 40 |
} |
| 41 |
// Some themes use ->get_price_html() to show cart item price. Do not modify product price if we're in the cart |
| 42 |
if ( is_cart() ) { |
| 43 |
return $defaultPriceHTML; |
| 44 |
} |
| 45 |
// Do not modify price in quick edit and inline edit in admin |
| 46 |
if ( isset( $_POST['action'] ) && in_array( $_POST['action'], ['inline-save'], true ) ) { |
| 47 |
return $defaultPriceHTML; |
| 48 |
} |
| 49 |
$currentProductPageProductId = get_queried_object_id(); |
| 50 |
$parentProductId = ( $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id() ); |
| 51 |
// Handle product page pricing |
| 52 |
if ( $currentProductPageProductId === $parentProductId ) { |
| 53 |
// Do not modify prices for variations on product page |
| 54 |
if ( TierPricingTablePlugin::isVariationProductSupported( $product ) && !apply_filters( |
| 55 |
'tiered_pricing_table/catalog_pricing/format_variation_price', |
| 56 |
false, |
| 57 |
$defaultPriceHTML, |
| 58 |
$product |
| 59 |
) ) { |
| 60 |
return $defaultPriceHTML; |
| 61 |
} |
| 62 |
$newPriceHTML = null; |
| 63 |
if ( 'same_as_catalog' === ProductPagePriceSubsection::getFormatPriceType() ) { |
| 64 |
// Format only if this is not a variable product or if it is enabled for variable products |
| 65 |
if ( !TierPricingTablePlugin::isVariableProductSupported( $product ) || $this->useForVariable() ) { |
| 66 |
$newPriceHTML = FormatPriceManager::getFormattedPrice( $product, array( |
| 67 |
'html' => true, |
| 68 |
'use_cache' => true, |
| 69 |
'for_display' => true, |
| 70 |
'with_suffix' => true, |
| 71 |
'with_lowest_prefix' => true, |
| 72 |
'with_default_price' => false, |
| 73 |
) ); |
| 74 |
} |
| 75 |
} |
| 76 |
} else { |
| 77 |
// Formation can be disabled for variable products |
| 78 |
if ( TierPricingTablePlugin::isVariableProductSupported( $product ) && !$this->useForVariable() ) { |
| 79 |
$newPriceHTML = null; |
| 80 |
} else { |
| 81 |
$newPriceHTML = FormatPriceManager::getFormattedPrice( $product, array( |
| 82 |
'html' => true, |
| 83 |
'use_cache' => true, |
| 84 |
'for_display' => true, |
| 85 |
'with_suffix' => true, |
| 86 |
'with_lowest_prefix' => true, |
| 87 |
'with_default_price' => false, |
| 88 |
) ); |
| 89 |
} |
| 90 |
} |
| 91 |
$newPriceHtml = ( is_null( $newPriceHTML ) ? $defaultPriceHTML : $newPriceHTML ); |
| 92 |
return apply_filters( |
| 93 |
'tiered_pricing_table/catalog_pricing/price_html', |
| 94 |
$newPriceHtml, |
| 95 |
$defaultPriceHTML, |
| 96 |
$product |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
public function isEnabled() : bool { |
| 101 |
return 'yes' === $this->getContainer()->getSettings()->get( 'tiered_price_at_catalog', 'yes' ); |
| 102 |
} |
| 103 |
|
| 104 |
public function useForVariable() : bool { |
| 105 |
return $this->getContainer()->getSettings()->get( 'tiered_price_at_catalog_for_variable', 'yes' ) === 'yes'; |
| 106 |
} |
| 107 |
|
| 108 |
public function getDisplayType() : string { |
| 109 |
return $this->getContainer()->getSettings()->get( 'tiered_price_at_catalog_type', 'range' ); |
| 110 |
} |
| 111 |
|
| 112 |
} |
| 113 |
|