PluginProbe
Tiered Pricing Table for WooCommerce / 6.5.0
Tiered Pricing Table for WooCommerce v6.5.0
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 / YouSave / YouSaveService.php

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

99 lines 3.0 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 if ( $this->isEnabled() ) {
24 add_action( 'woocommerce_get_price_html', array( $this, 'addYouSave' ), 150, 2 );
25 }
26
27 add_shortcode( 'tiered_price_you_save', function ( $tag, $args ) {
28
29 $args = wp_parse_args( $args, array(
30 'product_id' => null,
31 'color' => $this->getTextColor(),
32 'template' => $this->getTemplate(),
33 'consider_sale_price' => $this->considerSalePrice(),
34 ) );
35
36 $product = wc_get_product( $args['product_id'] );
37
38 if ( ! $product ) {
39 return '';
40 }
41
42 return $this->getYouSaveHTML( $args['color'], $args['template'], $args['consider_sale_price'], $product );
43 } );
44 }
45
46 public function addYouSave( $priceHTML, WC_Product $product ) {
47
48 if ( false === strpos( $priceHTML, 'tiered-pricing-dynamic-price-wrapper' ) ) {
49 return $priceHTML;
50 }
51
52 $priceHTML .= '<br>' . $this->getYouSaveHTML( $this->getTextColor(), $this->getTemplate(),
53 $this->considerSalePrice(), $product );
54
55 return $priceHTML;
56 }
57
58 public function getYouSaveHTML( $color, $template, $considerSalePrice, $product = null ): string {
59 $template = $this->parseTemplate( $template );
60
61 ob_start();
62 ?>
63 <small data-consider-sale-price="<?php echo esc_attr( wc_bool_to_string( $considerSalePrice ) ); ?>"
64 data-product-id="<?php echo esc_attr( $product->get_id() ); ?>"
65 data-parent-id="<?php echo esc_attr( $product->get_parent_id() ? $product->get_parent_id() : $product->get_id() ); ?>"
66 class="tiered-pricing-you-save tiered-pricing-you-save--hidden"
67 style="color: <?php echo esc_attr( $color ); ?>"><?php echo wp_kses_post( $template ); ?>
68 </small>
69 <?php
70 return ob_get_clean();
71 }
72
73 public function getTemplate() {
74 return get_option( Settings::SETTINGS_PREFIX . 'you_save_template',
75 'You save {tp_ys_total_price} ({tp_ys_percentage_discount}%)' );
76 }
77
78 public function getTextColor() {
79 return get_option( Settings::SETTINGS_PREFIX . 'you_save_text_color', '#FF0000' );
80 }
81
82 public function considerSalePrice(): bool {
83 return get_option( Settings::SETTINGS_PREFIX . 'you_save_consider_sale_price', 'yes' ) === 'yes';
84 }
85
86 public function isEnabled(): bool {
87 return get_option( Settings::SETTINGS_PREFIX . 'you_save_enabled', 'yes' ) === 'yes';
88 }
89
90 protected function parseTemplate( $template ): string {
91 return strtr( $template, array(
92 '{tp_ys_price}' => '<span class="tiered-pricing-you-save__price"></span>',
93 '{tp_ys_total_price}' => '<span class="tiered-pricing-you-save__total"></span>',
94 '{tp_ys_percentage_discount}' => '<span class="tiered-pricing-you-save__discount"></span>',
95 ) );
96 }
97
98 }
99