php - 在 Dokan 中设置每个供应商的最小订单量
问题描述
我想在我的 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) 对不起我的英语不好。
解决方案
保留您的额外字段函数并尝试将其添加到您的 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'
);
}
}
}
}
}
}
推荐阅读
- python - 在 python 中导入集群
- javascript - 如何分离嵌套的异步函数并以所需的顺序执行它们?
- python-3.x - Python:检查字符串是否在列表中,否则抛出错误
- ffmpeg - 添加图像叠加层时如何将较少的参数传递给ffmpeg
- javascript - 使用 Google 地球引擎导出 NDVI 数据未按预期工作
- sorting - 使用 lodash 按包含数组对数组对象进行排序,并找到索引数组
- javascript - ReferenceError:卡未定义
- driverless-ai - 可以产生贡献的模型
- python - 将连接的组件分离到多个图像
- qt - Qt - QOpnGLWidget 和普通 QWidget 的性能差异