admin管理员组文章数量:1390983
Is there a way when adding a new product that all available Attributes would automatically be added to this product and oppened/expanded so that when I'm adding new product I don't have to first add them and then click on the headers to expand them?
Hope this image clears what I want to achieve:
Sure, I can manually trigger the click on "Expand all" button with jQuery, but am having trouble finding the place where to add this piece of jQ code. More important to me is the former question though, so any push in the right direction is much appreciated.
edit: I managed to do the open(show) part of the problem by adding the line:
jQuery('.expand_all').trigger("click");
in the themes/mytheme/admin/admin-functions.php file in siteoptions_admin_head function. But the first qq still remains. I did find a work around though - I could first add one product and add every existing attribute to it, and when making new products I would make a copy of this product and them add/remove attributes I don't need. This however would not work dynamically if attributes change, so its not much of an improvement.
Is there a way when adding a new product that all available Attributes would automatically be added to this product and oppened/expanded so that when I'm adding new product I don't have to first add them and then click on the headers to expand them?
Hope this image clears what I want to achieve:
Sure, I can manually trigger the click on "Expand all" button with jQuery, but am having trouble finding the place where to add this piece of jQ code. More important to me is the former question though, so any push in the right direction is much appreciated.
edit: I managed to do the open(show) part of the problem by adding the line:
jQuery('.expand_all').trigger("click");
in the themes/mytheme/admin/admin-functions.php file in siteoptions_admin_head function. But the first qq still remains. I did find a work around though - I could first add one product and add every existing attribute to it, and when making new products I would make a copy of this product and them add/remove attributes I don't need. This however would not work dynamically if attributes change, so its not much of an improvement.
Share Improve this question edited Oct 25, 2013 at 12:21 Nikola asked Oct 25, 2013 at 12:13 NikolaNikola 1551 gold badge3 silver badges10 bronze badges 1- Can we get an updated answer to this question? All the answers are deprecated. – Rahmat Baghdadi Commented Mar 13, 2020 at 16:46
2 Answers
Reset to default 6This was not as easy as I initially thought.
This will show show every attribute.... apparently by default WC hides any attribute that doesn't have any terms. They're already in the code, just with a display:none;
.
function wpa_120062_scripts(){ ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.woocommerce_attributes > div').removeClass('closed').show();
$('#woocommerce_attributes a.expand_all').click();
});
</script>
<?php
}
add_action('admin_print_footer_scripts', 'wpa_120062_scripts');
Depending on whether you plan to click all the "Visible on the product page" checkboxes, you can add some data to the attributes for a particular post when it is created. Per some other WPA questions, I've hooked into transition_post_status
. The "auto-draft" status, supposedly only fires on first creation... from there it either goes to "draft" or any of the other post statuses.
function wpa_120062_new_product($new_status, $old_status, $post){
if ( $new_status == "auto-draft" && isset( $post->post_type ) && $post->post_type == 'product' ){
// do stuff here
$defaults = array ( 'pa_color' => array (
'name' => 'pa_color',
'value' => '',
'position' => 1,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 1,
),
'pa_capacity' => array (
'name' => 'pa_capacity',
'value' => '',
'position' => 2,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 1,
)
);
update_post_meta( $post->ID , '_product_attributes', $defaults );
}
}
add_action('transition_post_status', 'wpa_120062_new_product', 10, 3);
pa_capacity
and pa_color
are just some sample attributes that I already had installed.
PS- I would suggest updating wpa_120062_scripts
to only display on product edit screens, but I'm a little fried after that.
EDIT
We can use the woocommerce function wc_get_attribute_taxonomies()
to dynamically get all the attributes and loop through them:
function wpa_120062_new_product($new_status, $old_status, $post){
if ( $new_status == "auto-draft" && isset( $post->post_type ) && $post->post_type == 'product' ){
if( function_exists( 'wc_get_attribute_taxonomies' ) && ( $attribute_taxonomies = wc_get_attribute_taxonomies() ) ) {
$defaults = array();
foreach ( $attribute_taxonomies as $tax ) {
$name = wc_attribute_taxonomy_name( $tax->attribute_name );
// do stuff here
$defaults[ $name ] = array (
'name' => $name,
'value' => '',
'position' => 1,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 1,
);
update_post_meta( $post->ID , '_product_attributes', $defaults );
}
}
}
}
add_action('transition_post_status', 'wpa_120062_new_product', 10, 3);
PS- I think the defaults are going to be a little different in WC2.1. This isn't quite working the same for me any longer with my bleeding edge git versions.
WC 2.1 update:
function wpa_120062_scripts(){ ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.product_attributes > div').removeClass('closed').show();
$('#product_attributes a.expand_all').click();
});
</script>
本文标签: pluginsHow to automatically add and show(open) all the product attributes
版权声明:本文标题:plugins - How to automatically add and show(open) all the product attributes? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744671328a2618842.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论