Untitled

 avatar
webwizards
plain_text
17 days ago
12 kB
12
Indexable
// Add price lists section to user profile
add_action('show_user_profile', 'cspg_add_price_lists_section', 100);
add_action('edit_user_profile', 'cspg_add_price_lists_section', 100);

function cspg_add_price_lists_section($user) {
    if (!current_user_can('edit_users')) return;
    
    $price_lists = get_user_meta($user->ID, '_custom_price_lists', true);
    if (!is_array($price_lists)) $price_lists = array('products' => array(), 'categories' => array());
    
    // Get B2BKing groups
    $groups = get_posts(array('post_type' => 'b2bking_group', 'numberposts' => -1, 'post_status' => 'publish'));
    
    ?>
    <div style="background:#fff;border:1px solid #ccd0d4;box-shadow:0 1px 1px rgba(0,0,0,.04);padding:20px;margin-top:20px;">
        <h3 style="margin-top:0;"><?php _e('B2BKing Price Lists'); ?></h3>
        
        <div style="background:#f0f0f1;padding:15px;margin-bottom:20px;border-radius:4px;">
            <label style="display:block;margin-bottom:8px;font-weight:600;"><?php _e('Product Search'); ?></label>
            <select id="cspg-product-search" style="width:100%;"></select>
            <p class="description" style="margin-top:8px;"><?php _e('Search for products to find their ID. Selected product ID will be shown below.'); ?></p>
            <div id="cspg-selected-product" style="margin-top:10px;display:none;padding:8px;background:#fff;border-left:3px solid #2271b1;">
                <strong><?php _e('Selected Product ID:'); ?></strong> <span id="cspg-selected-product-id"></span>
            </div>
        </div>
        
        <table class="form-table">
            <tr>
                <th><label><?php _e('Product Price Lists'); ?></label></th>
                <td>
                    <div id="cspg-product-lists">
                        <?php if (!empty($price_lists['products'])): ?>
                            <?php foreach ($price_lists['products'] as $index => $item): ?>
                                <div class="cspg-row" style="margin-bottom:10px;">
                                    <input type="text" name="cspg_product_id[]" value="<?php echo esc_attr($item['product_id']); ?>" placeholder="Product ID" style="width:100px;">
                                    <?php
                                    $product = wc_get_product($item['product_id']);
                                    if ($product) {
                                        echo '<span style="color:#666;margin-left:8px;">' . esc_html($product->get_name()) . '</span>';
                                    }
                                    ?>
                                    <select name="cspg_product_group[]" style="width:200px;margin-left:8px;">
                                        <option value=""><?php _e('Select Group'); ?></option>
                                        <?php foreach ($groups as $group): ?>
                                            <option value="<?php echo $group->ID; ?>" <?php selected($item['group_id'], $group->ID); ?>><?php echo $group->post_title; ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                    <button type="button" class="button cspg-remove" style="color:#a00;margin-left:8px;">Remove</button>
                                </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                    <button type="button" class="button" id="cspg-add-product">+ Add Product</button>
                    
                    <p class="description"><?php _e('Assign pricing groups to specific products (highest priority).'); ?></p>
                </td>
            </tr>
            <tr>
                <th><label><?php _e('Category Price Lists'); ?></label></th>
                <td>
                    <div id="cspg-category-lists">
                        <?php if (!empty($price_lists['categories'])): ?>
                            <?php foreach ($price_lists['categories'] as $index => $item): ?>
                                <div class="cspg-row" style="margin-bottom:10px;">
                                    <?php
                                    $categories = get_terms(array('taxonomy' => 'product_cat', 'hide_empty' => false));
                                    ?>
                                    <select name="cspg_category_id[]" style="width:200px;">
                                        <option value=""><?php _e('Select Category'); ?></option>
                                        <?php foreach ($categories as $cat): ?>
                                            <option value="<?php echo $cat->term_id; ?>" <?php selected($item['category_id'], $cat->term_id); ?>><?php echo $cat->name; ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                    <select name="cspg_category_group[]" style="width:200px;">
                                        <option value=""><?php _e('Select Group'); ?></option>
                                        <?php foreach ($groups as $group): ?>
                                            <option value="<?php echo $group->ID; ?>" <?php selected($item['group_id'], $group->ID); ?>><?php echo $group->post_title; ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                    <button type="button" class="button cspg-remove" style="color:#a00;">Remove</button>
                                </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                    <button type="button" class="button" id="cspg-add-category">+ Add Category</button>
                    
                    <p class="description"><?php _e('Assign pricing groups to product categories.'); ?></p>
                </td>
            </tr>
        </table>
        
        <p style="border-top:1px solid #ccd0d4;padding-top:15px;margin-top:20px;">
            <button type="submit" class="button button-primary"><?php _e('Save Price Lists'); ?></button>
        </p>
    </div>
    
    <script>
    jQuery(document).ready(function($) {
        var groups = <?php echo json_encode(array_map(function($g) { return array('id' => $g->ID, 'title' => $g->post_title); }, $groups)); ?>;
        
        // Initialize product search with Select2
        $('#cspg-product-search').select2({
            ajax: {
                url: ajaxurl,
                dataType: 'json',
                delay: 300,
                data: function (params) {
                    var searchTypes = ['products'];
                    
                    return {
                        action: 'b2bking_admin_content_search',
                        security: b2bking.security,
                        search: params.term,
                        types: searchTypes
                    };
                },
                processResults: function (data) {
                    if (data.success) {
                        return {
                            results: data.data
                        };
                    }
                    return { results: [] };
                },
                cache: true
            },
            minimumInputLength: 3,
            placeholder: 'Search for products...',
            allowClear: true,
            width: '100%'
        });
        
        // Show selected product ID
        $('#cspg-product-search').on('select2:select', function (e) {
            var data = e.params.data;
            $('#cspg-selected-product-id').text(data.id);
            $('#cspg-selected-product').show();
        });
        
        $('#cspg-product-search').on('select2:clear', function (e) {
            $('#cspg-selected-product').hide();
        });
        
        $('#cspg-add-product').click(function() {
            var html = '<div class="cspg-row" style="margin-bottom:10px;">' +
                '<input type="text" name="cspg_product_id[]" placeholder="Product ID" style="width:100px;">' +
                '<select name="cspg_product_group[]" style="width:200px;margin-left:8px;"><option value="">Select Group</option>';
            groups.forEach(function(g) { html += '<option value="' + g.id + '">' + g.title + '</option>'; });
            html += '</select><button type="button" class="button cspg-remove" style="color:#a00;margin-left:8px;">Remove</button></div>';
            $('#cspg-product-lists').append(html);
        });
        
        $('#cspg-add-category').click(function() {
            var categories = <?php echo json_encode(array_map(function($c) { return array('id' => $c->term_id, 'name' => $c->name); }, get_terms(array('taxonomy' => 'product_cat', 'hide_empty' => false)))); ?>;
            var html = '<div class="cspg-row" style="margin-bottom:10px;">' +
                '<select name="cspg_category_id[]" style="width:200px;"><option value="">Select Category</option>';
            categories.forEach(function(c) { html += '<option value="' + c.id + '">' + c.name + '</option>'; });
            html += '</select><select name="cspg_category_group[]" style="width:200px;"><option value="">Select Group</option>';
            groups.forEach(function(g) { html += '<option value="' + g.id + '">' + g.title + '</option>'; });
            html += '</select><button type="button" class="button cspg-remove" style="color:#a00;">Remove</button></div>';
            $('#cspg-category-lists').append(html);
        });
        
        $(document).on('click', '.cspg-remove', function() {
            $(this).closest('.cspg-row').remove();
        });
    });
    </script>
    <?php
}

