Untitled

 avatar
unknown
plain_text
2 years ago
2.1 kB
6
Indexable
add_action( 'woocommerce_check_cart_items', 'minimum_order_amount' );
function minimum_order_amount()
{

    $eachVendorCartTotal = array();
    $items = WC()->cart->get_cart();

    //build the array: [vendor_id][sub_total]
    foreach ($items as $item => $values) {

        $product_id = $values['product_id'];
        $product_qty = $values['quantity'];
        $product_price = get_post_meta($values['product_id'], '_price', true) * $product_qty;
        $vendor_id = get_post_field('post_author', $product_id);

        if (!array_key_exists($vendor_id, $eachVendorCartTotal)) {
            $eachVendorCartTotal[$vendor_id] = $product_price;
        } else {
            $sub_total = $product_price + $eachVendorCartTotal[$vendor_id];
            $eachVendorCartTotal[$vendor_id] = $sub_total;
        }

    }

    if (!empty($eachVendorCartTotal)) {
        foreach ($eachVendorCartTotal as $vendor_id => $value) {
            $errorMessage = "Your current order total for %s is %s — you must have an order with a minimum of %s to place your order for this vendor";

            $store_name = marketking()->get_store_name_display($vendor_id);
            $minimum_store_value = 1000;

            if(!empty($minimum_store_value)) {
                $vendor_minimum = !empty($minimum_store_value) ? $minimum_store_value : 0;
                if ($value < $vendor_minimum) {
                    if (is_cart()) {

                        wc_print_notice(
                            sprintf($errorMessage,
                                $store_name,
                                wc_price($value),
                                wc_price($vendor_minimum)
                            ), 'error'
                        );

                    } else {
                        wc_add_notice(
                            sprintf($errorMessage,
                                $store_name,
                                wc_price($value),
                                wc_price($vendor_minimum)
                            ), 'error'
                        );
                    }
                }
            }
        }
    }
}
Editor is loading...