| 1 |
<?php defined( 'ABSPATH' ) || die; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\UserBasedPricing\ProductManager; |
| 4 |
use TierPricingTable\Addons\UserBasedPricing\UserBasedPricingRule; |
| 5 |
use TierPricingTable\Addons\UserBasedPricing\UserBasedPriceManager; |
| 6 |
use TierPricingTable\Core\ServiceContainer; |
| 7 |
|
| 8 |
/** |
| 9 |
* Available variables |
| 10 |
* |
| 11 |
* @var int $product_id |
| 12 |
* @var int $loop |
| 13 |
*/ |
| 14 |
|
| 15 |
$product = wc_get_product( $product_id ); |
| 16 |
|
| 17 |
if ( ! $product ) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
$loop = ! is_null( $loop ) ? $loop : null; |
| 22 |
|
| 23 |
$fileManager = ServiceContainer::getInstance()->getFileManager(); |
| 24 |
|
| 25 |
?> |
| 26 |
|
| 27 |
<div class="form-field tpt-user-based-block" id="tpt-user-based-block-<?php echo esc_attr( $product_id ); ?>" |
| 28 |
data-product-type="<?php echo esc_attr( $product->get_type() ); ?>" |
| 29 |
data-add-action="<?php echo esc_attr( ProductManager::GET_USER_ROW_HTML__ACTION ); ?>" |
| 30 |
data-add-action-nonce="<?php echo esc_attr( wp_create_nonce( ProductManager::GET_USER_ROW_HTML__ACTION ) ); ?>" |
| 31 |
data-product-id="<?php echo esc_attr( $product_id ); ?>" |
| 32 |
data-loop="<?php echo esc_attr( $loop ); ?>"> |
| 33 |
<label class="tpt-user-based-block__name" style="line-height: normal"><?php esc_attr_e( 'Customer-Specific Pricing', 'tier-pricing-table' ); ?></label> |
| 34 |
<div class="tpt-user-based-block__content"> |
| 35 |
<div class="tpt-user-based-users"> |
| 36 |
<?php |
| 37 |
|
| 38 |
$presentUsers = array(); |
| 39 |
|
| 40 |
// We need to fetch users that have pricing. But we don't have a direct query for all users with this postmeta. |
| 41 |
// However, we can query postmeta directly to find user IDs. |
| 42 |
global $wpdb; |
| 43 |
$meta_keys_like = $wpdb->esc_like( '_user_' ) . '%' . $wpdb->esc_like( '_tiered_price_rules_type' ); |
| 44 |
$results = $wpdb->get_results( $wpdb->prepare( " |
| 45 |
SELECT meta_key FROM {$wpdb->postmeta} |
| 46 |
WHERE post_id = %d AND meta_key LIKE %s |
| 47 |
", $product_id, $meta_keys_like ) ); |
| 48 |
|
| 49 |
foreach ( $results as $result ) { |
| 50 |
// Extract user ID from meta_key (e.g. _user_123_tiered_price_rules_type) |
| 51 |
if ( preg_match( '/^_user_(\d+)_tiered_price_rules_type$/', $result->meta_key, $matches ) ) { |
| 52 |
$userId = (int) $matches[1]; |
| 53 |
$user = get_userdata( $userId ); |
| 54 |
|
| 55 |
if ( $user && UserBasedPriceManager::userHasRules( $userId, $product_id, 'edit' ) ) { |
| 56 |
|
| 57 |
$userBasedRule = UserBasedPricingRule::build( $product_id, $userId ); |
| 58 |
|
| 59 |
$fileManager->includeTemplate( 'addons/user-based-pricing/user.php', array( |
| 60 |
'pricing_rule' => $userBasedRule, |
| 61 |
'user' => $user, |
| 62 |
'product_id' => $product_id, |
| 63 |
'product' => $product, |
| 64 |
'loop' => $loop, |
| 65 |
) ); |
| 66 |
|
| 67 |
$presentUsers[] = $userId; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
?> |
| 72 |
</div> |
| 73 |
|
| 74 |
<div class="tpt-user-based-no-users" |
| 75 |
style="<?php echo esc_attr( ! empty( $presentUsers ) ? 'display: none;' : '' ); ?>"> |
| 76 |
<span> |
| 77 |
<?php |
| 78 |
esc_attr_e( 'Set up specific pricing and rules for individual customers.', |
| 79 |
'tier-pricing-table' ); |
| 80 |
?> |
| 81 |
</span> |
| 82 |
<p class="description" style="display: block; margin: 0"> |
| 83 |
<?php |
| 84 |
$settingsLink = add_query_arg( array( |
| 85 |
'section' => 'advanced', |
| 86 |
), ServiceContainer::getInstance()->getSettings()->getLink() ); |
| 87 |
|
| 88 |
$settingsLink = sprintf( '<a target="_blank" href="%s">%s</a>', esc_url( $settingsLink ), |
| 89 |
esc_html__( 'settings', 'tier-pricing-table' ) ); |
| 90 |
// translators: %s: settings link |
| 91 |
echo wp_kses_post( sprintf( __( 'You can disable this feature in the %s if you don\'t need customer-based pricing.', |
| 92 |
'tier-pricing-table' ), $settingsLink ) ); |
| 93 |
|
| 94 |
?> |
| 95 |
|
| 96 |
</p> |
| 97 |
</div> |
| 98 |
|
| 99 |
<div class="tpt-user-based-adding-form"> |
| 100 |
<select class="tpt-user-based-adding-form__user-selector wc-customer-search" data-placeholder="<?php esc_attr_e( 'Search for a customer…', 'woocommerce' ); ?>" data-allow_clear="true" style="width: 250px;"> |
| 101 |
</select> |
| 102 |
|
| 103 |
<button class="button tpt-user-based-adding-form__add-button"> |
| 104 |
<?php |
| 105 |
esc_attr_e( 'Add Customer', 'tier-pricing-table' ); |
| 106 |
?> |
| 107 |
</button> |
| 108 |
|
| 109 |
<div class="clear"></div> |
| 110 |
</div> |
| 111 |
|
| 112 |
<?php $usersToDeleteName = ! is_null( $loop ) ? "tiered_price_rules_users_to_delete_variation[$loop][]" : 'tiered_price_rules_users_to_delete[]'; ?> |
| 113 |
|
| 114 |
<select name="<?php echo esc_attr( $usersToDeleteName ); ?>" class="tiered_price_rules_users_to_delete" multiple |
| 115 |
style="display:none;"> |
| 116 |
<?php foreach ( $presentUsers as $presentUserId ) : ?> |
| 117 |
<option value="<?php echo esc_attr( $presentUserId ); ?>"><?php echo esc_attr( $presentUserId ); ?></option> |
| 118 |
<?php endforeach; ?> |
| 119 |
</select> |
| 120 |
</div> |
| 121 |
</div> |
| 122 |
|