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

问题描述 投票:5回答:2

基于“Add a checkbox on single product pages that adds an additional cost in 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;
}

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

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

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

php wordpress woocommerce hook-woocommerce orders
2个回答
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 );
    }
}

代码位于活动子主题(或活动主题)的function.php文件中。测试和工作。

在订单接收页面(和所有其他订单页面):

enter image description here

在管理员订单页面中:

enter image description here

在电子邮件通知中

enter image description here


1
投票
add_action( 'woocommerce_admin_order_data_after_order_details', 'warrenty_price_order_meta_general' );

function warrenty_price_order_meta_general( $order ){  ?>

        <br class="clear" />
        <h4>Gift Order <a href="#" class="edit_address">Edit</a></h4>
        <?php 
            /*
             * get all the meta data values we need
             */ 
            $_warrenty_price = get_post_meta( $order->get_id(), '_warrenty_price', true );

        ?>
        <div class="address">
            <p><strong>Warranty</strong></p>
            <?php

                if( $_warrenty_price ) : 
                ?>
                    <p><strong>Price:</strong> <?php echo $_warrenty_price ?></p>

                <?php
                endif;
            ?>
        </div>
<?php } ?>

电子邮件

add_action('woocommerce_email_order_meta', 'warrenty_price_email_order_meta', 10, 3);

function warrenty_price_email_order_meta($order_obj, $sent_to_admin, $plain_text) {


    $warrenty_price = get_post_meta($order_obj->get_id(), '_warrenty_price', true);

    if (empty($warrenty_price))
        return;

    if ($plain_text === false) {


        echo '<h2>Warranty</h2>
        <ul>
        <li><strong>Price:</strong> ' . $warrenty_price . '</li>
        </ul>';
    } else {

        echo "Warranty\n
        Price: $warrenty_price";
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.