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

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

110 lines 4.0 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 // Render
19 add_action(
20 'tiered_pricing_table/admin/before_advance_product_options',
21 array($this, 'render'),
22 99,
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 11,
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 role-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_roles_to_delete'] ) ) {
54 foreach ( $data['tiered_price_rules_roles_to_delete'] as $roleToRemove ) {
55 if ( !empty( $roleToRemove ) ) {
56 RoleBasedPriceManager::deleteAllDataForRole( $productId, $roleToRemove );
57 }
58 }
59 }
60 if ( !empty( $data['tiered_price_rules_roles_to_delete_variation'][$loop] ) ) {
61 foreach ( $data['tiered_price_rules_roles_to_delete_variation'][$loop] as $roleToRemove ) {
62 if ( !empty( $roleToRemove ) ) {
63 RoleBasedPriceManager::deleteAllDataForRole( $productId, $roleToRemove );
64 }
65 }
66 }
67 }
68
69 public function render( $productId, $loop = null ) {
70 $this->getContainer()->getFileManager()->includeTemplate( 'addons/role-based-pricing/role-based-block.php', array(
71 'product_id' => $productId,
72 'loop' => $loop,
73 ) );
74 }
75
76 /**
77 * AJAX Handler
78 */
79 public function getRoleRowHtml() {
80 $nonce = ( isset( $_GET['nonce'] ) ? sanitize_text_field( $_GET['nonce'] ) : false );
81 if ( wp_verify_nonce( $nonce, self::GET_ROLE_ROW_HTML__ACTION ) ) {
82 $role = ( isset( $_GET['role'] ) ? sanitize_text_field( $_GET['role'] ) : false );
83 $productId = ( isset( $_GET['product_id'] ) ? intval( $_GET['product_id'] ) : 0 );
84 $loop = ( isset( $_GET['loop'] ) && '' !== $_GET['loop'] ? intval( $_GET['loop'] ) : null );
85 $role = get_role( $role );
86 $product = wc_get_product( $productId );
87 if ( $role && $product ) {
88 $pricingRule = RoleBasedPricingRule::build( $productId, $role->name );
89 wp_send_json( array(
90 'success' => true,
91 'role_row_html' => $this->getContainer()->getFileManager()->renderTemplate( 'addons/role-based-pricing/role.php', array(
92 'loop' => $loop,
93 'pricing_rule' => $pricingRule,
94 'product' => $product,
95 ) ),
96 ) );
97 }
98 wp_send_json( array(
99 'success' => false,
100 'error_message' => __( 'Invalid role', 'tier-pricing-table' ),
101 ) );
102 }
103 wp_send_json( array(
104 'success' => false,
105 'error_message' => __( 'Invalid nonce', 'tier-pricing-table' ),
106 ) );
107 }
108
109 }
110