在管理面板中向 woocommerce quickedit 添加自定义复选框
Add a custom checkbox to woocommerce quickedit in admin panel
我尝试在 woocommerce 的产品编辑网站中创建一个复选框,这样我就可以 enable/disable 仅在特定产品上显示自定义徽章横幅。我让它在普通编辑页面中工作,但也想在产品快速编辑面板中显示复选框。
我使用下面显示的代码在产品编辑页面中使用它。这非常简单,因为它开箱即用。
/* 1. Add new checkbox to product edit page (General tab) */
add_action('woocommerce_product_options_general_product_data', 'bbloomer_add_badge_checkbox_to_products');
function bbloomer_add_badge_checkbox_to_products() {
woocommerce_wp_checkbox(array(
'id' => 'custom_badge',
'class' => '',
'label' => 'Echt SPECHTWERK'
)
);
}
/* 2. Save checkbox via custom field */
add_action('save_post', 'bbloomer_save_badge_checkbox_to_post_meta');
function bbloomer_save_badge_checkbox_to_post_meta($product_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (isset($_POST['custom_badge'])) {
update_post_meta($product_id, 'custom_badge', $_POST['custom_badge']);
} else
delete_post_meta($product_id, 'custom_badge');
}
// 3. Display badge @ single product page if checkbox checked
add_action('woocommerce_single_product_summary', 'bbloomer_display_badge_if_checkbox', 6);
function bbloomer_display_badge_if_checkbox() {
global $product;
if (get_post_meta($product->get_id(), 'custom_badge', true)) {
?> <div class="echt-spechtwerk-badge">
<img class="advantages-symbols" src="<?php echo get_bloginfo('wpurl') . '/wp-content/uploads/echt-SPECHTWERK-V6.svg' ?>">
</div>
<?php
}
}
为产品批量编辑和自定义字段快速编辑添加以下代码 -
function add_custom_field_in_bulk_edit_quick_edit(){
echo '<div class="inline-edit-group">';
woocommerce_wp_checkbox( array(
'id' => 'custom_badge',
'class' => '',
'label' => 'Echt SPECHTWERK'
) );
echo '</div>';
}
add_action( 'woocommerce_product_quick_edit_end', 'add_custom_field_in_bulk_edit_quick_edit', 99 );
add_action( 'woocommerce_product_bulk_edit_end', 'add_custom_field_in_bulk_edit_quick_edit', 99 );
function save_custom_field_in_bulk_edit_quick_edit( $post_id, $post ){
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( 'product' !== $post->post_type ) return $post_id;
if (isset($_REQUEST['custom_badge'])) {
update_post_meta( $post_id, 'custom_badge', $_REQUEST['custom_badge'] );
} else {
delete_post_meta( $post_id, 'custom_badge' );
}
}
add_action( 'woocommerce_product_bulk_and_quick_edit', 'save_custom_field_in_bulk_edit_quick_edit', 99, 2 );
我尝试在 woocommerce 的产品编辑网站中创建一个复选框,这样我就可以 enable/disable 仅在特定产品上显示自定义徽章横幅。我让它在普通编辑页面中工作,但也想在产品快速编辑面板中显示复选框。
我使用下面显示的代码在产品编辑页面中使用它。这非常简单,因为它开箱即用。
/* 1. Add new checkbox to product edit page (General tab) */
add_action('woocommerce_product_options_general_product_data', 'bbloomer_add_badge_checkbox_to_products');
function bbloomer_add_badge_checkbox_to_products() {
woocommerce_wp_checkbox(array(
'id' => 'custom_badge',
'class' => '',
'label' => 'Echt SPECHTWERK'
)
);
}
/* 2. Save checkbox via custom field */
add_action('save_post', 'bbloomer_save_badge_checkbox_to_post_meta');
function bbloomer_save_badge_checkbox_to_post_meta($product_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (isset($_POST['custom_badge'])) {
update_post_meta($product_id, 'custom_badge', $_POST['custom_badge']);
} else
delete_post_meta($product_id, 'custom_badge');
}
// 3. Display badge @ single product page if checkbox checked
add_action('woocommerce_single_product_summary', 'bbloomer_display_badge_if_checkbox', 6);
function bbloomer_display_badge_if_checkbox() {
global $product;
if (get_post_meta($product->get_id(), 'custom_badge', true)) {
?> <div class="echt-spechtwerk-badge">
<img class="advantages-symbols" src="<?php echo get_bloginfo('wpurl') . '/wp-content/uploads/echt-SPECHTWERK-V6.svg' ?>">
</div>
<?php
}
}
为产品批量编辑和自定义字段快速编辑添加以下代码 -
function add_custom_field_in_bulk_edit_quick_edit(){
echo '<div class="inline-edit-group">';
woocommerce_wp_checkbox( array(
'id' => 'custom_badge',
'class' => '',
'label' => 'Echt SPECHTWERK'
) );
echo '</div>';
}
add_action( 'woocommerce_product_quick_edit_end', 'add_custom_field_in_bulk_edit_quick_edit', 99 );
add_action( 'woocommerce_product_bulk_edit_end', 'add_custom_field_in_bulk_edit_quick_edit', 99 );
function save_custom_field_in_bulk_edit_quick_edit( $post_id, $post ){
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( 'product' !== $post->post_type ) return $post_id;
if (isset($_REQUEST['custom_badge'])) {
update_post_meta( $post_id, 'custom_badge', $_REQUEST['custom_badge'] );
} else {
delete_post_meta( $post_id, 'custom_badge' );
}
}
add_action( 'woocommerce_product_bulk_and_quick_edit', 'save_custom_field_in_bulk_edit_quick_edit', 99, 2 );