Сортировать упаковку и продукты в корзине WooCommerce - PullRequest
2 голосов
/ 22 марта 2020

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

Функциональность следующая:

1 ди sh находится в корзина для покупок, добавлена ​​1 коробка для ланча

2 блюда в корзине, добавлены 2 коробки для обеда

3 блюда в корзине, 3 коробки для обеда добавлены

Есть 3 ланч-бокса, поэтому теперь добавляется 1 пакет

function add_delivery_charge_to_cart( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $lunchbox_id  = 5737; // "LunchBox" to be added to cart
    $pakket_id = 5738; // "Pakket" to be added to cart

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Check if "LunchBox" product is already in cart
        if( $cart_item['data']->get_id() == $lunchbox_id ) {
            $lunchbox_key = $cart_item_key;
            $lunchbox_qty = $cart_item['quantity'];
        }

        // Check if "Pakket" product is already in cart
        if( $cart_item['data']->get_id() == $pakket_id ) {
            $pakket_key = $cart_item_key;
            $pakket_qty = $cart_item['quantity'];
        }       
    }

    // Get total items in cart, counts number of products and quantity per product
    $total_items_in_cart = WC()->cart->get_cart_contents_count();

    // If product "LunchBox" is in cart, we check the quantity to update it if needed
    if ( isset($lunchbox_key) && $lunchbox_qty != $total_items_in_cart ) {
        // Lunchbox total = total_items_in_cart 
        $lunchbox_total = $total_items_in_cart;

        // Isset lunchbox qty, lunchbox total - lunchbox qty
        if ( isset($lunchbox_qty) ) {
            $lunchbox_total = $lunchbox_total - $lunchbox_qty;
        }

        // Isset pakket qty, lunchbox total - pakket qty        
        if ( isset($pakket_qty) ) {
            $lunchbox_total = $lunchbox_total - $pakket_qty;
        } 

        // Set quantity, lunchbox
        $cart->set_quantity( $lunchbox_key, $lunchbox_total );

    } elseif ( !isset($lunchbox_key) && $total_items_in_cart > 0 ) {
        // Product "LunchBox" is not in cart, we add it
        $cart->add_to_cart( $lunchbox_id, $total_items_in_cart );
    }

    // Total items in cart greater than or equal to 3
    if ( $total_items_in_cart >= 3 ) {
        // Pakket total = total_items_in_cart 
        $pakket_total = $total_items_in_cart;

        // Isset lunchbox qty, pakket total - lunchbox qty
        if ( isset($lunchbox_qty) ) {
            $pakket_total = $pakket_total - $lunchbox_qty;
        }

        // Isset pakket qty, pakket total - pakket qty      
        if ( isset($pakket_qty) ) {
            $pakket_total = $pakket_total - $pakket_qty;
        }       


        // Pakket total = pakket_total / 3 = floor(result)
        // Floor = round fractions down, rounding result down
        $pakket_total = floor( $pakket_total / 3 );

        // If product "Pakket" is in cart
        if ( isset($pakket_key) ) {         
            $cart->set_quantity( $pakket_key, $pakket_total );
        } elseif ( !isset($pakket_key) ) {
            // Product "Pakket" is not in cart, we add it
            $cart->add_to_cart( $pakket_id, $pakket_total );
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'add_delivery_charge_to_cart', 10, 1 );

Есть небольшая проблема. Вся автоматически добавляемая упаковка в корзину сортируется в смеси с другими продуктами. Как сделать упаковку в корзину всегда внизу списка товаров?

По алфавиту сортировка АЗ не подходит:

add_action( 'woocommerce_cart_loaded_from_session', 'my_sort_cart_items_alphabetically' ); 
function my_sort_cart_items_alphabetically() {

// READ CART ITEMS
$products_in_cart = array();
foreach ( WC()->cart->get_cart_contents() as $key => $item ) {
$products_in_cart[ $key ] = $item['data']->get_title();
}

// SORT CART ITEMS
natsort( $products_in_cart );

// ASSIGN SORTED ITEMS TO CART
$cart_contents = array();
foreach ( $products_in_cart as $cart_key => $product_title ) {
$cart_contents[ $cart_key ] = WC()->cart->cart_contents[ $cart_key ];
}
WC()->cart->cart_contents = $cart_contents;
}

Буду рад вашей помощи!

1 Ответ

2 голосов
/ 22 марта 2020

Идентификаторы товаров, добавленные в массив, появятся внизу списка

function sort_cart_specific_product_at_bottom( $cart ) {    
    // Product id's to to display at tbe bottom of the product list
    $product_ids_last = array( 30, 815 );

    // Set empty arrays
    $products_in_cart = array();
    $products_last = array();
    $cart_contents = array();

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Get product id
        $product_id = $cart_item['data']->get_id();

        // In_array — checks if a value exists in an array
        if ( in_array( $product_id, $product_ids_last) ) {
            // Add to products last array
            $products_last[ $cart_item_key ] = $product_id;
        } else {
            // Add to products in cart array
            $products_in_cart[ $cart_item_key ] = $product_id;
        }
    }

    // Merges the elements together so that the values of one are appended to the end of the previous one.
    $products_in_cart = array_merge( $products_in_cart, $products_last );

    // Assign sorted items to cart
    foreach ( $products_in_cart as $cart_item_key => $product_id ) {
        $cart_contents[ $cart_item_key ] = $cart->cart_contents[ $cart_item_key ];
    }

    // Cart contents
    $cart->cart_contents = $cart_contents;

}
add_action( 'woocommerce_cart_loaded_from_session', 'sort_cart_specific_product_at_bottom', 10, 1 );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...