【问题标题】:Auto remove out of stock items in cart then notify account user - Woocommerce自动删除购物车中的缺货商品,然后通知帐户用户 - Woocommerce
【发布时间】:2019-01-07 02:08:30
【问题描述】:

我希望允许客户将商品添加到他们的 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 中“开箱即用”发生。有人可以确认这一点或提供一种方法吗?

谢谢你, 布莱恩

【问题讨论】:

  • 嗨,欢迎来到 Stack Overflow。您要求推荐的问题以及如何使 Stack Overflow 上的任何其他用户都非常模糊。请参考How to ask 并包括你做了什么,你卡在哪里以及你想要它做什么。谢谢=)
  • "我的客户希望" 如果您无法为您的客户进行特定的开发工作(由于缺乏知识或其他原因),您可以为您的客户做的最好的事情就是告诉他们,这样他们就可以聘请能力的开发人员。你只会浪费你和他们的时间。
  • 谢谢@Minial 我是这个网站的新手,所以将来会尝试提供更多细节。已编辑我的原始问题以遵守。希望现在更适合。 :)
  • @JustinR。这不是向该站点的新成员致意的理想方式。有人告诉我,stackoverflow 是寻求编程需求帮助的最佳场所,但是当我第一次尝试寻求帮助时,我被猛烈抨击了。或许提供一个价格来解决我的问题会是一个更好的问候。
  • 人们每天都来 SO 期待其他人免费为他们编写所有代码。一般来说,更多的人从事他们无法做的开发工作。我的声明适用于任何从事 Web 开发业务的人。这不是个人的。正如您最初发布的问题一样,它不符合 SO 社区准则。它缺乏代码,也没有表现出任何研究努力(因此反对票的数量)。与这里的大多数其他人一样,我很乐意在可能的情况下免费帮助回答问题,但我来这里并不是为了完全完成其他人的工作。

标签: php wordpress hook-woocommerce


【解决方案1】:

我已经弄清楚我哪里错了,我完全走错了路。我在下面发布正确的代码,以防其他人将来需要此功能。它两者兼而有之,在产品缺货时更新购物车并同时留下新消息。

/**
 * 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/ 的 Svetoslav Marinov 直接与我联系并提供他的意见。是他带领我走向了正确的方向。

如果您发现任何改进,请随时更新此代码。

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 2018-10-19
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    相关资源
    最近更新 更多