PluginProbe
Tiered Pricing Table for WooCommerce / 8.0.2
Tiered Pricing Table for WooCommerce v8.0.2
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 / TierLabels / Frontend / DisplayManager.php

DisplayManager.php in Tiered Pricing Table for WooCommerce 8.0.2, at src/Addons/TierLabels/Frontend/DisplayManager.php

117 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\TierLabels\Frontend;
2
3 use TierPricingTable\Addons\TierLabels\TierLabel;
4 use TierPricingTable\Addons\TierLabels\TierLabelsManager;
5 use TierPricingTable\PricingRule;
6
7 class DisplayManager {
8
9 public function __construct() {
10 $this->hooks();
11 }
12
13 private function hooks() {
14
15 $hooks = array(
16 'options' => 'tiered_pricing_table/options/label',
17 'dropdown' => 'tiered_pricing_table/dropdown/label',
18 'horizontal-table' => 'tiered_pricing_table/horizontal-table/label',
19 'plain-text' => 'tiered_pricing_table/plain-text/label',
20 'blocks' => 'tiered_pricing_table/blocks/label',
21 'table' => 'tiered_pricing_table/table/label',
22 );
23
24 foreach ( $hooks as $layout => $hook ) {
25 add_action( $hook, function ( PricingRule $rule, $quantity, $args ) use ( $layout ) {
26 $this->render( $rule, $quantity, $args, $layout );
27 }, 10, 3 );
28 }
29 }
30
31 public function getLabelForQuantity( PricingRule $rule, $quantity ): ?TierLabel {
32
33 $labelsData = $rule->data['tier_labels'][ $rule->getType() ] ?? array();
34
35 if ( empty( $labelsData ) ) {
36 return null;
37 }
38
39 $amountKey = null;
40
41 if ( (int) $quantity === $rule->getMinimum( true ) ) {
42 $amountKey = 'first_row';
43 } else {
44 foreach ( $rule->getRules() as $tierQty => $price ) {
45
46 if ( $tierQty === $quantity ) {
47 $amountKey = $tierQty;
48 break;
49 }
50 }
51 }
52
53 if ( ! $amountKey || empty( $labelsData[ $amountKey ] ) ) {
54 return null;
55 }
56
57 return TierLabelsManager::getInstance()->getLabel( $labelsData[ $amountKey ] );
58 }
59
60 public function render( PricingRule $rule, $quantity, $args = array(), $layout = '' ) {
61
62 $label = $this->getLabelForQuantity( $rule, $quantity );
63
64 if ( ! $label ) {
65 return;
66 }
67
68 $args = wp_parse_args( $args, array(
69 'id' => '',
70 'style' => 'default',
71 ) );
72
73 $CSS = '';
74
75 // CSS Fixes
76 if ( 'blocks' === $layout ) {
77 // styles with a floating label above the block (5 and 7 are card styles like the default)
78 if ( in_array( $args['style'], array( 'default', '1', '2', '4', '5', '7' ) ) ) {
79 $CSS .= "#{$args['id']} { gap: 20px 10px }";
80 $CSS .= "#{$args['id']} .tiered-pricing-block { position: relative; }";
81 $CSS .= "#{$args['id']} .tiered-pricing-tier-label {margin: 0 !important;
82 position: absolute;
83 left: 50%;
84 top: 0;
85 transform: translate(-50%, -50%);
86 text-transform: uppercase;
87 z-index: 20;}";
88 }
89
90 if ( in_array( $args['style'], array( 'default', '2', '4', '5', '7' ) ) ) {
91 $CSS .= "#{$args['id']} .tiered-pricing-block:has(#" . $label->getId() . ") { padding-top: 12px; }";
92 }
93
94 if ( $args['style'] === '1' ) {
95 $CSS .= "#{$args['id']} .tiered-pricing-block:has(#" . $label->getId() . ") {overflow: unset !important;}";
96 $CSS .= "#{$args['id']} .tiered-pricing-block:has(#" . $label->getId() . ") .tiered-pricing-block__quantity {padding-top: 12px !important;}";
97 }
98 } elseif ( 'table' === $layout ) {
99 $CSS .= "#{$args['id']} tbody td:has(#" . $label->getId() . ") { display: flex; align-items: center; gap: 5px; }";
100 } elseif ( 'plain-text' === $layout ) {
101 // the "one line" style keeps its tiers inline
102 $display = '2' === (string) $args['style'] ? 'inline-flex' : 'flex';
103 $CSS .= "#{$args['id']} .tiered-pricing-plain-text:has(#" . $label->getId() . ") { display: {$display}; align-items: center; gap: 5px; }";
104 }
105
106 if ( $CSS ) {
107 ?>
108 <style>
109 <?php echo wp_kses_post( $CSS ); ?>
110 </style>
111 <?php
112 }
113
114 // Output is escaped internally in TierLabel::render()
115 echo $label->render(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
116 }
117 }