在 WooCommerce Dokan 多供应商商店中,我为客户提供货到付款 (COD) 付款。
我创建了一个代码来计算购物车中的供应商,并将它们与我想要每个供应商的费用相乘。在我的示例中,每个供应商 2 欧元。所以假设我们在购物车上有每个供应商的 1 个产品(现在我们有 2 个供应商)。那应该是 2 * 2 = 4 欧元 COD 的总成本。
这是完美的工作,但是当我收到订单时,我只在主订单中看到费用,而不是在子订单中。在一个子订单中应该是 2 欧元,在另一个子订单中应该是 2 欧元。
这一直有效,但自 2021 年 2 月 11 日起,它突然停止了。有什么可以帮助我的想法吗?
这是我正在使用的代码:
// 2 € Fee COD - Add a custom fee based on cart subtotal:
add_action( 'woocommerce_cart_calculate_fees', 'custom_fee_for_dokan', 999, 1 );
function custom_fee_for_dokan ( $cart ) {
$car_items = WC()->cart->get_cart(); // Cart items
$items_sort = array(); // Initializing
// Loop through cart items
foreach ( $car_items as $cart_item_key => $cart_item ) {
// Get the vendor_id
$vendor_id = get_post_field( 'post_author', $cart_item['product_id'] );
$store_info = dokan_get_store_info( $vendor_id ); // Get the store data
$store_name = $store_info['store_name']; // Get the store name
// Set in multidimentional array the vendor and then the cart item key
$items_sort[$store_name][$cart_item_key] = $vendor_id;
}
if ( count($car_items) > 1 ) {
ksort( $items_sort ); // Sorting by vendor name
}
$vendors = 0;
// Loop by vendor name
foreach ( $items_sort as $store_name => $values ) {
$vendor_id = reset($values); // The vendor id
$store_url = dokan_get_store_url( $vendor_id ); // Get the store URL (if needed)
$vendors++;
}
// End of Loop
$flatrate = $vendors * 2;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
return; // Only checkout page
$payment_method = WC()->session->get( 'chosen_payment_method' );
if ( 'cod' == $payment_method ) {
// $surcharge == $vendors;
$cart->add_fee( 'Pay on delivery', $flatrate , true );
}
}
// jQuery - Update checkout on methode payment change
add_action( 'wp_footer', 'nik_checkout' );
function nik_checkout() {
if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
return; // Only checkout page
?>
<script type="text/javascript">
jQuery( function($){
$('form.checkout').on('change', 'input[name="payment_method"]', function(){
$(document.body).trigger('update_checkout');
});
});
</script>
<?php
}