Untitled
webwizards
plain_text
3 months ago
5.3 kB
11
Indexable
// Hook to add meta box
add_action('add_meta_boxes', 'b2bking_add_pricing_meta_box');
function b2bking_add_pricing_meta_box() {
add_meta_box(
'b2bking_pricing_details',
'Product Pricing Details',
'b2bking_pricing_meta_box_callback',
'b2bking_conversation',
'normal',
'high'
);
}
function b2bking_pricing_meta_box_callback($post) {
// Get the quote products meta
$quote_products = get_post_meta($post->ID, 'b2bking_quote_products', true);
if (empty($quote_products)) {
echo '<p>No products found in this conversation.</p>';
return;
}
// Get the conversation author (user who made the request)
$user_id = get_post_field('post_author', $post->ID);
// Get user's group ID
$currentusergroupidnr = get_user_meta($user_id, 'b2bking_customergroup', true);
// Parse the products string
$products = explode('|', $quote_products);
if (empty($products)) {
echo '<p>No valid products found.</p>';
return;
}
// Add some basic styling
echo '<style>
.b2bking-pricing-table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}
.b2bking-pricing-table th,
.b2bking-pricing-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.b2bking-pricing-table th {
background-color: #f2f2f2;
font-weight: bold;
}
.b2bking-pricing-table tr:nth-child(even) {
background-color: #f9f9f9;
}
.price-customer { color: #0073aa; font-weight: bold; }
.price-regular { color: #666; }
.price-group-regular { color: #d63638; }
.price-group-sale { color: #00a32a; font-weight: bold; }
</style>';
echo '<div class="b2bking-pricing-container">';
echo '<h4>User Group ID: ' . esc_html($currentusergroupidnr ? $currentusergroupidnr : 'No group assigned') . '</h4>';
echo '<table class="b2bking-pricing-table">';
echo '<thead>';
echo '<tr>';
echo '<th>Product</th>';
echo '<th>Quantity</th>';
echo '<th>Customer Price</th>';
echo '<th>Regular Price</th>';
echo '<th>Group Regular Price</th>';
echo '<th>Group Sale Price</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach ($products as $product_string) {
if (empty(trim($product_string))) continue;
// Parse product data: product_ID;quantity;customer_price
$product_parts = explode(';', $product_string);
if (count($product_parts) !== 3) continue;
$product_id_raw = $product_parts[0];
$quantity = $product_parts[1];
$customer_price = $product_parts[2];
// Extract product ID (remove 'product_' prefix if present)
$product_id = str_replace('product_', '', $product_id_raw);
$product_id = intval($product_id);
if ($product_id <= 0) continue;
// Get WooCommerce product object
$product = wc_get_product($product_id);
if (!$product) {
echo '<tr>';
echo '<td colspan="6">Product ID ' . esc_html($product_id) . ' not found</td>';
echo '</tr>';
continue;
}
// Get regular price from database
$regular_price = get_post_meta($product_id, '_regular_price', true);
// Get group prices
$user_group_regular_price = '';
$user_group_sale_price = '';
if ($currentusergroupidnr) {
$user_group_regular_price = get_post_meta($product_id, 'b2bking_regular_product_price_group_' . $currentusergroupidnr, true);
$user_group_sale_price = get_post_meta($product_id, 'b2bking_sale_product_price_group_' . $currentusergroupidnr, true);
}
// Format prices
$currency_symbol = get_woocommerce_currency_symbol();
$customer_price_formatted = $customer_price ? $currency_symbol . number_format(floatval($customer_price), 2) : '-';
$regular_price_formatted = $regular_price ? $currency_symbol . number_format(floatval($regular_price), 2) : '-';
$group_regular_formatted = $user_group_regular_price ? $currency_symbol . number_format(floatval($user_group_regular_price), 2) : '-';
$group_sale_formatted = $user_group_sale_price ? $currency_symbol . number_format(floatval($user_group_sale_price), 2) : '-';
echo '<tr>';
echo '<td>';
echo '<strong>' . esc_html($product->get_name()) . '</strong><br>';
echo '<small>ID: ' . esc_html($product_id) . '</small>';
echo '</td>';
echo '<td>' . esc_html($quantity) . '</td>';
echo '<td class="price-customer">' . esc_html($customer_price_formatted) . '</td>';
echo '<td class="price-regular">' . esc_html($regular_price_formatted) . '</td>';
echo '<td class="price-group-regular">' . esc_html($group_regular_formatted) . '</td>';
echo '<td class="price-group-sale">' . esc_html($group_sale_formatted) . '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
echo '</div>';
}Editor is loading...
Leave a Comment