在点击添加到购物车按钮woocommerce每个产品增加10%的额外金额

时间:2017-05-23 05:18:33

标签: woocommerce

Woocommerce产品详情页面: -

  • 我想加额外的价格。点击"添加到购物车按钮。
  • 我希望能够完全覆盖产品价格

3 个答案:

答案 0 :(得分:0)

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {
    $custom_price = 10; // This will be your custome price  
    foreach ( $cart_object->cart_contents as $key => $value ) {
        $value['data']->price = $custom_price;
    }
}

试试此代码段

答案 1 :(得分:0)

function calculate_extra10( $cart_object ) { 
global $isProcessed;

if( !WC()->session->__isset( "reload_checkout" )) {

foreach ( $cart_object->cart_contents as $key => $value ) {

$price = $cart_object->cart_contents[$key]['data']->price;

$additionCost = $price * 10 / 100;

$cart_object->cart_contents[$key]['data']->set_price($price + `$additionCost);`

} 

$isProcessed = true; 

}
}

add_action( 'woocommerce_before_calculate_totals', 'calculate_extra10', 99 );

答案 2 :(得分:0)

function calculate_eyehole_fee( $cart_object ) {  
    global $isProcessed;
    if( !WC()->session->__isset( "reload_checkout" )) {

        foreach ( $cart_object->cart_contents as $key => $value ) {
            $price = $cart_object->cart_contents[$key]['data']->price;
            $additionCost = $price * 10 / 100;
            $cart_object->cart_contents[$key]['data']->set_price($price + $additionCost);
        } 
        $isProcessed = true;  
    }
}

add_action( 'woocommerce_before_calculate_totals', 'calculate_eyehole_fee', 99 );