Php 在Dokan中设置每个供应商的最低订单金额
我想在我的Dokan商店有一个最低订购量。下面的代码正在执行此工作,但个别供应商无法选择自己的最小值Php 在Dokan中设置每个供应商的最低订单金额,php,wordpress,woocommerce,dokan,Php,Wordpress,Woocommerce,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 page
//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' );
}
}
}
//最小订单值
添加操作('woocommerce\u check\u cart\u items'、'required\u min\u cart\u subtotal\u amount');
所需功能\u最小\u购物车\u小计\u金额(){
//仅在购物车或结帐页面中运行
if(is_cart()| | is_checkout()){
//这里设置最低购物车总金额
$min_总计=200;
//总计(税前和运费前)
$total=WC()->购物车->小计;
//如果购物车总数小于所需的最小值,则添加错误通知
若有($总计)
保留您的额外字段函数,并尝试将其添加到function.php中。虽然有点棘手,但似乎有效:)
add_action('woocommerce_check_cart_items','dokan_minimum_order_amount');
函数dokan\u最小订单金额()
{
$eachVendorCartTotal=array();
$items=WC()->cart->get_cart();
//构建阵列:[供应商id][小计]
foreach($items作为$item=>$value){
$product_id=$values['product_id'];
$product\U QUOTY=$values['QUOTE'];
$product\U price=get\U post\U meta($values['product\U id','U price',true)*$product\U qty;
$vendor\u id=get\u post\u字段('post\u author',$product\u id);
如果(!array_key_存在($vendor_id,$eachvendorcattotal)){
$eachvendocartotal[$vendor\u id]=$product\u price;
}否则{
$sub_total=$product_price+$eachvendocartotal[$vendor_id];
$eachvendocartotal[$vendor\u id]=$sub\u total;
}
}
如果(!空($eachvendocartotal)){
foreach($eachVendorCartTotal作为$vendor\u id=>$value){
$errorMessage=“您当前%s的订单总数为%s-您必须有至少%s的订单才能为该供应商下订单”;
$store\u info=dokan\u get\u store\u info($vendor\u id);
$store_name=$store_info['store_name'];
如果(!空($store_info['minimum_order'])){
$vendor\u minimum=!空($store\u info['minimum\u order'])?$store\u info['minimum\u order']:0;
如果($value<$vendor\u最小值){
if(is_cart()){
wc_打印通知(
sprintf($errorMessage,
$store_name,
wc_价格(价值),
wc_价格(供应商最低价格)
)“错误”
);
}否则{
wc_添加_通知(
sprintf($errorMessage,
$store_name,
wc_价格(价值),
wc_价格(供应商最低价格)
)“错误”
);
}
}
}
}
}
}
//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 );
}
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'
);
}
}
}
}
}
}