【问题标题】:Cash On Delivery fee based on Dokan vendors count in WooCommerceWooCommerce 中基于 Dokan 供应商数量的货到付款费用
【发布时间】:2021-02-16 07:12:31
【问题描述】:

在 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
}

【问题讨论】:

    标签: php jquery woocommerce dokan fee


    【解决方案1】:

    此问题与 Dokan 插件 相关,该插件在最新版本的插件中不再按子订单分摊费用。因此,您应该询问 Dokan 支持,检查这是否不是上次更新时引入的错误,或者是否不是新的可用设置。

    现在您的代码已经过时且复杂。它可以真正简化和优化

    以下代码将根据 dokan 供应商数量设置 COD 费用:

    // COD Fee based on vendors count 
    add_action( 'woocommerce_cart_calculate_fees', 'dokan_cod_fee_vendors_based' );
    function dokan_cod_fee_vendors_based ( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
        
        // Only checkout page
        if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
            return;
            
        $fee_by_vendor = 2; // HERE set the fee by vendor
        $vendors_array = array(); // Initializing
    
        // Loop through cart items
        foreach ( $cart->get_cart() as $item ) {
            $vendor_id = get_post_field('post_author', $item['product_id']); // Get the vendor_id
        
            // Set in an indexed array to get how many vendors
            $vendors_array[$vendor_id] = $vendor_id;
        }
    
        $fee_total = count($vendors_array) * $fee_by_vendor; // Get fee total by vendor
    
        if ( 'cod' === WC()->session->get( 'chosen_payment_method' ) ) {
            $cart->add_fee( 'Cash On Delivery Fee', $fee_total, true ); // Apply the fee
        }
    }
    
    // jQuery - Update checkout on methode payment change
    add_action( 'wp_footer', 'payment_refresh_checkout_js' );
    function payment_refresh_checkout_js() {
        // Only checkout page
        if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
            return; // Exit
        ?>
        <script type="text/javascript">
        jQuery( function($){
            $('form.checkout').on('change', 'input[name="payment_method"]', function(){
                $(document.body).trigger('update_checkout');
            });
        });
        </script>
        <?php
    }
    

    代码进入活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

    【讨论】:

    • 它有效,但在子订单上我看不到货到付款。它应该说 2 欧元。它不再拆分它了
    猜你喜欢
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 2019-05-05
    • 2020-10-12
    • 2021-04-12
    • 2018-03-25
    • 2022-10-26
    • 2018-01-30
    相关资源
    最近更新 更多