具有相同名称的多个属性 Woocommerce

问题描述 投票:0回答:2

我有许多具有相同显示名称但唯一的段的属性。当创建新的可变产品时,不可能区分两者之间的区别。

是否可以将 slug 包含在“添加”属性下拉列表中?

Screenshot

php wordpress woocommerce code-snippets
2个回答
1
投票

我有同样的需求,不知何故我可以通过使用 PHP + jQuery 来实现它:

将以下内容添加到您的主题functions.php文件中:

// Show attribute label + slugs in wordpress admin product editing pages.
function show_attribute_slugs_in_product_edit_page( $hook ) {
    global $post;
    if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
        if ( 'product' === $post->post_type ) {     
            wp_enqueue_script(  'show_slugs', get_stylesheet_directory_uri().'/assets/js/show_slugs.js' );
        }
    }
}
add_action( 'admin_enqueue_scripts', 'show_attribute_slugs_in_product_edit_page', 10, 1 );

并在 your-theme > assets > js 文件夹中创建一个名为 show_slugs.js 的文件,其中包含以下内容:

(function( $ ){
    var attr_renamed = false;
    $(document).on('click', '#woocommerce-product-data > div.inside > div > ul > li.attribute_tab > a', function(){ 
        if (attr_renamed === false) {
            $("#product_attributes > div.toolbar.toolbar-top > select option").not(":nth-child(1)").each(function() {
                var attr_label = $(this).text();
                var attr_value = $(this).val().replace('pa_', '');
                $(this).text(attr_label + ' (' + attr_value + ')');
                attr_renamed = true;
            });
        }
    });
})( jQuery );

Screenshot


-2
投票

我有同样的问题。我需要在下拉列表中显示值。我将您的代码添加到我的网站,但它对属性不起作用。检查了一些东西,但我无法修复它。

© www.soinside.com 2019 - 2024. All rights reserved.