// Save price lists
add_action('personal_options_update', 'cspg_save_price_lists');
add_action('edit_user_profile_update', 'cspg_save_price_lists');

function cspg_save_price_lists($user_id) {
    if (!current_user_can('edit_user', $user_id)) return;
    
    $price_lists = array('products' => array(), 'categories' => array());
    
    // Save product assignments
    if (isset($_POST['cspg_product_id']) && is_array($_POST['cspg_product_id'])) {
        foreach ($_POST['cspg_product_id'] as $index => $product_id) {
            $group_id = isset($_POST['cspg_product_group'][$index]) ? intval($_POST['cspg_product_group'][$index]) : 0;
            if (!empty($product_id) && $group_id > 0) {
                $price_lists['products'][] = array(
                    'product_id' => intval($product_id),
                    'group_id' => $group_id
                );
            }
        }
    }
    
    // Save category assignments
    if (isset($_POST['cspg_category_id']) && is_array($_POST['cspg_category_id'])) {
        foreach ($_POST['cspg_category_id'] as $index => $category_id) {
            $group_id = isset($_POST['cspg_category_group'][$index]) ? intval($_POST['cspg_category_group'][$index]) : 0;
            if ($category_id > 0 && $group_id > 0) {
                $price_lists['categories'][] = array(
                    'category_id' => intval($category_id),
                    'group_id' => $group_id
                );
            }
        }
    }
    
    update_user_meta($user_id, '_custom_price_lists', $price_lists);
}

// Hook into B2BKing pricing group filter
add_filter('b2bking_b2b_group_for_pricing', 'cspg_override_pricing_group', 10, 3);

function cspg_override_pricing_group($default_group, $user_id, $product_id) {
    $price_lists = get_user_meta($user_id, '_custom_price_lists', true);
    if (!is_array($price_lists)) return $default_group;
    
    // Check product-specific assignments (highest priority)
    if (!empty($price_lists['products'])) {
        foreach ($price_lists['products'] as $item) {
            if (intval($item['product_id']) === intval($product_id)) {
                return intval($item['group_id']);
            }
        }
    }
    
    // Check category assignments
    if (!empty($price_lists['categories'])) {
        $product_categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids'));
        foreach ($price_lists['categories'] as $item) {
            if (in_array(intval($item['category_id']), $product_categories)) {
                return intval($item['group_id']);
            }
        }
    }
    
    return $default_group;
}
Editor is loading...
Leave a Comment