如何解决将自定义字段添加到高级批量编辑
我只是在后端添加了一些自定义价格,并在产品页面上添加了激活此价格的按钮。这是我使用的代码:
对于后端:
// Add a custom field to product in backend (for testing)
add_action( 'woocommerce_product_options_pricing','add_field_product_options_pricing' );
function add_field_product_options_pricing() {
global $post;
echo '<div class="options_group">';
woocommerce_wp_text_input( array(
'id' => '_bestprice','label' => __('Best price','woocommerce') . ' (' . get_woocommerce_currency_symbol() . ')','placeholder' => __('Set the best price…','woocommerce'),'description' => __('Enter the custom value here.','desc_tip' => 'true',));
echo '</div>';
}
// Save product custom field to database when submitted in Backend (for testing)
add_action( 'woocommerce_process_product_meta','save_product_options_custom_fields',30,1 );
function save_product_options_custom_fields( $post_id ){
// Saving custom field value
if( isset( $_POST['_bestprice'] ) ){
update_post_meta( $post_id,'_bestprice',sanitize_text_field( $_POST['_bestprice'] ) );
}
}
[在此处输入图片说明][1]
对于按钮
// Add a 'Get better price' additional button and a hidden field below single add to cart button
add_action( 'woocommerce_after_add_to_cart_button','before_add_to_cart_button' );
function before_add_to_cart_button() {
global $product;
// Get your product 'bestpprice' custom field
$bestprice = get_post_meta( $product->get_id(),true);
if( ! empty($bestprice) ):
$bestprice = wc_get_price_to_display( $product,array( 'price' => $bestprice ) );
$reg_price = wc_get_price_to_display( $product,array( 'price' => $product->get_regular_price() ) );
$range = wc_format_sale_price( $reg_price,$bestprice );
?>
<!-- The button and hidden field -->
<div class="bestprice-wrapper"><br>
<a href="" class="get_bestprice button alt" id="get_bestprice"><?php _e('Get better price'); ?></a>
<input type="hidden" name="bestprice" id="bestprice" class="bestprice" value="" />
</div>
<!-- The jQuery code -->
<script type="text/javascript">
(function($){
var b = '<?php echo $bestprice; ?>',i = 'input[name=bestprice]',p = 'p.price',r = '<?php echo $range; ?>',t = 'a#get_bestprice'
u = true;
$(t).click( function(e){
e.preventDefault();
if(u){
$(p).html(r); // Replacing price with the range
$(i).val(b); // Set the best price in hidden input field
u = false; // Disable button
$(t).text('Better Price active'); // change button text
$(t).removeClass('alt'); // Remove button 'alt' class for styling
}
});
})(jQuery);
</script>
<?php
endif;
}
[在此处输入图片描述][2] 对于购物车
// Add custom fields data to cart items
add_filter( 'woocommerce_add_cart_item_data','custom_add_cart_item_data',20,2 );
function custom_add_cart_item_data( $cart_item,$product_id ){
if( ! isset( $_POST['bestprice'] ) )
return $cart_item;
if( ! empty( $_POST['bestprice'] ) )
$cart_item['custom_data']['bestprice'] = (float) esc_attr( $_POST['bestprice'] );
return $cart_item;
}
// Replacing cart item price with 'bestprice'
add_action( 'woocommerce_before_calculate_totals','set_cart_item_bestprice',1 );
function set_cart_item_bestprice( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ){
if( isset( $cart_item['custom_data']['bestprice'] ) ){
// Set the calculated item price (if there is one)
$cart_item['data']->set_price( (float) $cart_item['custom_data']['bestprice'] );
}
}
}
一切似乎都很好,但是我真正需要的是将这个自定义价格字段从后端添加到产品批量编辑中,如图所示 [在此处输入图片说明][3]
感谢您的每一个回答 [1]:https://i.stack.imgur.com/5BIqh.png [2]:https://i.stack.imgur.com/lyxRx.png [3]:https://i.stack.imgur.com/QFbZr.png
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。