Woocomerce 代码片段,将为单选按钮上不同类别结账时的每个商品添加自定义固定费用

问题描述 投票:0回答:1

我正在寻找一个片段,可以帮助在 Wocomerce 结账时设置每件商品的自定义费用并总结最终价格。项目应该来自几个类别。单击复选框时将收取费用。谢谢大家!

编辑了此处找到的一些代码,它有效,但只为购物车中的所有商品添加费用,没有每个类别的条件:


// Add a custom checkbox fields after billing fields
add_action( 'woocommerce_after_checkout_billing_form', 'add_custom_checkout_checkbox', 20 );
function add_custom_checkout_checkbox(){

    // Add a custom checkbox field
    woocommerce_form_field( 'installment_fee', array(
        'type'  => 'checkbox',
        'label' => __(' Faster ship'),
        'class' => array( 'form-row-wide' ),
    ), '' );
}

// Remove "(optional)" label on "Installement checkbox" field
add_filter( 'woocommerce_form_field' , 'remove_order_comments_optional_fields_label', 10, 4 );
function remove_order_comments_optional_fields_label( $field, $key, $args, $value ) {
    // Only on checkout page for Order notes field
    if( 'installment_fee' === $key && is_checkout() ) {
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        $field = str_replace( $optional, '', $field );
    }
    return $field;
}

// jQuery - Ajax script
add_action( 'wp_footer', 'checkout_fee_script' );
function checkout_fee_script() {
    // Only on Checkout
    if( is_checkout() && ! is_wc_endpoint_url() ) :

    if( WC()->session->__isset('enable_fee') )
        WC()->session->__unset('enable_fee')
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof wc_checkout_params === 'undefined') 
            return false;

        $('form.checkout').on('change', 'input[name=installment_fee]', function(e){
            var fee = $(this).prop('checked') === true ? '1' : '';

            $.ajax({
                type: 'POST',
                url: wc_checkout_params.ajax_url,
                data: {
                    'action': 'enable_fee',
                    'enable_fee': fee,
                },
                success: function (result) {
                    $('body').trigger('update_checkout');
                },
            });
        });
    });
    </script>
    <?php
    endif;
}

// Get Ajax request and saving to WC session
add_action( 'wp_ajax_enable_fee', 'get_enable_fee' );
add_action( 'wp_ajax_nopriv_enable_fee', 'get_enable_fee' );
function get_enable_fee() {
    if ( isset($_POST['enable_fee']) ) {
        WC()->session->set('enable_fee', ($_POST['enable_fee'] ? true : false) );
    }
    die();
}

// Add a custom dynamic 3% fee
 add_action( 'woocommerce_cart_calculate_fees', 'custom_percetage_fee', 20, 1 );
function custom_percetage_fee( $cart ) {
    // Only on checkout
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_checkout() )
        return;

    // Gets cart contents
    foreach ( $cart->get_cart_contents() as $cart_item ) {
   
              $quantity = $cart_item['quantity'];

 $amount = 5;
        
    $total_qty = WC()->cart->cart_contents_count;  }

    if( WC()->session->get('enable_fee') ) {
        $cart->add_fee( __( 'Make shipping faster', 'woocommerce')." (5 usd.)", ($quantity * $amount  ));}}}
    
   
  
wordpress woocommerce e-commerce hook-woocommerce checkout
1个回答
0
投票

您的上一个函数有一些错误,请尝试改用:

// Add a custom dynamic item quantity based fee
add_action( 'woocommerce_cart_calculate_fees', 'custom_percetage_fee', 20, 1 );
function custom_percetage_fee( $cart ) {
    // Only on checkout
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_checkout() )
        return;
    
    if( WC()->session->get('enable_fee') ) {
        $amount   = 5;
        $quantity = 0; 

        // loop through cart items
        foreach ( $cart->get_cart() as $cart_item ) {
            $quantity += $cart_item['quantity'];  
        }

        $cart->add_fee(__('Make shipping faster', 'woocommerce').' (5 usd.)', ($quantity * $amount));
    }
}

应该可以。


处理产品类别(或产品标签)

现在要处理特定的产品类别,您可以使用以下内容:

// Add a custom dynamic item quantity based fee
add_action( 'woocommerce_cart_calculate_fees', 'custom_percetage_fee', 20, 1 );
function custom_percetage_fee( $cart ) {
    // Only on checkout
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_checkout() )
        return;
    
    if( WC()->session->get('enable_fee') ) {
        $amount   = 5;
        $quantity = 0;
        $terms    = array('shirts', 'glasses', 'shoes'); // Product category slugs

        // loop through cart items
        foreach ( $cart->get_cart() as $cart_item ) {
            if( has_term($terms, 'product_cat', $cart_item['product_id']) ) {
                $quantity += $cart_item['quantity']; 
            }
        }

        $cart->add_fee(__('Make shipping faster', 'woocommerce').' (5 usd.)', ($quantity * $amount));
    }
}

要处理产品标签,您将在代码中将“product_cat”替换为“product_tag”…

代码位于子主题的 function.php 文件中(或插件中)。已测试并工作。

© www.soinside.com 2019 - 2024. All rights reserved.