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 / UserBasedPricing / ProductManager.php

ProductManager.php in Tiered Pricing Table for WooCommerce 6.5.0, at src/Addons/UserBasedPricing/ProductManager.php

111 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\UserBasedPricing;
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_USER_ROW_HTML__ACTION = 'tpt_get_user_row_html';
14
15 public function __construct() {
16 // Get user row via AJAX
17 add_action( 'wp_ajax_' . self::GET_USER_ROW_HTML__ACTION, array($this, 'getUserRowHtml') );
18 // Render (priority 100 to show after role-based pricing)
19 add_action(
20 'tiered_pricing_table/admin/before_advance_product_options',
21 array($this, 'render'),
22 100,
23 1
24 );
25 add_action(
26 'woocommerce_variation_options_pricing',
27 function ( $loop, $variationData, WP_Post $variation ) {
28 $this->render( $variation->ID, $loop );
29 },
30 12,
31 3
32 );
33 // Save
34 add_action( 'woocommerce_process_product_meta', function ( $productId ) {
35 } );
36 add_action(
37 'woocommerce_save_product_variation',
38 function ( $variationId, $loop ) {
39 },
40 10,
41 3
42 );
43 }
44
45 /**
46 * Delete removed user-based rules
47 *
48 * @param int $productId
49 * @param null|int $loop
50 * @param array $data
51 */
52 public function handleRemoving( $productId, $loop, $data ) {
53 if ( !empty( $data['tiered_price_rules_users_to_delete'] ) ) {
54 foreach ( $data['tiered_price_rules_users_to_delete'] as $userToRemove ) {
55 if ( !empty( $userToRemove ) ) {
56 UserBasedPriceManager::deleteAllDataForUser( $productId, (int) $userToRemove );
57 }
58 }
59 }
60 if ( !empty( $data['tiered_price_rules_users_to_delete_variation'][$loop] ) ) {
61 foreach ( $data['tiered_price_rules_users_to_delete_variation'][$loop] as $userToRemove ) {
62 if ( !empty( $userToRemove ) ) {
63 UserBasedPriceManager::deleteAllDataForUser( $productId, (int) $userToRemove );
64 }
65 }
66 }
67 }
68
69 public function render( $productId, $loop = null ) {
70 $this->getContainer()->getFileManager()->includeTemplate( 'addons/user-based-pricing/user-based-block.php', array(
71 'product_id' => $productId,
72 'loop' => $loop,
73 ) );
74 }
75
76 /**
77 * AJAX Handler
78 */
79 public function getUserRowHtml() {
80 $nonce = ( isset( $_GET['nonce'] ) ? sanitize_text_field( $_GET['nonce'] ) : false );
81 if ( wp_verify_nonce( $nonce, self::GET_USER_ROW_HTML__ACTION ) ) {
82 $userId = ( isset( $_GET['user_id'] ) ? intval( $_GET['user_id'] ) : 0 );
83 $productId = ( isset( $_GET['product_id'] ) ? intval( $_GET['product_id'] ) : 0 );
84 $loop = ( isset( $_GET['loop'] ) && '' !== $_GET['loop'] ? intval( $_GET['loop'] ) : null );
85 $user = get_userdata( $userId );
86 $product = wc_get_product( $productId );
87 if ( $user && $product ) {
88 $pricingRule = UserBasedPricingRule::build( $productId, $userId );
89 wp_send_json( array(
90 'success' => true,
91 'user_row_html' => $this->getContainer()->getFileManager()->renderTemplate( 'addons/user-based-pricing/user.php', array(
92 'loop' => $loop,
93 'pricing_rule' => $pricingRule,
94 'product' => $product,
95 'user' => $user,
96 ) ),
97 ) );
98 }
99 wp_send_json( array(
100 'success' => false,
101 'error_message' => __( 'Invalid user', 'tier-pricing-table' ),
102 ) );
103 }
104 wp_send_json( array(
105 'success' => false,
106 'error_message' => __( 'Invalid nonce', 'tier-pricing-table' ),
107 ) );
108 }
109
110 }
111