wordpress 根据另一个产品的数量从WooCommerce迷你购物车中删除产品

f0ofjuux  于 2023-03-29  发布在  WordPress
关注(0)|答案(1)|浏览(151)

如果客户从WooCommerce迷你购物车中删除另一个产品,我试图将产品X从WooCommerce迷你购物车中删除,因此不再满足产品X在购物车中的条件。
条件是在购物车中具有至少数量8的产品,其被分配给产品类别15和51。
我得到了它的购物车页面的工作,但我似乎不能找出如何删除产品从迷你车后,用户更新它和条件不再满足。
也许有人可以帮助我实现这一点的基础上的代码,我已经尝试?
我的小推车使用 AJAX ,所以我尝试了这个:

function bierfass_ajax_product_remove() {
    // Get mini cart
    ob_start();

    // Get Bierfass product
    $product_x_id = 13387;

    // Get WooCommerce cart object
    $cart = WC()->cart;

    // Initialize flag variable
    $conditions_met = false;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        if ( has_term( '51', 'product_cat', $cart_item['product_id'] ) && has_term( '15', 'product_cat', $cart_item['product_id'] ) ) {
            if ( $cart_item['quantity'] >= 8 ) {
                $conditions_met = true;
                break; // exit the loop bc we found the what we're looking for
            }
        }
    }

    // Check if conditions are met, else remove product X from cart
    if ( $conditions_met == false ) {
        foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
            if ( $cart_item['product_id'] == $product_x_id ) {
                $cart->remove_cart_item( $cart_item_key );
                break;
            }
        }
    }

    WC()->cart->calculate_totals();
    WC()->cart->maybe_set_cart_cookies();

    woocommerce_mini_cart();

    $mini_cart = ob_get_clean();

    // Fragments and mini cart are returned
    $data = array(
        'fragments' => apply_filters( 'woocommerce_add_to_cart_fragments', array(
            'div.widget_shopping_cart_content' => '<div class="widget_shopping_cart_content">' . $mini_cart . '</div>'
            )
        ),
        'cart_hash' => apply_filters( 'woocommerce_add_to_cart_hash', WC()->cart->get_cart_for_session() ? md5( json_encode( WC()->cart->get_cart_for_session() ) ) : '', WC()->cart->get_cart_for_session() )
    );

    wp_send_json( $data );

    die();

    }

    add_action( 'wp_ajax_product_remove', 'my_ajax_product_remove' );
    add_action( 'wp_ajax_nopriv_product_remove', 'my_ajax_product_remove' );

使用这个js:

$(document).on('click', '.mini_cart_item a.remove', function (e) {
    e.preventDefault();

    var product_id = $(this).attr("data-product_id"),
        cart_item_key = $(this).attr("data-cart_item_key"),
        product_container = $(this).parents('.mini_cart_item');

        // Add loader
    product_container.block({
        message: null,
        overlayCSS: {
            cursor: 'none'
        }
    });

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: wc_add_to_cart_params.ajax_url,
        data: {
        action: "product_remove",
        product_id: product_id,
            cart_item_key: cart_item_key
        },
        success: function (response) {
            if (!response || response.error)
            return;

            var fragments = response.fragments;

             // Replace fragments
            if (fragments) {
                $.each(fragments, function (key, value) {
                    $(key).replaceWith(value);
                });
            }

            // Update cart
            $(document.body).trigger('wc_update_cart');
        }
    });

});

但这根本不起作用。
这是我用来从购物车页面的购物车中删除产品X的代码(工作):

function check_cart_after_update() {

    // Get product X
    $product_x_id = 13387;

    // Get WooCommerce cart object
    $cart = WC()->cart;

    // Initialize flag variable
    $conditions_met = false;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        if ( has_term( '51', 'product_cat', $cart_item['product_id'] ) && has_term( '15', 'product_cat', $cart_item['product_id'] ) ) {
            if ( $cart_item['quantity'] >= 8 ) {
                $conditions_met = true;
                break; // exit the loop bc we found the what we're looking for
            }
        }
    }

    // Check if conditions are met, else remove product X from cart
    if ( $conditions_met == false ) {
        foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
            if ( $cart_item['product_id'] == $product_x_id ) {
                $cart->remove_cart_item( $cart_item_key );
                break;
            }
        }
    }

}
// Run the check_cart_after_update() function on the cart and checkout pages
add_action( 'woocommerce_before_cart', 'check_cart_after_update' );
add_action( 'woocommerce_before_checkout_form', 'check_cart_after_update' );
r7xajy2e

r7xajy2e1#

算了,我很蠢。
我可以直接把这个添加到我的工作代码中:

add_action( 'check_cart_after_update', 'check_cart_after_update', 10, 1 );

相关问题