购物车商品价格计算,基于 Woocommerce 中不同的基本价格
Cart item price calculation, based on different basic prices in Woocommerce
在 Woocommerce 中,我使用自定义字段根据此代码计算产品价格 - 。非常感谢 LoicTheAztec 的帮助。
// 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']]);
}
}
没想到新的计算用户价格的条件((这是最后一个了。
如果用户购买了底价大于6660的产品,则标准计算按照上述代码进行。
如果用户购买了底价低于6660的商品,则新价为999/天。接下来,用户选择租赁期。如果他选择2天,那么999 * 2。如果他选择4天,那么999 * 4。
据我理解,需要加上条件if base_price> 6600(以后走标准计算代码)和if base_price <6600(然后根据用户的选择算) 999 * 2 或 999 * 4).
我需要在产品单页的基本价格旁边做一个价格['new_price']的结论。
但是我不太明白如何正确地实现它。
我将很高兴得到你的帮助!
更新: 我放了一个没有插件的干净版本的 Wordpress。激活店面的主题。添加了演示数据。
在文件中,functions.php 添加了代码以及您的编辑:
// 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
$rental_days = $cart_item_data['custom_data']['rental_days'];
// Below is the old/original code for your reference.
/*$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;*/
// Above is the old/original code for your reference. The new code is what follows (below):
// Set base price.
$cart_item_data['custom_data']['base_price'] = $base_price;
// Set new price.
if ( $base_price > 6660 ) {
$price_rate = $rental_days * $percentage / 100;
$cart_item_data['custom_data']['new_price'] = $base_price * $price_rate;
} else {
$cart_item_data['custom_data']['new_price'] = 999 * $rental_days;
}
}
// 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']]);
}
}
截图如下:
计算价格的小问题。
如果底价>6660,则不算数,也不会出现在购物车中。
如果基本价格 < 6660,则一切正常。
我需要计算这两个条件并显示在购物车中。
如果客户同时购买了两种产品,其中一种不符合价格,那就糟糕了((
更新 2: 我更新了代码。这是购物车的屏幕截图:
应该是 5100 美元,而不是 680 美元。
应该是 10200 美元,而不是 1360 美元。
17000 / 100 * 15 = 2550
2550 * 2 = 5100
2550 * 4 = 10200
以及如何在单个产品页面上显示基本价格旁边的新价格?
(Updated/new 回答)
第 1 步: 此代码上方:
// HERE your rental days settings
function get_rental_days_options() {
... code removed, but nothing changed here.
}
添加此代码,计算新价格 — 并基于新计算:
// Calculate new price based on rental period/days.
function calc_rental_new_price( $rental_days, $base_price ) {
$percentage = 15;
if ( $base_price > 6660 ) {
$price_rate = $rental_days * $percentage / 100;
return $base_price * $price_rate;
} else {
return 999 * $rental_days;
}
}
第 2 步: 使用这个新的 add_custom_field_data()
代码 — 为清楚起见重新缩进:
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']['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();
// Set base price.
$cart_item_data['custom_data']['base_price'] = $base_price;
// Set new price.
$cart_item_data['custom_data']['new_price'] = calc_rental_new_price(
$cart_item_data['custom_data']['rental_days'], $base_price
);
}
// 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;
}
第 3 步: 在这段代码之后:
function custom_fields_update_order_item_meta($item, $cart_item_key, $values, $order) {
... code removed, but nothing changed here.
}
添加此代码,在单个产品页面上的基本价格旁边显示新价格:
// Adds the new price next to the base price. That new price is for 1 day rental period.
add_filter( 'woocommerce_get_price_html', 'add_new_price_html', 10, 2 );
function add_new_price_html( $html, $product ) {
// Check if we're on a single Product page, the shop page, or a products archive page.
if ( ! is_product() && ! is_shop() && ! is_product_taxonomy() ) {
return $html;
}
$base_price = (float) ( 'variable' === $product->get_type() ?
$product->get_variation_regular_price() : $product->get_regular_price() );
$new_price = calc_rental_new_price( 1, $base_price );
// If it's a Variable product, appends "From " to the new price.
$text_from = ( 'variable' === $product->get_type() ) ? 'From ' : '';
return $html . ' <span class="new-price">' . $text_from . wc_price( $new_price ) . ' / day</span>';
}
2018 年 9 月 4 日更新
修复了 $base_price
。
新价格现在也显示在商店和产品存档页面上。
在 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']]);
}
}
没想到新的计算用户价格的条件((这是最后一个了。
如果用户购买了底价大于6660的产品,则标准计算按照上述代码进行。
如果用户购买了底价低于6660的商品,则新价为999/天。接下来,用户选择租赁期。如果他选择2天,那么999 * 2。如果他选择4天,那么999 * 4。
据我理解,需要加上条件if base_price> 6600(以后走标准计算代码)和if base_price <6600(然后根据用户的选择算) 999 * 2 或 999 * 4).
我需要在产品单页的基本价格旁边做一个价格['new_price']的结论。
但是我不太明白如何正确地实现它。
我将很高兴得到你的帮助!
更新: 我放了一个没有插件的干净版本的 Wordpress。激活店面的主题。添加了演示数据。
在文件中,functions.php 添加了代码以及您的编辑:
// 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
$rental_days = $cart_item_data['custom_data']['rental_days'];
// Below is the old/original code for your reference.
/*$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;*/
// Above is the old/original code for your reference. The new code is what follows (below):
// Set base price.
$cart_item_data['custom_data']['base_price'] = $base_price;
// Set new price.
if ( $base_price > 6660 ) {
$price_rate = $rental_days * $percentage / 100;
$cart_item_data['custom_data']['new_price'] = $base_price * $price_rate;
} else {
$cart_item_data['custom_data']['new_price'] = 999 * $rental_days;
}
}
// 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']]);
}
}
截图如下:
计算价格的小问题。
如果底价>6660,则不算数,也不会出现在购物车中。 如果基本价格 < 6660,则一切正常。
我需要计算这两个条件并显示在购物车中。
如果客户同时购买了两种产品,其中一种不符合价格,那就糟糕了((
更新 2: 我更新了代码。这是购物车的屏幕截图:
应该是 5100 美元,而不是 680 美元。
应该是 10200 美元,而不是 1360 美元。
17000 / 100 * 15 = 2550
2550 * 2 = 5100
2550 * 4 = 10200
以及如何在单个产品页面上显示基本价格旁边的新价格?
(Updated/new 回答)
第 1 步: 此代码上方:
// HERE your rental days settings
function get_rental_days_options() {
... code removed, but nothing changed here.
}
添加此代码,计算新价格 — 并基于新计算:
// Calculate new price based on rental period/days.
function calc_rental_new_price( $rental_days, $base_price ) {
$percentage = 15;
if ( $base_price > 6660 ) {
$price_rate = $rental_days * $percentage / 100;
return $base_price * $price_rate;
} else {
return 999 * $rental_days;
}
}
第 2 步: 使用这个新的 add_custom_field_data()
代码 — 为清楚起见重新缩进:
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']['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();
// Set base price.
$cart_item_data['custom_data']['base_price'] = $base_price;
// Set new price.
$cart_item_data['custom_data']['new_price'] = calc_rental_new_price(
$cart_item_data['custom_data']['rental_days'], $base_price
);
}
// 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;
}
第 3 步: 在这段代码之后:
function custom_fields_update_order_item_meta($item, $cart_item_key, $values, $order) {
... code removed, but nothing changed here.
}
添加此代码,在单个产品页面上的基本价格旁边显示新价格:
// Adds the new price next to the base price. That new price is for 1 day rental period.
add_filter( 'woocommerce_get_price_html', 'add_new_price_html', 10, 2 );
function add_new_price_html( $html, $product ) {
// Check if we're on a single Product page, the shop page, or a products archive page.
if ( ! is_product() && ! is_shop() && ! is_product_taxonomy() ) {
return $html;
}
$base_price = (float) ( 'variable' === $product->get_type() ?
$product->get_variation_regular_price() : $product->get_regular_price() );
$new_price = calc_rental_new_price( 1, $base_price );
// If it's a Variable product, appends "From " to the new price.
$text_from = ( 'variable' === $product->get_type() ) ? 'From ' : '';
return $html . ' <span class="new-price">' . $text_from . wc_price( $new_price ) . ' / day</span>';
}
2018 年 9 月 4 日更新
修复了
$base_price
。新价格现在也显示在商店和产品存档页面上。