我想在Dokan商店中获得最低订购量。以下代码可以完成工作,但是各个供应商无法选择自己的最小值。
//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' );
}
}
}
所以我在Dokan设置页面上添加了一个自定义字段
//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 );
}
现在,我想在这里使用一些代码,
$ min_total = 200;
从购物车中的物品获取供应商ID,并使用它获取用户元数据并在上方显示minimum_order值。 (200) 对不起,我的英语不好。
答案 0 :(得分:1)
保留额外的字段函数,并尝试将其添加到function.php中。只是有点棘手,但似乎可以::
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'
);
}
}
}
}
}
}