将缺货通知替换为 WooCommerce 购物车页面上每个购物车商品的缺货数量
Replace backorder notification with number of backorders for each cart item on WooCommerce cart page
我正在尝试在 WooCommerce 的购物车页面上编辑延期交货通知,以便显示每个购物车商品的延期交货数量(我正在使用变体)。
因此,如果购物车商品的数量为 7,但库存为 5,则延期交货应为 2。(此信息默认显示在 Woocommerce 的订单电子邮件中,但在下订单前不会显示在购物车页面上).
只要购物车中只有 1 件商品,下面的代码就可以正常工作。但是,如果购物车商品多于 1 件,则所有有延期交货的购物车商品都会返回相同数量的延期交货商品。
有谁知道我应该更改什么,以便延期交货通知将为每个有延期交货的购物车商品显示正确的延期交货数量 - 无论购物车中有多少有延期交货的购物车商品?
我正在使用过滤器挂钩 woocommerce_cart_item_backorder_notification
function backorder_info() {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Get an instance of the WC_Product Object
$product = $cart_item['data'];
// Get stock quantity
$stock_qty = $product->get_stock_quantity();
// get cart item quantity
$item_qty = $cart_item['quantity'];
// Calculate number of backorders
$backorder_qty = $item_qty - $stock_qty;
$html = '<p class="backorder_notification">' .__( " " .$backorder_qty. " i restordre","woocommerce").'</p>';
// return backorder quantity
return $html;
}
}
// Display backorder notification on cart page
add_filter( 'woocommerce_cart_item_backorder_notification', 'custom_cart_item_backorder_notification', 10, 2 );
function custom_cart_item_backorder_notification($html, $product_id) {
$html = backorder_info();
return $html;
}
如您所见,woocommerce_cart_item_backorder_notification
过滤器挂钩包含 productID
作为第二个参数。
只有这个是 parentID
,因此对于变量产品,确定购物车中当前有哪个变体 (variantID
) 并不容易。
这就是为什么我们要首先通过以下代码确保不是 parentID
而是真正的 productID
被传递给 woocommerce_cart_item_backorder_notification
过滤器钩子
function filter_woocommerce_cart_item_product_id( $cart_item_product_id, $cart_item, $cart_item_key ) {
// Return real product ID instead of parent ID
$cart_item_product_id = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
return $cart_item_product_id;
}
add_filter( 'woocommerce_cart_item_product_id', 'filter_woocommerce_cart_item_product_id', 10, 3 );
然后你得到,根据延期交货数量
更改backorder_notification
function custom_cart_item_backorder_notification( $html, $product_id ) {
// Get cart items quantities
$cart_item_quantities = WC()->cart->get_cart_item_quantities();
// Product quantity in cart
$product_qty_in_cart = isset( $cart_item_quantities[ $product_id ] ) ? $cart_item_quantities[ $product_id ] : 0;
// Get product
$product = wc_get_product( $product_id );
// Get stock quantity
$stock_qty = $product->get_stock_quantity();
// Still in stock
if ( $stock_qty >= 1 ) {
// Calculate number of backorders
$backorder_qty = $product_qty_in_cart - $stock_qty;
} else {
// Calculate number of backorders
$backorder_qty = $product_qty_in_cart;
}
// Output
$html = '<p class="backorder_notification">' . sprintf( __( '%s in backorder', 'woocommerce' ), $backorder_qty ) . '</p>';
return $html;
}
add_filter( 'woocommerce_cart_item_backorder_notification', 'custom_cart_item_backorder_notification', 10, 2 );
我正在尝试在 WooCommerce 的购物车页面上编辑延期交货通知,以便显示每个购物车商品的延期交货数量(我正在使用变体)。
因此,如果购物车商品的数量为 7,但库存为 5,则延期交货应为 2。(此信息默认显示在 Woocommerce 的订单电子邮件中,但在下订单前不会显示在购物车页面上).
只要购物车中只有 1 件商品,下面的代码就可以正常工作。但是,如果购物车商品多于 1 件,则所有有延期交货的购物车商品都会返回相同数量的延期交货商品。
有谁知道我应该更改什么,以便延期交货通知将为每个有延期交货的购物车商品显示正确的延期交货数量 - 无论购物车中有多少有延期交货的购物车商品?
我正在使用过滤器挂钩 woocommerce_cart_item_backorder_notification
function backorder_info() {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Get an instance of the WC_Product Object
$product = $cart_item['data'];
// Get stock quantity
$stock_qty = $product->get_stock_quantity();
// get cart item quantity
$item_qty = $cart_item['quantity'];
// Calculate number of backorders
$backorder_qty = $item_qty - $stock_qty;
$html = '<p class="backorder_notification">' .__( " " .$backorder_qty. " i restordre","woocommerce").'</p>';
// return backorder quantity
return $html;
}
}
// Display backorder notification on cart page
add_filter( 'woocommerce_cart_item_backorder_notification', 'custom_cart_item_backorder_notification', 10, 2 );
function custom_cart_item_backorder_notification($html, $product_id) {
$html = backorder_info();
return $html;
}
如您所见,woocommerce_cart_item_backorder_notification
过滤器挂钩包含 productID
作为第二个参数。
只有这个是 parentID
,因此对于变量产品,确定购物车中当前有哪个变体 (variantID
) 并不容易。
这就是为什么我们要首先通过以下代码确保不是 parentID
而是真正的 productID
被传递给 woocommerce_cart_item_backorder_notification
过滤器钩子
function filter_woocommerce_cart_item_product_id( $cart_item_product_id, $cart_item, $cart_item_key ) {
// Return real product ID instead of parent ID
$cart_item_product_id = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
return $cart_item_product_id;
}
add_filter( 'woocommerce_cart_item_product_id', 'filter_woocommerce_cart_item_product_id', 10, 3 );
然后你得到,根据延期交货数量
更改backorder_notification
function custom_cart_item_backorder_notification( $html, $product_id ) {
// Get cart items quantities
$cart_item_quantities = WC()->cart->get_cart_item_quantities();
// Product quantity in cart
$product_qty_in_cart = isset( $cart_item_quantities[ $product_id ] ) ? $cart_item_quantities[ $product_id ] : 0;
// Get product
$product = wc_get_product( $product_id );
// Get stock quantity
$stock_qty = $product->get_stock_quantity();
// Still in stock
if ( $stock_qty >= 1 ) {
// Calculate number of backorders
$backorder_qty = $product_qty_in_cart - $stock_qty;
} else {
// Calculate number of backorders
$backorder_qty = $product_qty_in_cart;
}
// Output
$html = '<p class="backorder_notification">' . sprintf( __( '%s in backorder', 'woocommerce' ), $backorder_qty ) . '</p>';
return $html;
}
add_filter( 'woocommerce_cart_item_backorder_notification', 'custom_cart_item_backorder_notification', 10, 2 );