Untitled
webwizards
plain_text
17 days ago
6.1 kB
6
Indexable
function get_user_purchase_lists($user_id) {
if (!$user_id) {
return [];
}
$purchase_lists = get_posts([
'post_type' => 'b2bking_list',
'post_status' => 'publish',
'numberposts' => -1,
'author' => $user_id,
'orderby' => 'title',
'order' => 'ASC'
]);
return $purchase_lists;
}
/**
* Add product to purchase list
*/
function add_product_to_purchase_list($list_id, $product_id, $quantity = 1) {
// Verify user owns the list
$list = get_post($list_id);
if (!$list || $list->post_type !== 'b2bking_list' || $list->post_author != get_current_user_id()) {
return ['success' => false, 'message' => 'Invalid list or permission denied'];
}
// Get current list details
$list_details = get_post_meta($list_id, 'b2bking_purchase_list_details', true);
// Parse existing items
$items = [];
if (!empty($list_details)) {
$pairs = explode('|', trim($list_details, '|'));
foreach ($pairs as $pair) {
if (empty($pair)) continue;
list($pid, $qty) = explode(':', $pair);
$items[$pid] = intval($qty);
}
}
// Add or update product quantity
if (isset($items[$product_id])) {
$items[$product_id] += intval($quantity);
} else {
$items[$product_id] = intval($quantity);
}
// Build new list details string
$new_list_details = '';
foreach ($items as $pid => $qty) {
$new_list_details .= $pid . ':' . $qty . '|';
}
// Update list
update_post_meta($list_id, 'b2bking_purchase_list_details', $new_list_details);
return ['success' => true, 'message' => 'Product added to list'];
}
/**
* AJAX handler for adding product to list
*/
function ajax_add_to_purchase_list() {
check_ajax_referer('add_to_purchase_list_nonce', 'nonce');
if (!is_user_logged_in()) {
wp_send_json_error(['message' => 'You must be logged in']);
}
$list_id = intval($_POST['list_id']);
$product_id = intval($_POST['product_id']);
$quantity = isset($_POST['quantity']) ? intval($_POST['quantity']) : 1;
if (!$list_id || !$product_id) {
wp_send_json_error(['message' => 'Invalid parameters']);
}
$result = add_product_to_purchase_list($list_id, $product_id, $quantity);
if ($result['success']) {
wp_send_json_success($result);
} else {
wp_send_json_error($result);
}
}
add_action('wp_ajax_add_to_purchase_list', 'ajax_add_to_purchase_list');
/**
* Display purchase list selector on product page
*/
function display_purchase_list_selector() {
if (!is_user_logged_in()) {
return;
}
global $product;
if (!$product) {
return;
}
$user_id = get_current_user_id();
$lists = get_user_purchase_lists($user_id);
if (empty($lists)) {
return;
}
?>
<div class="purchase-list-selector" style="margin: 20px 0;">
<label for="purchase-list-dropdown" style="display: block; margin-bottom: 8px; font-weight: 600;">
Add to Purchase List:
</label>
<div style="display: flex; gap: 10px; align-items: flex-start;">
<select id="purchase-list-dropdown" class="purchase-list-dropdown" style="flex: 1; padding: 8px;">
<option value="">-- Select a list --</option>
<?php foreach ($lists as $list) : ?>
<option value="<?php echo esc_attr($list->ID); ?>">
<?php echo esc_html($list->post_title); ?>
</option>
<?php endforeach; ?>
</select>
<button type="button" class="button add-to-purchase-list-btn" data-product-id="<?php echo esc_attr($product->get_id()); ?>">
Add to List
</button>
</div>
<div class="purchase-list-message" style="margin-top: 10px; display: none;"></div>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.add-to-purchase-list-btn').on('click', function(e) {
e.preventDefault();
var button = $(this);
var listId = $('#purchase-list-dropdown').val();
var productId = button.data('product-id');
var quantity = $('input.qty').val() || 1;
var messageDiv = $('.purchase-list-message');
if (!listId) {
messageDiv.html('<span style="color: #e74c3c;">Please select a list</span>').show();
return;
}
button.prop('disabled', true).text('Adding...');
messageDiv.hide();
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: {
action: 'add_to_purchase_list',
nonce: '<?php echo wp_create_nonce('add_to_purchase_list_nonce'); ?>',
list_id: listId,
product_id: productId,
quantity: quantity
},
success: function(response) {
if (response.success) {
messageDiv.html('<span style="color: #27ae60;">✓ ' + response.data.message + '</span>').show();
} else {
messageDiv.html('<span style="color: #e74c3c;">✗ ' + response.data.message + '</span>').show();
}
},
error: function() {
messageDiv.html('<span style="color: #e74c3c;">✗ An error occurred</span>').show();
},
complete: function() {
button.prop('disabled', false).text('Add to List');
}
});
});
});
</script>
<?php
}
// Hook into WooCommerce product page (after add to cart button)
add_action('woocommerce_after_add_to_cart_button', 'display_purchase_list_selector', 10);Editor is loading...
Leave a Comment