我希望允许客户将商品添加到其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中“开箱即用”发生。有人可以确认一下还是提供一种方式?
谢谢你, 布莱恩
答案 0 :(得分:0)
无论如何,从那以后我就弄清楚了我要去哪里错了,而我完全走错了路。我在下面发布了正确的代码,以防将来其他人需要此功能。两者兼而有之;缺货时更新购物车并同时留下新消息。
/**
* 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 it 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/的Svetoslav Marinov的帮助和帮助,感谢他们直接与我联系并提供了他的意见。是他带领我朝着正确的方向前进。
祝大家有美好的一天!随意更新此代码,并找出可以发现的任何改进。