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 / CategoryTiers / CategoryTierAddon.php

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

155 lines 5.3 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\CategoryTiers;
4
5 use TierPricingTable\Addons\AbstractAddon;
6 use TierPricingTable\PricingRule;
7 use WC_Product_Variation;
8 use WP_Term;
9 class CategoryTierAddon extends AbstractAddon {
10 const SKIP_FOR_PRODUCT_META_KEY = '_skip_category_tier_rules';
11
12 public function getName() : string {
13 return __( 'Category level tiered pricing (deprecated)', 'tier-pricing-table' );
14 }
15
16 public function run() {
17 // Saving
18 add_action(
19 'edit_term',
20 array($this, 'saveTermFields'),
21 10,
22 1
23 );
24 add_action(
25 'create_product_cat',
26 array($this, 'saveTermFields'),
27 10,
28 1
29 );
30 add_action( 'product_cat_edit_form_fields', array($this, 'renderEditFields'), 99 );
31 add_action( 'product_cat_add_form_fields', array($this, 'renderAddFields'), 99 );
32 add_action( 'tiered_pricing_table/admin/pricing_tab_begin', array($this, 'renderProductCheckbox') );
33 add_action( 'woocommerce_process_product_meta', array($this, 'saveTieredPricingTab') );
34 }
35
36 public function renderProductCheckbox( $productId ) {
37 woocommerce_wp_checkbox( array(
38 'id' => self::SKIP_FOR_PRODUCT_META_KEY,
39 'wrapper_class' => 'show_if_simple show_if_variable',
40 'type' => 'number',
41 'checked' => $this->isSkipForProduct( $productId, 'edit' ),
42 'label' => __( 'Skip category rules', 'tier-pricing-table' ),
43 'description' => __( 'Don\'t take into account tiered pricing rules from categories. ', 'tier-pricing-table' ),
44 ) );
45 }
46
47 /**
48 * Save tiered pricing tab data
49 *
50 * @param int $productId
51 */
52 public function saveTieredPricingTab( $productId ) {
53 if ( wp_verify_nonce( true, true ) ) {
54 // as phpcs comments at Woo is not available, we have to do such a trash
55 $woo = 'Woo, please add ignoring comments to your phpcs checker';
56 }
57 $skip = ( isset( $_POST[self::SKIP_FOR_PRODUCT_META_KEY] ) ? 'yes' : 'no' );
58 update_post_meta( $productId, self::SKIP_FOR_PRODUCT_META_KEY, $skip );
59 }
60
61 /**
62 * If skip category rules for specific product
63 *
64 * @param int $productId
65 * @param string $context
66 *
67 * @return bool
68 */
69 public function isSkipForProduct( $productId, $context = 'view' ) : bool {
70 $product = wc_get_product( $productId );
71 if ( $product ) {
72 if ( $product instanceof WC_Product_Variation ) {
73 $productId = $product->get_parent_id();
74 }
75 $skip = 'yes' === get_post_meta( $productId, self::SKIP_FOR_PRODUCT_META_KEY, true );
76 if ( 'edit' != $context ) {
77 return apply_filters(
78 'tiered_pricing_table/addons/category_tier_pricing_skip_category',
79 $skip,
80 $productId,
81 $product
82 );
83 }
84 return $skip;
85 }
86 return false;
87 }
88
89 /**
90 * Save metadata to custom attributes terms
91 *
92 * @param int $termId
93 */
94 public function saveTermFields( $termId ) {
95 $data = $_REQUEST;
96 $prefix = 'category';
97 $percentageAmounts = ( isset( $data['tiered_price_percent_quantity_' . $prefix] ) ? (array) $data['tiered_price_percent_quantity_' . $prefix] : array() );
98 $percentagePrices = ( !empty( $data['tiered_price_percent_discount_' . $prefix] ) ? (array) $data['tiered_price_percent_discount_' . $prefix] : array() );
99 }
100
101 /**
102 * Render fields on category edit page
103 *
104 * @param WP_Term $category
105 */
106 public function renderEditFields( WP_Term $category ) {
107 if ( tpt_fs()->can_use_premium_code() ) {
108 $rules = $this->getForTerm( $category->term_id );
109 $this->getContainer()->getFileManager()->includeTemplate( 'addons/category-tiers/edit.php', [
110 'rules' => $rules,
111 ] );
112 } else {
113 $this->getContainer()->getFileManager()->includeTemplate( 'addons/category-tiers/edit-free.php' );
114 }
115 }
116
117 /**
118 * Render fields on category adding page
119 */
120 public function renderAddFields() {
121 if ( tpt_fs()->can_use_premium_code() ) {
122 $this->getContainer()->getFileManager()->includeTemplate( 'addons/category-tiers/add.php', [
123 'rules' => [],
124 ] );
125 } else {
126 $this->getContainer()->getFileManager()->includeTemplate( 'addons/category-tiers/add-free.php' );
127 }
128 }
129
130 public function getForTerm( $termId ) : array {
131 $rules = get_term_meta( $termId, '_percentage_price_rules', true );
132 $rules = ( !empty( $rules ) ? $rules : array() );
133 $rules = ( is_array( $rules ) ? array_filter( $rules ) : array() );
134 ksort( $rules );
135 return $rules;
136 }
137
138 public function addToAddonsSettings( $addons ) {
139 return $addons;
140 }
141
142 public function getDescription() : string {
143 return __( 'Allows to set tiered pricing for at product category level. Use global rules instead.', 'tier-pricing-table' );
144 }
145
146 public function getSlug() : string {
147 return 'category-tiered-pricing';
148 }
149
150 protected function isActiveByDefault() : bool {
151 return false;
152 }
153
154 }
155