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 進行默認庫存減少