如何在产品的基本价格旁边输入新价格?

时间:2018-08-25 07:46:09

标签: php wordpress woocommerce custom-fields price

在Woocommerce中,我根据此代码Cart item price calculation, based on chosen “days” custom field in Woocommerce使用自定义字段来计算产品的价格。

// HERE your rental days settings
function get_rental_days_options() {
    return array(
            '2' => __("2 Days", "woocommerce"),
            '4' => __("4 Days", "woocommerce"),
    );
}

// Add a custom field before single add to cart
add_action('woocommerce_before_add_to_cart_button', 'display_single_product_custom_fields', 5);

function display_single_product_custom_fields() {
    // Get the rental days data options
    $options = array('' => __("Choosen period", "woocommerce")) + get_rental_days_options();

    echo '<div class="custom-text text"> < h3 > '.__("Rental", "woocommerce").' < /h3> < label > '.__("Start Date", "woocommerce").': < /label> < input type = "date"
    name = "rental_date"
    value = ""
    class = "rental_date" / >
            < label > Period: < /label> < select class = "rental-days"
    id = "rental-days"
    name = "rental_days" > ';

    foreach($options as $key => $option) {
            echo '<option value="'.$key.
            '">'.$option.
            '</option>';
    }

    echo '</select> < /div>';
}

// Get custom field value, calculate new item price, save it as custom cart item data
add_filter('woocommerce_add_cart_item_data', 'add_custom_field_data', 20, 3);

function add_custom_field_data($cart_item_data, $product_id, $variation_id) {
    // HERE set the percentage rate to be applied to get the new price
    $percentage = 2;

    if (isset($_POST['rental_date']) && !empty($_POST['rental_date'])) {
            $cart_item_data['custom_data']['start_date'] = $_POST['rental_date'];
    }

    if (isset($_POST['rental_days']) && !empty($_POST['rental_days'])) {
            $cart_item_data['custom_data']['rental_days'] = esc_attr($_POST['rental_days']);

            $_product_id = $variation_id > 0 ? $variation_id : $product_id;

            $product = wc_get_product($_product_id); // The WC_Product Object
            $base_price = (float) $product - > get_regular_price(); // Get the product regular price

            $price_rate = $cart_item_data['custom_data']['rental_days'] * $percentage / 100;

            $cart_item_data['custom_data']['base_price'] = $base_price;
            $cart_item_data['custom_data']['new_price'] = $base_price * $price_rate;
    }

    // Make each cart item unique
    if (isset($cart_item_data['custom_data']['rental_days']) || isset($cart_item_data['custom_data']['start_date'])) {
            $cart_item_data['custom_data']['unique_key'] = md5(microtime().rand());
    }

    return $cart_item_data;
}

// 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;

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

// Display cart item custom price details
add_filter('woocommerce_cart_item_price', 'display_cart_items_custom_price_details', 20, 3);

function display_cart_items_custom_price_details($product_price, $cart_item, $cart_item_key) {
    if (isset($cart_item['custom_data']['base_price'])) {
            $product = $cart_item['data'];
            $base_price = $cart_item['custom_data']['base_price'];
            $product_price = wc_price(wc_get_price_to_display($product, array('price' => $base_price))).
            '<br>';

            if (isset($cart_item['custom_data']['rental_days'])) {
                    $rental_days = get_rental_days_options();
                    $product_price. = $rental_days[$cart_item['custom_data']['rental_days']];
            }
    }
    return $product_price;
}

// Display in cart item the selected date
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['custom_data']['start_date'])) {
            $cart_item_data[] = array(
                    'name' => __("Rental start date", "woocommerce"),
                    'value' => date('d.m.Y', strtotime($cart_item['custom_data']['start_date'])),
            );
    }

    if (isset($cart_item['custom_data']['rental_days'])) {
            $rental_days = get_rental_days_options();
            $cart_item_data[] = array(
                    'name' => __("Rental period", "woocommerce"),
                    'value' => $rental_days[$cart_item['custom_data']['rental_days']],
            );
    }

    return $cart_item_data;
}

// Save and display custom field in orders and email notifications (everywhere)
add_action('woocommerce_checkout_create_order_line_item', 'custom_fields_update_order_item_meta', 20, 4);

function custom_fields_update_order_item_meta($item, $cart_item_key, $values, $order) {
    if (isset($values['custom_data']['date'])) {
            $date = date('d.m.Y', strtotime($values['custom_data']['date']));
            $item - > update_meta_data(__('Start date', 'woocommerce'), $date);
    }
    if (isset($values['custom_data']['rental_days'])) {
            $rental_days = get_rental_days_options();
            $item - > update_meta_data(__('Rental period', 'woocommerce'), $rental_days[$values['custom_data']['rental_days']]);
    }
}

一切正常。但是我需要在产品单页上的基本价格旁边得出价格['new_price']的结论。

当经理添加产品的基本价格时,必须计算一个新价格(基于上述代码)并显示在基本价格旁边。自然使用当前货币符号。

// Display custom price details
// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'display_custom_price', 20, 3 );
add_filter('woocommerce_product_get_regular_price', 'display_custom_price', 20, 3 );
// Variations
add_filter('woocommerce_product_variation_get_regular_price', 'display_custom_price', 20, 3 );
add_filter('woocommerce_product_variation_get_price', 'display_custom_price', 20, 3 );

function display_custom_price( $product_price, $price ) {
if (isset($price['custom_data']['new_price'])) {
    $product = $price['data'];
    $new_price = $price['custom_data']['new_price'];
    $product_price = wc_price(wc_get_price_to_display($product, array('price' => $new_price)));        
}
return $product_price;
 }

还是正确?

add_filter('woocommerce_get_price_html', 'display_custom_price', 20, 3 );
add_filter('woocommerce_variable_price_html', 'display_custom_price', 20, 3 );

我该怎么做?

我将很高兴您的帮助!

0 个答案:

没有答案