我希望允许客户将物品添加到他们的Woocommerce购物车中,并根据需要将其保留在那里,以便他们可以在闲暇时添加到购物车中。任何缺货的生产线都需要自动从购物车中删除,并显示一条消息以表明这种情况已发生。例如"所有缺货商品已从帐户中删除,因为它们不再可用"。
到目前为止,我已经尝试过这个
public function is_in_stock() {
return apply_filters( 'woocommerce_product_is_in_stock', 'instock' === $this->get_stock_status(), $this );
}
function notes_in_cart() {
global $woocommerce;
if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {
return;
}
if ( isset( $_POST['post_data'] ) ) {
parse_str( $_POST['post_data'], $post_data );
} else {
$post_data = $_POST; // fallback for final checkout (non-ajax)
}
if ( WC()->cart->needs_shipping() ){
// set $out_of_stock_exists to false by default
$out_of_stock_exists = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if($values['data']->backorders_allowed()){ //check if backorders are allowed on this product
// get the stock quantity - returns the available amount number
$stock_info = $values['data']->get_stock_quantity();
if($stock_info < $values['quantity']){
set $out_of_stock_exists to true and stop foreach execution
$out_of_stock_exists = true;
break;
}
}
}
//if cart has items out of stock
if ($out_of_stock_exists) {
?>
<tr class="ceckoutStockMeta">
<th>Item Shipments</th>
<td>
<p style="color: red;">*All out of stock items have been removed from your cart as they are no longer available.</p><br>
<form>
<input type="radio" name="stockOp" id="stockOption1" value="ship" />
<label for="stockOption1">Ship what is available now</label><br>
<input type="radio" name="stockOp" id="stockOption2" value="hold" />
<label for="stockOption2">Wait and ship together</label>
</form>
</td>
</tr>
<?php
}
}
add_action( 'woocommerce_cart_totals_after_order_total', 'notes_in_cart' );
add_action( 'woocommerce_review_order_after_order_total', 'notes_in_cart' );
我不确定所有这些是否有必要,考虑到无论如何都需要禁止延期交货。
有人可以告诉我这是否正确吗?
至于从帐户购物车中自动删除缺货行,我猜这将在Woocommerce上"开箱即用"发生。有人可以确认这一点或提供一种方法吗?
谢谢布莱恩
我已经弄清楚我哪里出错了,我完全走错了轨道。我在下面发布了正确的代码,以防将来其他人需要此功能。它两者兼而有之,在产品缺货时更新购物车,同时留下新消息。
/**
* This code will automatically remove any out of stock items from a shopping cart.
* This would be in cases when users add products to their cart and come back to it later.
*
*/
function orb_check_for_out_of_stock_products() {
if ( WC()->cart->is_empty() ) {
return;
}
$removed_products = [];
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_obj = $cart_item['data'];
if ( ! $product_obj->is_in_stock() ) {
WC()->cart->remove_cart_item( $cart_item_key );
$removed_products[] = $product_obj;
}
}
if (!empty($removed_products)) {
wc_clear_notices(); // remove any WC notice about sorry about out of stock products to be removed from cart.
foreach ( $removed_products as $idx => $product_obj ) {
$product_name = $product_obj->get_title();
$msg = sprintf( __( "The product '%s' was removed from your cart because it is now out of stock. Sorry for any inconvenience caused.", 'woocommerce' ), $product_name);
wc_add_notice( $msg, 'error' );
}
}
}
add_action('woocommerce_before_cart', 'orb_check_for_out_of_stock_products');
我感谢那些提供帮助的人,并非常感谢 http://orbisius.com/的斯维托斯拉夫·马里诺夫直接与我联系。是他带领我走向正确的方向。
请随时使用您可以确定的任何改进来更新此代码。