我试图在woocommerce的产品编辑站点中创建一个复选框,从而允许我启用/禁用仅在特定产品上显示的自定义徽章横幅。我可以在正常的编辑页面中使用它,但也想在产品quickedit面板中显示该复选框。
我在产品编辑页面中看到了它的代码,如下所示。这非常简单,因为它开箱即用。
/* 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 );
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句