Chia sẻ code thêm nhanh thuộc tính trong Woo

Cập nhật lần cuối 25/11/2025 by trong WordPress vào 25/11/2025 có 2 Views

Vấn đề: Khi sản phẩm bạn có nhiều thuộc tính. Mỗi khi thêm sản phẩm bạn phải bấm thêm mới từng thuộc tính một sẽ rất lâu và mất thời gian

Hướng giải quyết: Chúng ta sẽ cùng nhau thêm chức năng để tạo ra được nhiều nút thêm nhanh thuộc tính mỗi khi thêm sản phẩm bạn chỉ cần ấn vào nút thêm nhanh này sẽ load toàn bộ thuộc tính mình cần ra luôn => Rất nhanh phải không nào?

Chia sẻ code thêm nhanh thuộc tính

Bạn chỉ cần thêm đoạn code sau vào wp-content/themes/{your-theme}/functions.php sau đó sửa nhóm thuộc tính cho đúng với nhu cầu của từng site là được

Để duy trì blog nên mình có làm aff cho 1 số bên hosting. Nhưng dù aff mình cũng chọn 1 số nhà cung cấp uy tín về chất lượng và support nên các bạn cứ yên tâm nhé.

Nếu có mua hosting mà có trong list dưới đây các bạn click vào link trước khi mua để ủng hộ mình nhé. Mình cảm ơn nhiều

/*
Thêm nhanh thuộc tính sản phẩm
Author: Levantoan.com
*/
function devvn_admin_scripts() {
    $screen = get_current_screen();
    if ( ! $screen || $screen->post_type !== 'product' || $screen->base !== 'post' ) {
        return;
    }
    
    global $wc_product_attributes;

    $data_args = array(
        array(
            'name_group' => 'Thêm nhanh thuộc tính',
            'attributes' => array(
                array(
                    'slug' => 'loai-da',
                    'is_visibility' => true,
                ),
                array(
                    'slug' => 'van-de-ve-da',
                    'is_visibility' => true,
                ),
                array(
                    'slug' => 'thanh-phan',
                    'is_visibility' => true,
                ),
            ),
        ),
    );

    $metabox_product_rule = array();
    if ( !empty( $data_args ) ) {
        foreach ( $data_args as $loop => $group ) {
            $name_group = isset( $group['name_group'] ) ? $group['name_group'] : '';
            $attributes = isset( $group['attributes'] ) ? $group['attributes'] : array();
            $metabox_product_rule[ $loop ]['cat_name'] = $name_group;
            if ( !empty( $attributes ) ) {
                foreach ( $attributes as $value ) {
                    $slug = isset( $value['slug'] ) ? $value['slug'] : '';
                    if ( isset( $wc_product_attributes[ 'pa_' . $slug ] ) ) {
                        $slug = 'pa_' . $slug;
                    }
                    $is_visibility = isset( $value['is_visibility'] ) ? $value['is_visibility'] : true;
                    $metabox_product_rule[ $loop ]['list_attribute'][] = array(
                        'slug' => $slug,
                        'is_visibility' => $is_visibility,
                    );
                }
            }
        }
    }
    $devvn_admin_array = array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'metabox_product_rule'  =>  json_encode($metabox_product_rule)
    );
    ?>
    <script type="text/javascript">
    var devvn_admin_array = <?php echo wp_json_encode($devvn_admin_array); ?>;
    (function($){
        $(document).ready(function(){
            if($('#product_attributes').length > 0){
                function devvn_attribute_row_indexes() {
                    $( '.product_attributes .woocommerce_attribute' ).each( function( index, el ) {
                        $( '.attribute_position', el ).val( parseInt( $( el ).index( '.product_attributes .woocommerce_attribute' ), 10 ) );
                    });
                }
                var wrapper = $('#product_attributes');
                var html = '';
                var list_attribute_cat = $.parseJSON(devvn_admin_array.metabox_product_rule);
                if(list_attribute_cat.length > 0){
                    $(list_attribute_cat).each(function(index, value){
                        html += '<button type="button" data-index="'+index+'" class="button devvn_add_bulk_attribute" title="Thêm nhanh các thuộc tính cho '+value.cat_name+'">'+value.cat_name+'</button>';
                    });
                    $('.toolbar.toolbar-top .actions', wrapper).append(html);
                }

                $('body').on('click', '.devvn_add_bulk_attribute', function () {
                    var size         = $( '.product_attributes .woocommerce_attribute' ).length;
                    var $wrapper     = $( this ).closest( '#product_attributes' );
                    var $attributes  = $wrapper.find( '.product_attributes' );
                    var product_type = $( 'select#product-type' ).val();
                    var index        = $(this).data('index');
                    /*console.log(list_attribute_cat[index]);
                    return false;*/
                    var data = {
                        action:   'devvn_add_bulk_attribute',
                        list_attribute: list_attribute_cat[index].list_attribute,
                        stt:        size,
                        security: woocommerce_admin_meta_boxes.add_attribute_nonce
                    };

                    $wrapper.block({
                        message: null,
                        overlayCSS: {
                            background: '#fff',
                            opacity: 0.6
                        }
                    });
                    $.post( devvn_admin_array.ajax_url, data, function( response ) {
                        $attributes.append( response );

                        if ( 'variable' !== product_type ) {
                            $attributes.find( '.enable_variation' ).hide();
                        }

                        $( document.body ).trigger( 'wc-enhanced-select-init' );
                        devvn_attribute_row_indexes();
                        $wrapper.unblock();

                        $( document.body ).trigger( 'woocommerce_added_attribute' );
                    });

                    if(list_attribute_cat[index].list_attribute){
                        $(list_attribute_cat[index].list_attribute).each(function(index, value){
                            $( 'select.attribute_taxonomy' ).find( 'option[value="' + value.slug + '"]' ).attr( 'disabled','disabled' );
                        })
                    }

                    return false;
                });
            }
        })
    })(jQuery)
    </script>
    <?php
}
add_action( 'admin_footer', 'devvn_admin_scripts' );

