PluginProbe
Tiered Pricing Table for WooCommerce / trunk
Tiered Pricing Table for WooCommerce vtrunk
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 / Addons / YouSave / YouSaveService.php

YouSaveService.php in Tiered Pricing Table for WooCommerce trunk, at src/Addons/YouSave/YouSaveService.php

115 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\YouSave;
2
3 use TierPricingTable\Core\ServiceContainerTrait;
4 use TierPricingTable\Settings\Settings;
5 use WC_Product;
6
7 /**
8 * Class YouSaveService
9 *
10 * Shows saving amount
11 *
12 * @package TierPricingTable\Addons\YouSave
13 */
14 class YouSaveService {
15
16 use ServiceContainerTrait;
17
18 /**
19 * CatalogPriceManager constructor.
20 */
21 public function __construct() {
22
23 add_action( 'woocommerce_get_price_html', array( $this, 'addYouSave' ), 150, 2 );
24
25 add_shortcode( 'tiered_price_you_save', function ( $tag, $args ) {
26
27 $args = wp_parse_args( $args, array(
28 'product_id' => null,
29 'color' => $this->getTextColor(),
30 'template' => $this->getTemplate(),
31 'consider_sale_price' => $this->considerSalePrice(),
32 ) );
33
34 $product = wc_get_product( $args['product_id'] );
35
36 if ( ! $product ) {
37 return '';
38 }
39
40 $hasRules = \TierPricingTable\PricingTable::getInstance()->productHasPricingRules( $product );
41 $isEnabled = $hasRules
42 ? 'yes' === $this->getContainer()->getSettings()->get( 'you_save_enabled', 'yes' )
43 : 'yes' === $this->getContainer()->getSettings()->get( 'you_save_non_tiered', 'no' ) && 'yes' === $this->getContainer()->getSettings()->get( 'you_save_consider_sale_price', 'yes' );
44
45 if ( ! $isEnabled ) {
46 return '';
47 }
48
49 return $this->getYouSaveHTML( $args['color'], $args['template'], $args['consider_sale_price'], $product );
50 } );
51 }
52
53 public function addYouSave( $priceHTML, WC_Product $product ) {
54
55 if ( false === strpos( $priceHTML, 'tiered-pricing-dynamic-price-wrapper' ) ) {
56 return $priceHTML;
57 }
58
59 $hasRules = \TierPricingTable\PricingTable::getInstance()->productHasPricingRules( $product );
60 $isEnabled = $hasRules
61 ? 'yes' === $this->getContainer()->getSettings()->get( 'you_save_enabled', 'yes' )
62 : 'yes' === $this->getContainer()->getSettings()->get( 'you_save_non_tiered', 'no' ) && 'yes' === $this->getContainer()->getSettings()->get( 'you_save_consider_sale_price', 'yes' );
63
64 if ( ! $isEnabled ) {
65 return $priceHTML;
66 }
67
68 $priceHTML .= '<br>' . $this->getYouSaveHTML( $this->getTextColor(), $this->getTemplate(),
69 $this->considerSalePrice(), $product );
70
71 return $priceHTML;
72 }
73
74 public function getYouSaveHTML( $color, $template, $considerSalePrice, $product = null ): string {
75 $template = $this->parseTemplate( $template );
76
77 ob_start();
78 ?>
79 <small data-consider-sale-price="<?php echo esc_attr( wc_bool_to_string( $considerSalePrice ) ); ?>"
80 data-product-id="<?php echo esc_attr( $product->get_id() ); ?>"
81 data-parent-id="<?php echo esc_attr( $product->get_parent_id() ? $product->get_parent_id() : $product->get_id() ); ?>"
82 class="tiered-pricing-you-save tiered-pricing-you-save--hidden"
83 style="color: <?php echo esc_attr( $color ); ?>"><?php echo wp_kses_post( $template ); ?>
84 </small>
85 <?php
86 return ob_get_clean();
87 }
88
89 public function getTemplate() {
90 return get_option( Settings::SETTINGS_PREFIX . 'you_save_template',
91 'You save {tp_ys_total_price} ({tp_ys_percentage_discount}%)' );
92 }
93
94 public function getTextColor() {
95 return get_option( Settings::SETTINGS_PREFIX . 'you_save_text_color', '#FF0000' );
96 }
97
98 public function considerSalePrice(): bool {
99 return get_option( Settings::SETTINGS_PREFIX . 'you_save_consider_sale_price', 'yes' ) === 'yes';
100 }
101
102 public function isEnabled(): bool {
103 return get_option( Settings::SETTINGS_PREFIX . 'you_save_enabled', 'yes' ) === 'yes';
104 }
105
106 protected function parseTemplate( $template ): string {
107 return strtr( $template, array(
108 '{tp_ys_price}' => '<span class="tiered-pricing-you-save__price"></span>',
109 '{tp_ys_total_price}' => '<span class="tiered-pricing-you-save__total"></span>',
110 '{tp_ys_percentage_discount}' => '<span class="tiered-pricing-you-save__discount"></span>',
111 ) );
112 }
113
114 }
115