Untitled

 avatar
unknown
plain_text
2 months ago
5.6 kB
2
Indexable

// Add the quick edit fields
add_action('woocommerce_product_quick_edit_end', function() {
    $groups = get_posts(array(
        'post_type' => 'b2bking_group',
        'post_status' => 'publish',
        'numberposts' => -1
    ));

    if (!empty($groups)) {
        echo '<div class="b2bking-prices-wrapper">';
        foreach ($groups as $group) {
            ?>
            <br class="clear"><div class="b2bking-group-prices" data-group-id="<?php echo esc_attr($group->ID); ?>">
                <h4><?php echo esc_html($group->post_title) . ' Prices'; ?></h4>
                <label>
                    <span class="title"><?php echo esc_html__('Regular', 'b2bking'); ?></span>
                    <span class="input-text-wrap">
                        <input type="text" 
                               name="b2bking_regular_product_price_group_<?php echo esc_attr($group->ID); ?>" 
                               class="text wc_input_price" 
                               value="">
                    </span>
                </label>
                <br class="clear">
                <label>
                    <span class="title"><?php echo esc_html__('Sale', 'b2bking'); ?></span>
                    <span class="input-text-wrap">
                        <input type="text" 
                               name="b2bking_sale_product_price_group_<?php echo esc_attr($group->ID); ?>" 
                               class="text wc_input_price" 
                               value="">
                    </span>
                </label>
            </div>
            <?php
        }
        echo '</div>';
    }
});

// Add columns to products list
add_filter('manage_product_posts_columns', function($columns) {
    $groups = get_posts(array(
        'post_type' => 'b2bking_group',
        'post_status' => 'publish',
        'numberposts' => -1
    ));

    foreach ($groups as $group) {
        $columns['b2b_price_' . $group->ID] = sprintf(
            __('%s Prices', 'b2bking'),
            esc_html($group->post_title)
        );
    }
    
    return $columns;
});

// Populate the columns
add_action('manage_product_posts_custom_column', function($column, $post_id) {
    if (strpos($column, 'b2b_price_') === 0) {
        $group_id = str_replace('b2b_price_', '', $column);
        
        $regular_price = get_post_meta($post_id, 'b2bking_regular_product_price_group_' . $group_id, true);
        $sale_price = get_post_meta($post_id, 'b2bking_sale_product_price_group_' . $group_id, true);
        
        if (!empty($regular_price)) {
            echo 'Regular: ' . wc_price($regular_price) . '<br>';
        }
        if (!empty($sale_price)) {
            echo 'Sale: ' . wc_price($sale_price);
        }
        if (empty($regular_price) && empty($sale_price)) {
            echo '-';
        }

        // Hidden inputs for JavaScript
        echo '<input type="hidden" id="b2b_regular_price_' . $group_id . '_' . $post_id . '" value="' . esc_attr($regular_price) . '">';
        echo '<input type="hidden" id="b2b_sale_price_' . $group_id . '_' . $post_id . '" value="' . esc_attr($sale_price) . '">';
    }
}, 10, 2);

// Save the quick edit fields
add_action('woocommerce_product_quick_edit_save', function($product) {
    $groups = get_posts(array(
        'post_type' => 'b2bking_group',
        'post_status' => 'publish',
        'numberposts' => -1
    ));

    foreach ($groups as $group) {
        // Save regular price
        $regular_price_key = 'b2bking_regular_product_price_group_' . $group->ID;
        if (isset($_REQUEST[$regular_price_key])) {
            $regular_price = wc_clean($_REQUEST[$regular_price_key]);
            $product->update_meta_data($regular_price_key, $regular_price);
        }

        // Save sale price
        $sale_price_key = 'b2bking_sale_product_price_group_' . $group->ID;
        if (isset($_REQUEST[$sale_price_key])) {
            $sale_price = wc_clean($_REQUEST[$sale_price_key]);
            $product->update_meta_data($sale_price_key, $sale_price);
        }
    }
    
    $product->save();
});

// Add JavaScript to handle quick edit
add_action('admin_footer', function() {
    if (!isset($_GET['post_type']) || $_GET['post_type'] !== 'product') {
        return;
    }
    ?>
    <script type="text/javascript">
    jQuery(function($) {
        // When quick edit is clicked
        $('#the-list').on('click', '.editinline', function() {
            var post_id = $(this).closest('tr').attr('id');
            post_id = post_id.replace('post-', '');
            
            // Get all B2BKing group prices
            $('.b2bking-group-prices').each(function() {
                var group_id = $(this).data('group-id');
                
                // Set regular price
                var regular_price = $('#b2b_regular_price_' + group_id + '_' + post_id).val();
                $('input[name="b2bking_regular_product_price_group_' + group_id + '"]').val(regular_price);
                
                // Set sale price
                var sale_price = $('#b2b_sale_price_' + group_id + '_' + post_id).val();
                $('input[name="b2bking_sale_product_price_group_' + group_id + '"]').val(sale_price);
            });
        });
    });
    </script>

    <style type="text/css">
    .b2bking-prices-wrapper {
        padding: 10px 0;
    }
    .b2bking-group-prices {
        margin-bottom: 15px;
    }
    .b2bking-group-prices h4 {
        margin: 5px 0;
    }
    .b2bking-group-prices label {
        display: block;
        margin: 5px 0;
    }
    </style>
    <?php
});
Editor is loading...
Leave a Comment