根据特定产品的原价设置百分比费用的 WooCommerce 结帐单选按钮

WooCommerce checkout radio buttons that set a percentage fee based on the original price of a specific product

我正在尝试在 woocommerce 结帐中实施保修选项。下面的代码非常适合根据特定项目 'subtotal' 金额设置百分比费用。

// Custom function to get related cart items subtotal for specific defined product Ids
function get_related_items_subtotal( $cart ) {
    // HERE below define the related targeted products IDs in the array
    $targeted_ids = array(29, 27, 28, 72, 84, 95);
    $custom_subtotal = 0; // Initialize

    // Loop through cart items
    foreach ( $cart->get_cart() as $item ) {
        if ( array_intersect($targeted_ids, array($item['product_id'], $item['variation_id']) ) ) {
            $custom_subtotal += $item['line_subtotal'] + $item['line_subtotal_tax'];
        }
    }
    return $custom_subtotal;
}

// 1 - Display custom checkout radio buttons fields
add_action( 'woocommerce_review_order_before_payment', 'display_custom_checkout_radio_buttons' );
function display_custom_checkout_radio_buttons() {
    $custom_subtotal = get_related_items_subtotal( WC()->cart );

    if ( $custom_subtotal > 0 ) {
        $value = WC()->session->get( 'warranty' );
        $value = empty( $value ) ? WC()->checkout->get_value( 'warranty' ) : $value;
        $value = empty( $value ) ? '0' : $value;

        echo '<div id="checkout-radio">
            <h4>' . __("Choose your Warranty") .'</h4>';

        woocommerce_form_field( 'warranty', array(
            'type' => 'radio',
            'class' => array( 'form-row-wide', 'update_totals_on_change' ),
            'options' => array(
                '0'  => __( '1 Year Repair or Replace Warranty - Included', 'woocommerce' ),
                '10' => __( '2 Years Extended Warranty', 'woocommerce' ) . ' (' . strip_tags( wc_price( 10 * $custom_subtotal / 100 ) ) . ')',
                '15' => __( '3 Years Extended Warranty', 'woocommerce' ) . ' (' . strip_tags( wc_price( 15 * $custom_subtotal / 100 ) ) . ')',
            ),
        ), $value );

        echo '</div>';
    }


}

// 2 - Customizing Woocommerce checkout radio form field
add_filter( 'woocommerce_form_field_radio', 'custom_form_field_radio', 20, 4 );
function custom_form_field_radio( $field, $key, $args, $value ) {
    if ( ! empty( $args['options'] ) && 'warranty' === $key && is_checkout() ) {
        $field = str_replace( '</label><input ', '</label><br><input ', $field );
        $field = str_replace( '<label ', '<label style="display:inline;margin-left:8px;" ', $field );
    }
    return $field;
}

// 3 - Add a percentage Fee based on radio buttons for specific defined product Ids
add_action( 'woocommerce_cart_calculate_fees', 'percentage_fee_based_on_radio_buttons', 20 );
function percentage_fee_based_on_radio_buttons( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = (float) WC()->session->get( 'warranty' );

    if ( $percentage ) {
        $custom_subtotal = get_related_items_subtotal( $cart );

        if ( $custom_subtotal > 0 ) {
            $label_text = sprintf( __('Extended Warranty %d years', 'woocommerce'), $percentage == 10 ? 2 : 3 );
            $cart->add_fee( $label_text, $custom_subtotal * $percentage / 100 );
        }
    }
}

// 4 - Set chosen radio button value to a WC Session variable
add_action( 'woocommerce_checkout_update_order_review', 'chosen_input_radio_button_value_to_wc_session' );
function chosen_input_radio_button_value_to_wc_session( $posted_data ) {
    parse_str( $posted_data, $fields );

    if ( isset( $fields['warranty'] ) ){
        WC()->session->set( 'warranty', $fields['warranty'] );
    }
}

有没有办法根据特定产品的原价而不是 'subtotal' 金额输出计算出的保修金额选项?我正在考虑将价格作为计算的基础,其 SKU 包含一些特定的词。这可能吗?请指教

我向 get_related_items_subtotal() 添加了一些代码以检查 SKU。检查下面的代码。

