更新过程中我应该使用特定术语吗?
需要提及的是,我在functions.php 文件中使用了这段代码。
add_action( 'manage_product_posts_custom_column', 'custom_product_is_in_stock_column_content', 10, 2 );
function custom_product_is_in_stock_column_content( $column, $product_id ) {
switch ( $column ) {
case 'is_in_stock':
echo '<div class="stock-box">';
echo '<form method="post">';
echo '<input type="number" class="stock-input" name="stock_value" value="' . ( get_post_meta( $product_id, '_stock_status', true ) == 'instock' ? 1 : 0 ) . '" min="0">';
echo '<button type="submit" class="stock-button" name="update_stock">↻</button>';
echo '</form>';
echo '</div>';
break;
}
}
add_action( 'admin_post_update_stock', 'update_product_stock' );
function update_product_stock() {
if ( isset( $_POST['stock_value'] ) && isset( $_POST['update_stock'] ) ) {
$stock_value = intval( $_POST['stock_value'] );
$product_id = isset( $_POST['post_ID'] ) ? intval( $_POST['post_ID'] ) : 0;
if ( $product_id > 0 ) {
$new_status = $stock_value > 0 ? 'instock' : 'outofstock';
wc_update_product_stock_status( $product_id, $new_status );
}
}
wp_safe_redirect( admin_url( 'edit.php?post_type=product' ) );
exit;
}
现在,此功能需要 jQuery / JavaScript / Ajax,以便能够从这些自定义输入字段更新产品库存数量。
代码:
// Add input fields to stock status column
add_action( 'manage_product_posts_custom_column', 'product_stock_quantity_column_content', 10, 2 );
function product_stock_quantity_column_content( $column, $product_id ) {
if ( $column === 'is_in_stock' ) {
global $product;
if( $product->get_manage_stock() ) {
printf('<div style="margin-bottom:5px;width:120px">
<input type="number" name="stock_qty-%d" value="%d" style="width:80px">
<button type="button" class="update-qty button button-primary" data-id="%d">↻</button>
<span class="message-%d" style="display:none"></span></div>',
$product_id, $product->get_stock_quantity('edit'), $product_id, $product_id);
}
}
}
// WP Admin Ajax receiver
add_action('wp_ajax_update_stock_quantity', 'update_stock_quantity_ajax');
function update_stock_quantity_ajax() {
if (isset($_POST['product_id']) && isset($_POST['update_qty'])) {
$product = wc_get_product(intval($_POST['product_id']));
$product->set_stock_quantity(intval($_POST['update_qty']));
echo $product->save(); // We return the product Id when saved, as a response.
}
wp_die(); // Exit silently (Always at the end to avoid an Error 500)
}
// jQuery Ajax
add_action('admin_footer', 'update_stock_quantity_js');
function update_stock_quantity_js() {
global $pagenow, $typenow;
if( 'edit.php' === $pagenow && 'product' === $typenow ) :
?>
<script id="update-stock-qty" type="text/javascript">
jQuery(function($) {
$('body').on('click', 'button.update-qty', function() {
const productID = $(this).data('id'),
updateQty = $('input[name=stock_qty-'+productID+']').val();
$.ajax({
url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
type: 'POST',
data: {
'action': 'update_stock_quantity',
'product_id': productID,
'update_qty': updateQty,
},
success: function(response) {
if ( response > 0 ) {
$('.message-'+response).html(' Updated').css('color','green').fadeIn().delay(1500).fadeOut();
}
},
error: function(error) {
$('.message-'+productID).html(' Error').css('color','green').fadeIn().delay(1500).fadeOut();
console.log(error);
}
});
});
});
</script>
<?php
endif;
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。相关: