如何在WooCommerce上实现手续费,如托运和结账页面

时间:2018-02-05 作者:Shaheed Mon

我想在购物车/结帐页面添加处理费,选项与发货选项一样。客户可以从多个选项中选择其包装类型。

这里是前端的一个示例。example output

我试图触发wc_update_cart, 但似乎它并没有发送我的自定义字段值。

1 个回复
SO网友:Nicolai Grossherr

有一篇Woocommerce文档文章:Add a surcharge to cart and checkout – uses fees API. 这里举了一些例子,我没有任何功劳。

示例1:Add a percentage based surcharge to all transactions

/**
 * Add a 1% surcharge to your cart / checkout
 * change the $percentage to set the surcharge to a value to suit
 * Uses the WooCommerce fees API
 *
 * Add to theme functions.php
*/
add_action( \'woocommerce_cart_calculate_fees\',\'woocommerce_custom_surcharge\' );
function woocommerce_custom_surcharge() {
  global $woocommerce;

    if ( is_admin() && ! defined( \'DOING_AJAX\' ) )
        return;

    $percentage = 0.01;
    $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;    
    $woocommerce->cart->add_fee( \'Surcharge\', $surcharge, true, \'\' );

}
示例2:Add a standard $ value surcharge to all transactions

add_action( \'woocommerce_cart_calculate_fees\',\'wc_add_surcharge\' ); 
function wc_add_surcharge() { 
global $woocommerce; 

if ( is_admin() && ! defined( \'DOING_AJAX\' ) ) 
return;

$county = array(\'US\');
// change the $fee to set the surcharge to a value to suit
$fee = 1.00;

if ( in_array( WC()->customer->get_shipping_country(), $county ) ) : 
    $woocommerce->cart->add_fee( \'Surcharge\', $fee, true, \'standard\' );  
endif;
}
示例3:Add a surcharge based on the delivery country

/**
 * Add a 1% surcharge to your cart / checkout based on delivery country
 * Taxes, shipping costs and order subtotal are all included in the surcharge amount
 *
 * Change $percentage to set the surcharge to a value to suit
 *
 * Add countries to array(\'US\'); to include more countries to surcharge
 * http://en.wikipedia.org/wiki/ISO_3166-1#Current_codes for available alpha-2 country codes 
 *
 * Change in_array to !in_array to EXCLUDE the $countries array from surcharges
 *
 * Uses the WooCommerce fees API
 * Add to theme functions.php
 */
add_action( \'woocommerce_cart_calculate_fees\',\'woocommerce_custom_surcharge\' );
function woocommerce_custom_surcharge() {
  global $woocommerce;

    if ( is_admin() && ! defined( \'DOING_AJAX\' ) )
        return;

    $county     = array(\'US\');
    $percentage     = 0.01;

    if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) ) :
        $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
        $woocommerce->cart->add_fee( \'Surcharge\', $surcharge, true, \'\' );
    endif;

}

结束

相关推荐

将jQuery与WordPress结合使用(使用wp_enQueue_脚本

我需要在我的主题的单页模板中包含jquery。但看起来它不想这样。我尝试用wp\\u enqueue\\u scripts()调用它,但我什么也没做。下面是函数中的内容。phpadd_action( \'wp_enqueue_scripts\', \'custom_theme_load_scripts\' ); function custom_theme_load_scripts() { wp_enqueue_script( \'jquery\' ); } 以及我