add_action( 'wp_ajax_devvn_add_bulk_attribute', 'devvn_add_bulk_attribute_func' );
function devvn_add_bulk_attribute_func(){
    ob_start();
    check_ajax_referer( 'add-attribute', 'security' );

    if ( ! current_user_can( 'edit_products' ) ) {
        wp_die( -1 );
    }

    $list_attribute = isset($_POST['list_attribute']) ? wc_clean( $_POST['list_attribute'] ) : array();

    $stt = isset($_POST['stt']) ? absint( $_POST['stt'] ) : 0;
    if($list_attribute && is_array($list_attribute) && !empty($list_attribute)){
        $loop = 0;
        foreach($list_attribute as $attr_item){

            $attr_slug = isset($attr_item['slug']) ? $attr_item['slug'] : '';
            $attr_visibility = isset($attr_item['is_visibility']) ? $attr_item['is_visibility'] : true;

            $i             = $stt + $loop;
            $metabox_class = array();
            $attribute     = new WC_Product_Attribute();

            $attribute->set_id( wc_attribute_taxonomy_id_by_name( sanitize_text_field( $attr_slug ) ) );
            $attribute->set_name( sanitize_text_field( $attr_slug ) );
            $attribute->set_visible( apply_filters( 'woocommerce_attribute_default_visibility', 1 ) );
            $attribute->set_variation( apply_filters( 'woocommerce_attribute_default_is_variation', 0 ) );
            $attribute->set_visible( $attr_visibility );

            if ( $attribute->is_taxonomy() ) {
                $metabox_class[] = 'taxonomy';
                $metabox_class[] = $attribute->get_name();
            }

            include WC_ABSPATH . 'includes/admin/meta-boxes/views/html-product-attribute.php';
            $loop ++;
        }
    }
    echo ob_get_clean();
    wp_die();
}

Trong code trên bạn cần lưu ý đoạn code sau:

$data_args = array(
        array(
            'name_group' => 'Thêm nhanh thuộc tính',
            'attributes' => array(
                array(
                    'slug' => 'loai-da',
                    'is_visibility' => true,
                ),
                array(
                    'slug' => 'van-de-ve-da',
                    'is_visibility' => true,
                ),
                array(
                    'slug' => 'thanh-phan',
                    'is_visibility' => true,
                ),
            ),
        ),
    );

Ở đoạn này bạn hãy đổi thành dữ liệu của bạn cho phù hợp nhé

  • slug: là slug của thuộc tính
  • is_visibility: Là có hiện thuộc tính này trên trang sản phẩm hay ko. true hoặc false

Ví dụ thêm nhiều nhóm thì sửa đoạn đó như sau

$data_args = array(
        array(
            'name_group' => 'Thêm nhanh thuộc tính',
            'attributes' => array(
                array(
                    'slug' => 'loai-da',
                    'is_visibility' => true,
                ),
                array(
                    'slug' => 'van-de-ve-da',
                    'is_visibility' => true,
                ),
                array(
                    'slug' => 'thanh-phan',
                    'is_visibility' => true,
                ),
            ),
        ),
        array(
            'name_group' => 'Vợt cầu lông',
            'attributes' => array(
                array(
                    'slug' => 'brand',
                    'is_visibility' => true,
                ),
                array(
                    'slug' => 'trong-luong',
                    'is_visibility' => true,
                ),
                array(
                    'slug' => 'kich-thuoc-can-vot',
                    'is_visibility' => true,
                ),
                array(
                    'slug' => 'diem-can-bang',
                    'is_visibility' => true,
                ),
            ),
        ),
    );

Code này mình sẽ tối giản nhất để các bạn dễ sử dụng. Ngoài cách này bạn có thể dùng thêm ACF Repeater hay SCF để thêm dạng field repeater để thêm các nhóm trong admin cho khách có thể tự thêm nhé

Chúc các bạn làm được như ý

5/5 - (1 vote)
  • Bình luận
Sản phẩm nổi bật của Toản
x