Reputation: 31
I want to have a minimum order amount in my Dokan store. The following code is doing the work but individual vendors can't select their own minimum value.
//minimum order value
add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
// HERE Set minimum cart total amount
$min_total = 200;
// Total (before taxes and shipping charges)
$total = WC()->cart->subtotal;
// Add an error notice is cart total is less than the minimum required
if( $total <= $min_total ) {
// Display an error message
wc_add_notice( '<strong>' . sprintf( __("A minimum total purchase amount of %s is required to checkout."), wc_price($min_total) ) . '<strong>', 'error' );
}
}
}
So I added a custom field to the Dokan settings page
//Extra field on the seller settings and show the value on the store banner -Dokan
// Add an extra field in seller settings
add_filter( 'dokan_settings_form_bottom', 'extra_fields', 10, 2);
function extra_fields( $current_user, $profile_info ){
$minimum_order= isset( $profile_info['minimum_order'] ) ? $profile_info['minimum_order'] : '';
?>
<div class="gregcustom dokan-form-group">
<label class="dokan-w3 dokan-control-label" for="setting_address">
<?php _e( 'Minimum order value', 'dokan' ); ?>
</label>
<div class="dokan-w5">
<input type="number" class="dokan-form-control input-md valid" name="minimum_order" id="reg_minimum_order" value="<?php echo $minimum_order; ?>" />
</div>
</div>
<?php
}
//save the field value
add_action( 'dokan_store_profile_saved', 'save_extra_fields', 15 );
function save_extra_fields( $store_id ) {
$dokan_settings = dokan_get_store_info($store_id);
if ( isset( $_POST['minimum_order'] ) ) {
$dokan_settings['minimum_order'] = $_POST['minimum_order'];
}
update_user_meta( $store_id, 'dokan_profile_settings', $dokan_settings );
}
Now I want some code to use in here,
$min_total = 200;
to get vendor id from items of the cart and use it to get user meta and to show that minimum_order value in above. (200) Sorry for my bad English.
Upvotes: 3
Views: 1478
Reputation: 31
Keep your extra field function and try to add this in your function.php. Just a little tricky but seems to work :)
add_action( 'woocommerce_check_cart_items', 'dokan_minimum_order_amount' );
function dokan_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_info = dokan_get_store_info($vendor_id);
$store_name = $store_info['store_name'];
if(!empty($store_info['minimum_order'])) {
$vendor_minimum = !empty($store_info['minimum_order']) ? $store_info['minimum_order'] : 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'
);
}
}
}
}
}
}
Upvotes: 3