WooCommerce中基于结帐单选按钮选择的费用

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

[我正在尝试在结帐时输出单选按钮,这可能会作为产品添加吗?另外,当我添加“它被破坏的数学部分时。

我不是很好,将不胜感激!

// Output the Custom field in check out
add_action( 'woocommerce_review_order_before_payment', 'checkout_radio_choice', 10,2);
function checkout_radio_choice(){
?>
    <label for="custom_field">

        <input type="radio" name="custom_field" checked="checked" value="option0">DECLINED  <br />
        <input type="radio" name="custom_field" value="option1">SIGNATURE <br />
         <input type="radio" name="custom_field" value="option2">INSURANCE<br />
    </label> <br />
<?php
}

// Stores the custom field value in Cart object
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_product_field_data', 10, 2 );
function save_custom_product_field_data( $cart_item_data, $product_id ) {
    if( isset( $_REQUEST['custom_field'] ) ) {
        $cart_item_data[ 'custom_field' ] = $_REQUEST['custom_field'];
        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
        WC()->session->set( 'my_order_data', $_REQUEST['custom_field'] );
    }
return $cart_item_data;
}

// Outuput custom Item value in Cart and Checkout pages
add_filter( 'woocommerce_get_item_data', 'output_custom_product_field_data', 10, 2 );
 function output_custom_product_field_data( $cart_data, $cart_item ) {

     if( !empty( $cart_data ) )
         $custom_items = $cart_data;

    if( isset( $cart_item['custom_field'] ) ) {
        $custom_items[] = array(
            'key'       => __('Custom Item', 'woocommerce'),
            'value'     => $cart_item['custom_field'],
            'display'   => $cart_item['custom_field'],
        );
    }
    return $custom_items;
}

*********************************((((
Math portion
function WPE_checkout_radio_choice_fee( $cart ) {

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

    $radio = WC()->session->get( 'radio_chosen' );

    if ( "option_1" == $radio ) {

        $fee = 4;

    } elseif ( "option_2" == $radio ) {

        $fee = ( ( wc_prices_include_tax() ? $cart->get_cart_contents_total() + $cart->get_cart_contents_tax() : $cart->get_cart_contents_total() ) * 0.027 + 4.00 );

    }

    $cart->add_fee( __('Protection', 'woocommerce'), $fee
    );

}
php wordpress woocommerce checkout hook-woocommerce
1个回答
0
投票

动作挂钩woocommerce_add_cart_item_datawoocommerce_get_item_data用于产品自定义字段,但不用于结帐自定义字段。

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