| 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 |
// Activate new tab |
| 19 |
add_filter( 'tiered_pricing_table/admin/role_customer_pricing_tab_active', '__return_true' ); |
| 20 |
// Render (priority 100 to show after role-based pricing) |
| 21 |
add_action( |
| 22 |
'tiered_pricing_table/admin/role_customer_pricing_tab_content', |
| 23 |
array($this, 'render'), |
| 24 |
100, |
| 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 |
12, |
| 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 user-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_users_to_delete'] ) ) { |
| 56 |
foreach ( $data['tiered_price_rules_users_to_delete'] as $userToRemove ) { |
| 57 |
if ( !empty( $userToRemove ) ) { |
| 58 |
UserBasedPriceManager::deleteAllDataForUser( $productId, (int) $userToRemove ); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
if ( !empty( $data['tiered_price_rules_users_to_delete_variation'][$loop] ) ) { |
| 63 |
foreach ( $data['tiered_price_rules_users_to_delete_variation'][$loop] as $userToRemove ) { |
| 64 |
if ( !empty( $userToRemove ) ) { |
| 65 |
UserBasedPriceManager::deleteAllDataForUser( $productId, (int) $userToRemove ); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public function render( $productId, $loop = null ) { |
| 72 |
$this->getContainer()->getFileManager()->includeTemplate( 'addons/user-based-pricing/user-based-block.php', array( |
| 73 |
'product_id' => $productId, |
| 74 |
'loop' => $loop, |
| 75 |
) ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* AJAX Handler |
| 80 |
*/ |
| 81 |
public function getUserRowHtml() { |
| 82 |
$nonce = ( isset( $_GET['nonce'] ) ? sanitize_text_field( $_GET['nonce'] ) : false ); |
| 83 |
if ( wp_verify_nonce( $nonce, self::GET_USER_ROW_HTML__ACTION ) ) { |
| 84 |
$userId = ( isset( $_GET['user_id'] ) ? intval( $_GET['user_id'] ) : 0 ); |
| 85 |
$productId = ( isset( $_GET['product_id'] ) ? intval( $_GET['product_id'] ) : 0 ); |
| 86 |
$loop = ( isset( $_GET['loop'] ) && '' !== $_GET['loop'] ? intval( $_GET['loop'] ) : null ); |
| 87 |
$user = get_userdata( $userId ); |
| 88 |
$product = wc_get_product( $productId ); |
| 89 |
if ( $user && $product ) { |
| 90 |
$pricingRule = UserBasedPricingRule::build( $productId, $userId ); |
| 91 |
wp_send_json( array( |
| 92 |
'success' => true, |
| 93 |
'user_row_html' => $this->getContainer()->getFileManager()->renderTemplate( 'addons/user-based-pricing/user.php', array( |
| 94 |
'loop' => $loop, |
| 95 |
'pricing_rule' => $pricingRule, |
| 96 |
'product' => $product, |
| 97 |
'user' => $user, |
| 98 |
) ), |
| 99 |
) ); |
| 100 |
} |
| 101 |
wp_send_json( array( |
| 102 |
'success' => false, |
| 103 |
'error_message' => __( 'Invalid user', 'tier-pricing-table' ), |
| 104 |
) ); |
| 105 |
} |
| 106 |
wp_send_json( array( |
| 107 |
'success' => false, |
| 108 |
'error_message' => __( 'Invalid nonce', 'tier-pricing-table' ), |
| 109 |
) ); |
| 110 |
} |
| 111 |
|
| 112 |
} |
| 113 |
|