在Woocommerce中显示产品可选成本的订单详情

nig*_*ter 5 php wordpress orders woocommerce hook-woocommerce

基于" 在单个产品页面添加复选框,在Woocommerce中添加额外费用 "答案代码,我正在尝试为我的产品添加"额外保修"选项(产品页面中的复选框):

/*
 * add warrenty
 */

// Backend: Additional pricing option custom field
add_action( 'woocommerce_product_options_pricing', 'wc_cost_product_field' );
function wc_cost_product_field() {
    woocommerce_wp_text_input( array(
        'id'        => '_warrenty_price',
        'class'     => 'wc_input_price short',
        'label'     => __( 'Warrenty', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')'
    ));
}

// Backend: Saving product pricing option custom field value
add_action( 'woocommerce_admin_process_product_object', 'save_product_custom_meta_data', 100, 1 );
function save_product_custom_meta_data( $product ){
    if ( isset( $_POST['_warrenty_price'] ) )
        $product->update_meta_data( '_warrenty_price', sanitize_text_field($_POST['_warrenty_price']) );
}

// Front: Add a text input field inside the add to cart form on single product page
add_action('woocommerce_single_product_summary','add_warrenty_price_option_to_single_product', 2 );
function add_warrenty_price_option_to_single_product(){
    global $product;

    if( $product->is_type('variable') || ! $product->get_meta( '_warrenty_price' ) ) return;

    add_action('woocommerce_before_add_to_cart_button', 'product_option_custom_field', 30 );
}

function product_option_custom_field(){
    global $product;

    $active_price = (float) $product->get_price();
    $warrenty_price = (float) $product->get_meta( '_warrenty_price' );
    $warrenty_price_html   = strip_tags( wc_price( wc_get_price_to_display( $product, array('price' => $warrenty_price ) ) ) );
    $active_price_html   = wc_price( wc_get_price_to_display( $product ) );
    $disp_price_sum_html = wc_price( wc_get_price_to_display( $product, array('price' => $active_price + $warrenty_price ) ) );

    echo '<div class="hidden-field">
    <p class="form-row form-row-wide" id="warrenty_option_field" data-priority="">
    <span class="woocommerce-input-wrapper"><span class="war-title"> ' . __("Warrenty price:", "Woocommerce") .
    '</span><label class="checkbox"><input type="checkbox" class="input-checkbox " name="warrenty_option" id="warrenty_option" value="1"> Add Warrenty for ' . $warrenty_price_html .
    '</label></span></p>
    <input type="hidden" name="warrenty_price" value="' . $warrenty_price . '">
    <input type="hidden" name="active_price" value="' . $active_price . '"></div>';

    // Jquery: Update displayed price
    ?>
    <script type="text/javascript">
    jQuery(function($) {
        var cb = 'input[name="warrenty_option"]'
            pp = 'p.price';

        // On change / select a variation
        $('form.cart').on( 'change', cb, function(){
            if( $(cb).prop('checked') === true )
                $(pp).html('<?php echo $disp_price_sum_html; ?>');
            else
                $(pp).html('<?php echo $active_price_html; ?>');
        })

    });
    </script>
    <?php
}

// Front: Calculate new item price and add it as custom cart item data
add_filter('woocommerce_add_cart_item_data', 'add_custom_product_data', 10, 3);
function add_custom_product_data( $cart_item_data, $product_id, $variation_id ) {
    if (isset($_POST['warrenty_option']) && !empty($_POST['warrenty_option'])) {
        $cart_item_data['new_price'] = (float) ($_POST['active_price'] + $_POST['warrenty_price']);
        $cart_item_data['warrenty_price'] = (float) $_POST['warrenty_price'];
        $cart_item_data['active_price'] = (float) $_POST['active_price'];
        $cart_item_data['unique_key'] = md5(microtime().rand());
    }

    return $cart_item_data;
}

// Front: Set the new calculated cart item price
add_action('woocommerce_before_calculate_totals', 'extra_price_add_custom_price', 20, 1);

function extra_price_add_custom_price($cart) {
    if (is_admin() && !defined('DOING_AJAX'))
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    foreach($cart->get_cart() as $cart_item) {
        if (isset($cart_item['new_price']))
            $cart_item['data']->set_price((float) $cart_item['new_price']);
    }
}

// Front: Display option in cart item
add_filter('woocommerce_get_item_data', 'display_custom_item_data', 10, 2);

function display_custom_item_data($cart_item_data, $cart_item) {
    if (isset($cart_item['warrenty_price'])) {
        $cart_item_data[] = array(
            'name' => __("Extra Warrenty", "woocommerce"),
            'value' => strip_tags( '+ ' . wc_price( wc_get_price_to_display( $cart_item['data'], array('price' => $cart_item['warrenty_price'] ) ) ) )
        );
    }

    return $cart_item_data;
}
Run Code Online (Sandbox Code Playgroud)

它工作正常,价格在付款中计算.

问题是字段值不会出现在订单详细信息表(和电子邮件通知)中.所以我无法知道客户是否支付了保修费(计算产品的最终价格除外).

我应该在代码中添加什么内容,以便该字段显示在订单详细信息和邮件中?

Loi*_*tec 4

您只需要这段代码即可在各处显示此保修选项:

// Save warranty as order item custom meta data and display it everywhere
add_action('woocommerce_checkout_create_order_line_item', 'save_order_item_product_warranty', 10, 4 );
function save_order_item_product_warranty( $item, $cart_item_key, $values, $order ) {
    if( isset($values['warrenty_price']) && $values['warrenty_price'] > 0 ) {
        $key = __("Extra Warrenty", "woocommerce");
        $value = strip_tags( '+ '. wc_price( wc_get_price_to_display( $values['data'], array('price' => $values['warrenty_price']) ) ) );
        $item->update_meta_data( $key, $value );
    }
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。

在收到订单的页面(以及所有其他订单页面)中:

在此输入图像描述

在管理订单页面中:

在此输入图像描述

在电子邮件通知中:

在此输入图像描述