// Custom function to get related cart items subtotal for specific defined product Ids
function get_related_items_subtotal( $cart ) {
    // HERE below define the related targeted products IDs in the array
    $targeted_skus = array('BCA', 'ABC', 'MPN', 'A4545A', '5656FGDF', 'FDF4FD');
    $custom_subtotal = 0; // Initialize

    // Loop through cart items
    foreach ( $cart->get_cart() as $item ) {

        // Retrieve WC_Product object from the product-id:
        $product = wc_get_product( $item["variation_id"] ? $item["variation_id"] : $item["product_id"] );

        $sku = $product->get_sku();

        if( is_string_contain_word( $sku, $targeted_skus ) ){
            $custom_subtotal += $item['line_subtotal'] + $item['line_subtotal_tax'];
        }

    }
    return $custom_subtotal;
}

// 1 - Display custom checkout radio buttons fields
add_action( 'woocommerce_review_order_before_payment', 'display_custom_checkout_radio_buttons' );
function display_custom_checkout_radio_buttons() {
    $custom_subtotal = get_related_items_subtotal( WC()->cart );

    if ( $custom_subtotal > 0 ) {
        $value = WC()->session->get( 'warranty' );
        $value = empty( $value ) ? WC()->checkout->get_value( 'warranty' ) : $value;
        $value = empty( $value ) ? '0' : $value;

        echo '<div id="checkout-radio">
            <h4>' . __("Choose your Warranty") .'</h4>';

        woocommerce_form_field( 'warranty', array(
            'type' => 'radio',
            'class' => array( 'form-row-wide', 'update_totals_on_change' ),
            'options' => array(
                '0'  => __( '1 Year Repair or Replace Warranty - Included', 'woocommerce' ),
                '10' => __( '2 Years Extended Warranty', 'woocommerce' ) . ' (' . strip_tags( wc_price( 10 * $custom_subtotal / 100 ) ) . ')',
                '15' => __( '3 Years Extended Warranty', 'woocommerce' ) . ' (' . strip_tags( wc_price( 15 * $custom_subtotal / 100 ) ) . ')',
            ),
        ), $value );

        echo '</div>';
    }


}

// 2 - Customizing Woocommerce checkout radio form field
add_filter( 'woocommerce_form_field_radio', 'custom_form_field_radio', 20, 4 );
function custom_form_field_radio( $field, $key, $args, $value ) {
    if ( ! empty( $args['options'] ) && 'warranty' === $key && is_checkout() ) {
        $field = str_replace( '</label><input ', '</label><br><input ', $field );
        $field = str_replace( '<label ', '<label style="display:inline;margin-left:8px;" ', $field );
    }
    return $field;
}

// 3 - Add a percentage Fee based on radio buttons for specific defined product Ids
add_action( 'woocommerce_cart_calculate_fees', 'percentage_fee_based_on_radio_buttons', 20 );
function percentage_fee_based_on_radio_buttons( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = (float) WC()->session->get( 'warranty' );

    if ( $percentage ) {
        $custom_subtotal = get_related_items_subtotal( $cart );

        if ( $custom_subtotal > 0 ) {
            $label_text = sprintf( __('Extended Warranty %d years', 'woocommerce'), $percentage == 10 ? 2 : 3 );
            $cart->add_fee( $label_text, $custom_subtotal * $percentage / 100 );
        }
    }
}

// 4 - Set chosen radio button value to a WC Session variable
add_action( 'woocommerce_checkout_update_order_review', 'chosen_input_radio_button_value_to_wc_session' );
function chosen_input_radio_button_value_to_wc_session( $posted_data ) {
    parse_str( $posted_data, $fields );

    if ( isset( $fields['warranty'] ) ){
        WC()->session->set( 'warranty', $fields['warranty'] );
    }
}

function is_string_contain_word( $find, $string, $exact = false ){
    if( is_array( $string ) ){
        foreach ( $string as $key => $str ) {
            if( !$exact ){
                $find = strtolower( $find );
                $str  = strtolower( $str );
            }   
            if ( preg_match( '/\b'.$find.'\b/', $str ) ) {
                return true;
            }
        }
        return false;
    }else{
        if( !$exact ){
            $find   = strtolower( $find );
            $string = strtolower( $string );
        }
        if ( preg_match( '/\b'.$find.'\b/', $string ) ) {
            return true;
        }
        return false;
    }
}