我想在 WooCommerce 结帐中设置每件商品的自定义费用并总结最终价格。项目应该来自多个类别。单击复选框时将收取费用。
编辑了此处找到的一些代码,它有效,但只为购物车中的所有商品添加费用,没有每个类别的条件:
// 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 = ' <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 ));}}}
您的上一个函数有一些错误,请尝试改用:
// 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));
}
}
应该可以。
添加 1: 处理 产品类别(或产品标签)
现在要处理特定的产品类别,您可以使用以下内容:
// 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'
附加 2: 处理 产品 ID (或/和变体 ID)
然后要处理产品 ID,您可以使用类似以下内容:
// 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;
$Product_ids = array(46, 57, 63, 64, 65, 66, 172, 177); // Product or variation IDs
// loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
if( count( array_intersect($Product_ids, [$cart_item['product_id'], $cart_item['variation_id']]) ) > 0 ) {
$quantity += $cart_item['quantity'];
}
}
$cart->add_fee(__('Make shipping faster', 'woocommerce').' (5 usd.)', ($quantity * $amount));
}
}
代码位于子主题的 function.php 文件中(或插件中)。已测试并工作。