Удалить определенный элемент корзины на основе минимального промежуточного итога в корзине Woocommerce и оформить заказ - PullRequest
0 голосов
/ 15 октября 2018

В Woocommerce я пытаюсь автоматически удалить подарочный продукт из корзины, если в корзине менее 300 000 донгов.

Вот мой код:

add_action( 'template_redirect', 'remove_product_from_cart' );
function remove_product_from_cart() {
// Run only in the Cart or Checkout Page
     global $woocommerce;
     $current =  WC()->cart->cart_contents_total;
    $min_amount= 300000;
     $prod_to_remove = 357;
if ( $current < $min_amount) {
if( is_cart() || is_checkout() ) {

    // Cycle through each product in the cart
    foreach( WC()->cart->cart_contents as $prod_in_cart ) {
        // Get the Variation or Product ID
        $prod_id = ( isset( $prod_in_cart['variation_id'] ) && $prod_in_cart['variation_id'] != 0 ) ? $prod_in_cart['variation_id'] : $prod_in_cart['product_id'];
        // Check to see if IDs match
        if( $prod_to_remove == $prod_id ) {
            // Get it's unique ID within the Cart
            $prod_unique_id = WC()->cart->generate_cart_id( $prod_id );
            // Remove it from the cart by un-setting it
            unset( WC()->cart->cart_contents[$prod_unique_id] );
         wc_add_notice( __( 'Quà tặng của bạn đã bị xóa khỏi giỏ hàng vì ' ), 'notice' );
        }
    }
}
}}

Но проблема в том,что он не работает с изменениями продукта.

Любая помощь приветствуется.

1 Ответ

0 голосов
/ 16 октября 2018

Uptaded (для обработки нескольких элементов, подлежащих удалению) .

Попробуйте следующий пересмотренный код, который также будет лучше работать с вариациями продукта:

add_action( 'woocommerce_check_cart_items', 'conditionally_remove_specific_cart_item' );
function conditionally_remove_specific_cart_item() {
    ## -- Settings -- ##
    $min_required   = 300000; // The minimal required cart subtotal
    $items_to_remove = array( 446, 27 ); // Product Ids to be removed from cart

    // Only if cart subtotal is less than 300000
    if( WC()->cart->cart_contents_total >= $min_required )
        return;

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        if( array_intersect( $items_to_remove, array( $cart_item['variation_id'], $cart_item['product_id'] ) ) ) {
            // Remove cart item
            WC()->cart->remove_cart_item( $cart_item_key );

            // Add a custom  notice
            wc_add_notice( sprintf(
            __( "Your gift has been removed from the cart because its requires a minimal cart amount of %s", "woocommerce" ),
            $min_required ), 'notice' );
        }
    }
}

Код помещается в файл function.php вашей активной дочерней темы (или активной темы).Проверено и работает.

В корзине:

enter image description here

На странице оформления заказа:

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...