dongyuzhu2244 2019-01-07 02:08
浏览 33

自动删除购物车中的缺货商品,然后通知帐户用户 - Woocommerce

I wish to allow customers to add items to their Woocommerce cart and leave it there for as long as they want, so they can add to the cart at their leisure. Any lines that go out of stock need to automatically be removed from the cart and a message to show that this has occurred. Something like "All out of stock items have been removed from the account as they are no longer available".

So far I have tried this

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' );

I am not sure if all of this is necessary, considering backorders need to be disallowed anyway.

Can someone tell me if this is correct?

As for auto removal of out of stock lines from an account's cart, I am guessing that this will happen 'out of the box' with Woocommerce. Can someone please confirm this or provide a way to do it?

Thank you, Bryan

  • 写回答

1条回答 默认 最新

  • doupixian1436 2019-01-10 00:16
    关注

    Anyway, I have since figured out where I was going wrong and I was totally on the wrong track. I am posting the correct code below, in case anyone else needs this functionality in the future. It does both; updates the cart when out of stock and leaves a new message at the same time.

    /**
     * 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');
    

    My thanks to those who offered help and a big thank you to Svetoslav Marinov from http://orbisius.com/ for contacting me directly with his input. It was he that lead me in the right direction.

    Have a great day everyone! Fell free to update this code with any improvements you can identify.

    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题