PluginProbe
Tiered Pricing Table for WooCommerce / 6.1.0
Tiered Pricing Table for WooCommerce v6.1.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 / ProductCatalogLoop / ProductCatalogLoop.php

ProductCatalogLoop.php in Tiered Pricing Table for WooCommerce 6.1.0, at src/Addons/ProductCatalogLoop/ProductCatalogLoop.php

147 lines 6.6 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\ProductCatalogLoop;
4
5 use TierPricingTable\Addons\AbstractAddon;
6 use TierPricingTable\Addons\ProductCatalogLoop\Settings\ProductCatalogLoopSettingsSection;
7 use WC_Product;
8 class ProductCatalogLoop extends AbstractAddon {
9 public function getName() : string {
10 return __( 'Product Catalog Loop', 'tier-pricing-table' );
11 }
12
13 public function getDescription() : string {
14 return __( 'Display tiered pricing in the product catalog (shop page.)', 'tier-pricing-table' );
15 }
16
17 public function getSlug() : string {
18 return 'shop-loop-display';
19 }
20
21 public function getRenderAttributes() : array {
22 if ( !ProductCatalogLoopSettingsSection::isCustomLayoutSettings() ) {
23 return array(
24 'display' => true,
25 );
26 }
27 $args = array(
28 'display_context' => 'shop-loop',
29 'display' => true,
30 'display_type' => ProductCatalogLoopSettingsSection::getLayoutType(),
31 'title' => ProductCatalogLoopSettingsSection::getTitle(),
32 'quantity_type' => ProductCatalogLoopSettingsSection::getQuantityType(),
33 'getSelectedQuantityColor' => ProductCatalogLoopSettingsSection::getSelectedQuantityColor(),
34 'quantity_column_title' => ProductCatalogLoopSettingsSection::getTableColumnsTitles()['head_quantity_text'],
35 'price_column_title' => ProductCatalogLoopSettingsSection::getTableColumnsTitles()['head_price_text'],
36 'discount_column_title' => ProductCatalogLoopSettingsSection::getTableColumnsTitles()['head_discount_text'],
37 'show_discount_column' => ProductCatalogLoopSettingsSection::blocksShowDiscount(),
38 'clickable_rows' => ProductCatalogLoopSettingsSection::isClickableTableRows(),
39 'active_tier_color' => ProductCatalogLoopSettingsSection::getSelectedQuantityColor(),
40 'options_show_original_product_price' => ProductCatalogLoopSettingsSection::isShowOriginalProductPriceInOptions(),
41 'options_show_default_option' => ProductCatalogLoopSettingsSection::isShowDefaultOption(),
42 'options_option_text' => ProductCatalogLoopSettingsSection::getOptionText(),
43 'options_default_option_text' => ProductCatalogLoopSettingsSection::getDefaultOptionText(),
44 'plain_text_show_default_option' => ProductCatalogLoopSettingsSection::isShowFirstPlainTextTier(),
45 'plain_text_option_text' => ProductCatalogLoopSettingsSection::getPlainTextTemplate(),
46 'plain_text_default_option_text' => ProductCatalogLoopSettingsSection::getFirstTierPlainTextTemplate(),
47 );
48 $default_quantity_measurement = array(
49 'singular' => '',
50 'plural' => '',
51 );
52 $quantity_measurement = $default_quantity_measurement;
53 if ( in_array( $args['display_type'], array('table', 'horizontal-table', 'tooltip') ) ) {
54 $quantity_measurement = ProductCatalogLoopSettingsSection::getTableQuantityMeasurement();
55 }
56 if ( 'blocks' === $args['display_type'] ) {
57 $quantity_measurement = ProductCatalogLoopSettingsSection::getBlocksQuantityMeasurement();
58 }
59 $args['quantity_measurement_singular'] = $quantity_measurement['singular'];
60 $args['quantity_measurement_plural'] = $quantity_measurement['plural'];
61 return $args;
62 }
63
64 public function run() {
65 add_filter( 'tiered_pricing_table/settings/sections', array($this, 'addSettingSection') );
66 if ( !ProductCatalogLoopSettingsSection::isEnabled() ) {
67 return;
68 }
69 add_filter(
70 'tiered_pricing_table/frontend/variation_render_settings',
71 function ( $settings, $productId, $context ) {
72 if ( 'shop-loop' !== $context ) {
73 return $settings;
74 }
75 return $this->getRenderAttributes();
76 },
77 10,
78 3
79 );
80 add_filter(
81 'tiered_pricing_table/frontend/default_price_behaviour_type',
82 function (
83 $type,
84 $product,
85 $priceHTML,
86 $priceDisplayContext
87 ) {
88 if ( 'shop-loop' === $priceDisplayContext ) {
89 return ( ProductCatalogLoopSettingsSection::isDynamicPrice() ? 'dynamic' : 'static' );
90 }
91 return $type;
92 },
93 10,
94 4
95 );
96 add_action( 'init', function () {
97 $position = ProductCatalogLoopSettingsSection::getPosition();
98 $closeLink = false;
99 if ( 'woocommerce_shop_loop_item_title' === $position['hook'] ) {
100 $closeLink = true;
101 remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
102 }
103 add_action( $position['hook'], function () use($closeLink) {
104 global $product, $post;
105 if ( !$product instanceof WC_Product ) {
106 $productID = ( isset( $post ) ? $post->ID : null );
107 $product = wc_get_product( $productID );
108 }
109 if ( !$product ) {
110 return;
111 }
112 if ( !$product->is_purchasable() ) {
113 return;
114 }
115 if ( $closeLink ) {
116 echo '</a>';
117 }
118 $this->render( $product, ProductCatalogLoopSettingsSection::useReducedStyles() );
119 }, $position['priority'] );
120 } );
121 }
122
123 public function render( WC_product $product, $useReducedStyles = false ) {
124 $classes = 'tiered-pricing-shop-loop';
125 if ( $useReducedStyles ) {
126 $classes .= ' tiered-pricing-shop-loop--reduced';
127 }
128 $this->getContainer()->getFileManager()->includeTemplate( 'frontend/shop-loop.php', array(
129 'classes' => $classes,
130 'product' => $product,
131 'settings' => $this->getRenderAttributes(),
132 ) );
133 }
134
135 public function addSettingSection( $sections ) : array {
136 $newSections = [];
137 for ($i = 0; $i < count( $sections ); $i++) {
138 if ( 1 === $i ) {
139 $newSections[] = new ProductCatalogLoopSettingsSection();
140 }
141 $newSections[] = $sections[$i];
142 }
143 return $newSections;
144 }
145
146 }
147