php - 购物车商品价格计算,基于 Woocommerce 中选择的 "days"自定义字段

标签 php wordpress woocommerce custom-fields price

在 Woocommerce 中,我使用自定义字段根据以下线程代码计算产品价格:Display product custom fields as order items in Woocommerce 3 .

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

function custom_product_price_field() {
echo '<div class="custom-text text">
<h3>Rental</h3>
<label>Start Date:</label>
<input type="date" name="rental_date" value="" class="rental_date" />
<label>Period Rental:</label>
<select name="custom_price" class="custom_price">
    <option value="" selected="selected">choosen period</option>
    <option value="2">2 days</option>
    <option value="4">4 days</option>
</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) {
if (isset($_POST['rental_date']) && !empty($_POST['rental_date'])) {
        $cart_item_data['custom_data']['date'] = $_POST['rental_date'];
}
if (isset($_POST['custom_price']) && !empty($_POST['custom_price'])) {
        $_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(); // Product reg price
        $custom_price = (float) sanitize_text_field($_POST['custom_price']);

        $cart_item_data['custom_data']['base_price'] = $base_price;
        $cart_item_data['custom_data']['new_price'] = $base_price/100 * 15 * $custom_price;
        $cart_item_data['custom_data']['rental'] = $custom_price;
}
if (isset($cart_item_data['custom_data']['new_price']) || isset($cart_item_data['custom_data']['date'])) {
        $cart_item_data['custom_data']['unique_key'] = md5(microtime().rand()); // Make each item unique
}
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'])) {
                $product_price. = $cart_item['custom_data']['rental'] == '2' ? __("2 days") : __("4 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']['date'])) {

        $cart_item_data[] = array(
                'name' => __("Chosen date", "woocommerce"),
                'value' => date('d.m.Y', strtotime($cart_item['custom_data']['date'])),
        );
}
if (isset($cart_item['custom_data']['rental'])) {
        $cart_item_data[] = array(
                'name' => __("Period Rental", "woocommerce"),
                'value' => $cart_item['custom_data']['rental'] == '2' ? __("2 days") : __("4 days"),
        );
}
return $cart_item_data;
}

有必要更改计算新价格的条件。目前,新价格的计算不考虑天数。这是必要条件。

如果用户选择“2 天”,则计算将是...$base_price/100 * 15 * value=2

如果用户选择“4 天”,则计算将是...$base_price/100 * 15 * value=4

我该怎么做?

更新:抱歉,忘记添加您给我的最后一个代码。怎么和他在一起?

// 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( __( 'Choosen Date', 'woocommerce' ), $date );
}
if ( isset( $values['custom_data']['rental'] ) ){
    $rental = $values['custom_data']['rental'] == '2' ? __("2 days") : __("4 days");
    $item->update_meta_data( __( 'Period Rental', 'woocommerce' ), $rental );
}
}

最佳答案

The code in your question make errors due to code formatting, surely when you copy paste it.
For example - > need to be -> or $product_price. = need to be $product_price .=
To understand, see about PHP operators.

在下面,您将找到基于租赁“期间”(天) 的正确计算方法:

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

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        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']] );
    }
}

代码进入事件子主题(或事件主题)的 function.php 文件。经过测试并有效。

enter image description here

关于php - 购物车商品价格计算,基于 Woocommerce 中选择的 "days"自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52023258/

相关文章:

php - 每秒查询一次表以获取通知。这是一个好习惯吗?

php - Codeigniter echo/var_dump 不工作

mysql - 如何重新计算 wp_term_taxonomy 中的计数列?

javascript - 平滑滚动 WordPress 上的粘性标题不起作用

php - 从 Woocommerce 主页上的简码隐藏促销产品

wordpress - Woocommerce 通过延期交货防止负变化库存

php - Woocommerce 可变产品未正确显示

php - 在使用 PHP SDK 以编程方式创建事件时,如何显示 field map ?

php - PDO 在 MYSQL_ATTR_INIT_COMMAND 中设置 session 和设置名称

javascript - jQuery - 添加图标以根据值选择选项