WooCommerce:额外库存位置
已发表: 2022-12-07WooCommerce 插件允许您管理每个产品的库存,但您只有一个库存数量字段!
如果您有两个仓库,并且作为商店管理员,需要管理每个位置的库存怎么办? 此外,如果某件商品在位置 1 缺货,但在位置 2 有库存,因此客户需要能够购买它怎么办?
这个惊人的解决方法将在产品设置中添加第二个输入数字,通过将库存 1 + 库存 2 相加来重新定义前端的库存数量和状态,最后减少库存 1 直到它变为 0,之后它将减少库存 2。
当然可以更改此默认行为,例如,可以通过附加代码定义从何处减少库存(按距离?)。 此外,还可以编写额外的代码以使其与可变产品或自定义产品类型兼容,并使其与退款一起使用。 无论哪种方式,享受!
PHP 代码段:第二库存位置管理
/**
* @snippet Second Stock Location @ WooCommerce Edit Product
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 7
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_action( 'woocommerce_product_options_stock', 'bbloomer_additional_stock_location' );
function bbloomer_additional_stock_location() {
global $product_object;
echo '<div class="show_if_simple show_if_variable">';
woocommerce_wp_text_input(
array(
'id' => '_stock2',
'value' => get_post_meta( $product_object->get_id(), '_stock2', true ),
'label' => '2nd Stock Location',
'data_type' => 'stock',
)
);
echo '</div>';
}
add_action( 'save_post_product', 'bbloomer_save_additional_stock' );
function bbloomer_save_additional_stock( $product_id ) {
global $typenow;
if ( 'product' === $typenow ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( isset( $_POST['_stock2'] ) ) {
update_post_meta( $product_id, '_stock2', $_POST['_stock2'] );
}
}
}
add_filter( 'woocommerce_product_get_stock_quantity' , 'bbloomer_get_overall_stock_quantity', 9999, 2 );
function bbloomer_get_overall_stock_quantity( $value, $product ) {
$value = (int) $value + (int) get_post_meta( $product->get_id(), '_stock2', true );
return $value;
}
add_filter( 'woocommerce_product_get_stock_status' , 'bbloomer_get_overall_stock_status', 9999, 2 );
function bbloomer_get_overall_stock_status( $status, $product ) {
if ( ! $product->managing_stock() ) return $status;
$stock = (int) $product->get_stock_quantity() + (int) get_post_meta( $product->get_id(), '_stock2', true );
$status = $stock && ( $stock > 0 ) ? 'instock' : 'outofstock';
return $status;
}
add_filter( 'woocommerce_payment_complete_reduce_order_stock', 'bbloomer_maybe_reduce_second_stock', 9999, 2 );
function bbloomer_maybe_reduce_second_stock( $reduce, $order_id ) {
$order = wc_get_order( $order_id );
$atleastastock2change = false;
foreach ( $order->get_items() as $item ) {
if ( ! $item->is_type( 'line_item' ) ) {
continue;
}
$product = $item->get_product();
$item_stock_reduced = $item->get_meta( '_reduced_stock', true );
if ( $item_stock_reduced || ! $product || ! $product->managing_stock() ) {
continue;
}
$qty = apply_filters( 'woocommerce_order_item_quantity', $item->get_quantity(), $order, $item );
$stock1 = (int) get_post_meta( $product->get_id(), '_stock', true );
if ( $qty <= $stock1 ) continue;
$atleastastock2change = true;
}
if ( ! $atleastastock2change ) return $reduce;
foreach ( $order->get_items() as $item ) {
if ( ! $item->is_type( 'line_item' ) ) {
continue;
}
$product = $item->get_product();
$item_stock_reduced = $item->get_meta( '_reduced_stock', true );
if ( $item_stock_reduced || ! $product || ! $product->managing_stock() ) {
continue;
}
$item_name = $product->get_formatted_name();
$qty = apply_filters( 'woocommerce_order_item_quantity', $item->get_quantity(), $order, $item );
$stock1 = (int) get_post_meta( $product->get_id(), '_stock', true );
$stock2 = (int) get_post_meta( $product->get_id(), '_stock2', true );
if ( $qty <= $stock1 ) {
wc_update_product_stock( $product, $qty, 'decrease' );
$order->add_order_note( sprintf( 'Reduced stock for item "%s"; Stock 1: "%s" to "%s".', $item_name, $stock1, $stock1 - $qty ) );
} else {
$newstock2 = $stock2 - ( $qty - $stock1 );
wc_update_product_stock( $product, $stock1, 'decrease' );
update_post_meta( $product->get_id(), '_stock2', $newstock2 );
$item->add_meta_data( '_reduced_stock', $qty, true );
$item->save();
$order->add_order_note( sprintf( 'Reduced stock for item "%s"; Stock 1: "%s" to "0" and Stock 2: "%s" to "%s".', $item_name, $stock1, $stock2, $newstock2 ) );
}
}
$order->get_data_store()->set_stock_reduced( $order_id, true );
return false;
}
用简单的英语:
- bbloomer_additional_stock_location显示第二个库存数量字段
- bbloomer_save_additional_stock保存自定义库存量
- bbloomer_get_overall_stock_quantity设置产品库存库存为stock 1 + stock 2
- bbloomer_get_overall_stock_status根据库存1+库存2设置产品库存状态
- bbloomer_maybe_reduce_second_stock从库存 1 减少库存,然后从库存 2 减少库存,以防订购数量大于库存 1,否则它让 WooCommerce 进行默认库存减少