PluginProbe
Tiered Pricing Table for WooCommerce / 7.1.5
Tiered Pricing Table for WooCommerce v7.1.5
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 / RoleBasedPricing / ProductManager.php

ProductManager.php in Tiered Pricing Table for WooCommerce 7.1.5, at src/Addons/RoleBasedPricing/ProductManager.php

112 lines 4.1 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\RoleBasedPricing;
4
5 use Exception;
6 use TierPricingTable\Core\ServiceContainerTrait;
7 use TierPricingTable\Forms\MinimumOrderQuantityForm;
8 use TierPricingTable\Forms\RegularPricingForm;
9 use TierPricingTable\Forms\TieredPricingRulesForm;
10 use WP_Post;
11 class ProductManager {
12 use ServiceContainerTrait;
13 const GET_ROLE_ROW_HTML__ACTION = 'tpt_get_role_row_html';
14
15 public function __construct() {
16 // Get role row via AJAX
17 add_action( 'wp_ajax_' . self::GET_ROLE_ROW_HTML__ACTION, array($this, 'getRoleRowHtml') );
18 // Activate new tab
19 add_filter( 'tiered_pricing_table/admin/role_customer_pricing_tab_active', '__return_true' );
20 // Render
21 add_action(
22 'tiered_pricing_table/admin/role_customer_pricing_tab_content',
23 array($this, 'render'),
24 99,
25 1
26 );
27 add_action(
28 'woocommerce_variation_options_pricing',
29 function ( $loop, $variationData, WP_Post $variation ) {
30 $this->render( $variation->ID, $loop );
31 },
32 11,
33 3
34 );
35 // Save
36 add_action( 'woocommerce_process_product_meta', function ( $productId ) {
37 } );
38 add_action(
39 'woocommerce_save_product_variation',
40 function ( $variationId, $loop ) {
41 },
42 10,
43 3
44 );
45 }
46
47 /**
48 * Delete removed role-based rules
49 *
50 * @param int $productId
51 * @param null|int $loop
52 * @param array $data
53 */
54 public function handleRemoving( $productId, $loop, $data ) {
55 if ( !empty( $data['tiered_price_rules_roles_to_delete'] ) ) {
56 foreach ( $data['tiered_price_rules_roles_to_delete'] as $roleToRemove ) {
57 if ( !empty( $roleToRemove ) ) {
58 RoleBasedPriceManager::deleteAllDataForRole( $productId, $roleToRemove );
59 }
60 }
61 }
62 if ( !empty( $data['tiered_price_rules_roles_to_delete_variation'][$loop] ) ) {
63 foreach ( $data['tiered_price_rules_roles_to_delete_variation'][$loop] as $roleToRemove ) {
64 if ( !empty( $roleToRemove ) ) {
65 RoleBasedPriceManager::deleteAllDataForRole( $productId, $roleToRemove );
66 }
67 }
68 }
69 }
70
71 public function render( $productId, $loop = null ) {
72 $this->getContainer()->getFileManager()->includeTemplate( 'addons/role-based-pricing/role-based-block.php', array(
73 'product_id' => $productId,
74 'loop' => $loop,
75 ) );
76 }
77
78 /**
79 * AJAX Handler
80 */
81 public function getRoleRowHtml() {
82 $nonce = ( isset( $_GET['nonce'] ) ? sanitize_text_field( $_GET['nonce'] ) : false );
83 if ( wp_verify_nonce( $nonce, self::GET_ROLE_ROW_HTML__ACTION ) ) {
84 $role = ( isset( $_GET['role'] ) ? sanitize_text_field( $_GET['role'] ) : false );
85 $productId = ( isset( $_GET['product_id'] ) ? intval( $_GET['product_id'] ) : 0 );
86 $loop = ( isset( $_GET['loop'] ) && '' !== $_GET['loop'] ? intval( $_GET['loop'] ) : null );
87 $role = get_role( $role );
88 $product = wc_get_product( $productId );
89 if ( $role && $product ) {
90 $pricingRule = RoleBasedPricingRule::build( $productId, $role->name );
91 wp_send_json( array(
92 'success' => true,
93 'role_row_html' => $this->getContainer()->getFileManager()->renderTemplate( 'addons/role-based-pricing/role.php', array(
94 'loop' => $loop,
95 'pricing_rule' => $pricingRule,
96 'product' => $product,
97 ) ),
98 ) );
99 }
100 wp_send_json( array(
101 'success' => false,
102 'error_message' => __( 'Invalid role', 'tier-pricing-table' ),
103 ) );
104 }
105 wp_send_json( array(
106 'success' => false,
107 'error_message' => __( 'Invalid nonce', 'tier-pricing-table' ),
108 ) );
109 }
110
111 }
112