这里有一个插件,它可以在其他人需要它时执行此操作。只需将此保存在
<?php
/*
Plugin Name: WC Split Cart Backorders
Plugin URI: https://www.creativemonarchy.com
Description: A simple plugin that helps you split the WooCommerce in stock and backorder stock items for the same product in your cart.
Author: Sohrab Iravani
Version: 1.0
Author URI: https://www.creativemonarchy.com
Requires at least: 4.9
Tested up to: 5.2.3
Text Domain: wc_split_cart_backorder
Domain Path: /languages/
*/
// Split in stock orders and backorders in the cart
add_action( \'woocommerce_add_to_cart\', \'split_bkorders\', 10, 6 );
function split_bkorders( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
foreach ( WC()->cart->get_cart() as $cart_item ) {
$quantity;
$product = $cart_item[\'data\'];
$stock_qty = $product->get_stock_quantity();
$item_qty = $cart_item[\'quantity\'];
if ($quantity > $stock_qty && $stock_qty > 0) {
WC()->cart->set_quantity( $cart_item_key, $stock_qty );
$backorder = $item_qty - $stock_qty;
$cart_item_data[\'unique_key\'] = md5( microtime() . rand() . "xxx" );
$cart_item_data[\'pr_field\'] = \'backorder\';
WC()->cart->add_to_cart( $product_id, 1, $variation_id, $variation, $cart_item_data );
$count = 0;
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ($count == 1) {
WC()->cart->set_quantity( $cart_item_key, $backorder );
break;
}
$count++;
}
} elseif ($stock_qty < 0) {
$cart_item_data = \'backorder\';
WC()->cart->cart_contents[$cart_item_key][\'pr_field\'] = $cart_item_data;
}
break;
}
}
// Add custom field "backorder" to cart item
function plugin_republic_get_item_data( $item_data, $cart_item_data ) {
if( isset( $cart_item_data[\'pr_field\'] ) ) {
$item_data[] = array(
\'key\' => __( \'Type\', \'plugin-republic\' ),
\'value\' => wc_clean( $cart_item_data[\'pr_field\'] )
);
}
return $item_data;
}
// Add custom field "backorder" to everywhere
add_filter( \'woocommerce_get_item_data\', \'plugin_republic_get_item_data\', 10, 2 );
function plugin_republic_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
if( isset( $values[\'pr_field\'] ) ) {
$item->add_meta_data(
__( \'Type\', \'plugin-republic\' ),
$values[\'pr_field\'],
true
);
}
}
add_action( \'woocommerce_checkout_create_order_line_item\', \'plugin_republic_checkout_create_order_line_item\', 10, 4 );
// Disable default backorder notification in cart
function inline_css() {
echo "<style>.backorder_notification{display: none;}</style>";
}
add_action( \'wp_head\', \'inline_css\', 0 );
// Disable quanity edit in cart
add_filter( \'woocommerce_cart_item_quantity\', \'wc_cart_item_quantity\', 10, 3 );
function wc_cart_item_quantity( $product_quantity, $cart_item_key, $cart_item ){
if( is_cart() ){
$product_quantity = sprintf( \'%2$s <input type="hidden" name="cart[%1$s][qty]" value="%2$s" />\', $cart_item_key, $cart_item[\'quantity\'] );
}
return $product_quantity;
}
// Create new order status "Partial Order"
add_filter( \'woocommerce_register_shop_order_post_statuses\', \'bbloomer_register_custom_order_status\' );
function bbloomer_register_custom_order_status( $order_statuses ){
$order_statuses[\'wc-partial-order\'] = array(
\'label\' => _x( \'Partial Order\', \'Order status\', \'woocommerce\' ),
\'public\' => false,
\'exclude_from_search\' => false,
\'show_in_admin_all_list\' => true,
\'show_in_admin_status_list\' => true,
\'label_count\' => _n_noop( \'Partial Order <span class="count">(%s)</span>\', \'Partial Order <span class="count">(%s)</span>\', \'woocommerce\' ),
);
return $order_statuses;
}
add_filter( \'wc_order_statuses\', \'bbloomer_show_custom_order_status\' );
function bbloomer_show_custom_order_status( $order_statuses ) {
$order_statuses[\'wc-partial-order\'] = _x( \'Partial Order\', \'Order status\', \'woocommerce\' );
return $order_statuses;
}
add_filter( \'bulk_actions-edit-shop_order\', \'bbloomer_get_custom_order_status_bulk\' );
function bbloomer_get_custom_order_status_bulk( $bulk_actions ) {
$bulk_actions[\'mark_partial-order\'] = \'Change status to partial order\';
return $bulk_actions;
}
// Update mix order to Partial Order
add_filter( \'woocommerce_thankyou_order_received_text\', \'misha_thank_you_title\', 20, 2 );
function misha_thank_you_title( $thank_you_title, $order ){
$items = $order->get_items();
$backorders = 0;
$order_count = 0;
foreach ( $items as $item_id => $item ) {
if (wc_get_order_item_meta( $item_id, \'Type\', true) == \'backorder\') {
$backorders++;
}
$order_count++;
}
if ($backorders > 0) {
if ($order_count == $backorders) {
$order->update_status(\'backordered\', \'order_note\');
} else {
$order->update_status(\'partial-order\', \'order_note\');
}
}
}
?>