将自定义字段添加到 Woocommerce 中的自定义产品计算价格
Add custom fields to custom product calculated price in Woocommerce
在 Woocommerce 中,我根据以下答案代码向我的产品单页添加了一些自定义字段:。
这是我的代码:
// 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="70" selected="selected">2 days</option>
<option value="40">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, 2 );
function add_custom_field_data( $cart_item_data, $product_id ){
if (! isset($_POST['custom_price']))
return $cart_item_data;
$custom_price = (float) sanitize_text_field( $_POST['custom_price'] );
if( empty($custom_price) )
return $cart_item_data;
$product = wc_get_product($product_id); // The WC_Product Object
$base_price = (float) $product->get_regular_price(); // Product reg price
// New price calculation
$new_price = $base_price * $custom_price/100;
// Set the custom amount in cart object
$cart_item_data['custom_data']['rental'] = (float) $custom_price;
$cart_item_data['custom_data']['new_price'] = (float) $new_price;
$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']['rental']) ) {
$product = $cart_item['data'];
$product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) );
$product_price .= '<br>' . wc_price( $cart_item['custom_data']['rental'] ).' ';
$product_price .= __("rental", "woocommerce" );
}
return $product_price;
}
我需要在购物车中显示用户在日历中 select 编辑的日期。
<input type="date" name="rental_date" value="" class="rental_date" />
当 select 租用一段时间后,一切正常并出现在购物车中。但我需要计算百分比:
<select name="custom_price" class="custom_price">
<option value="30%" selected="selected">2 days</option>
<option value="60%">4 days</option>
</select>
如果用户selects“2天”,则计算基准价格的30%。如果按“4天”计算60%。
例如,产品售价 200 美元。用户 select 的“2 天”等于 30%,并收到 140 美元的产品成本。同样,如果您 select “4 天”。
我该怎么做?
很高兴能为您提供帮助!
更新 4 - 尝试以下操作(随心所欲):
// 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="70" selected="selected">2 days</option>
<option value="40">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, 2 );
function add_custom_field_data( $cart_item_data, $product_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 = 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 * $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;
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']) ) {
$product_price .= $cart_item['custom_data']['rental'] == '70' ? __("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'])),
);
}
return $cart_item_data;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
在 Woocommerce 中,我根据以下答案代码向我的产品单页添加了一些自定义字段:
这是我的代码:
// 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="70" selected="selected">2 days</option>
<option value="40">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, 2 );
function add_custom_field_data( $cart_item_data, $product_id ){
if (! isset($_POST['custom_price']))
return $cart_item_data;
$custom_price = (float) sanitize_text_field( $_POST['custom_price'] );
if( empty($custom_price) )
return $cart_item_data;
$product = wc_get_product($product_id); // The WC_Product Object
$base_price = (float) $product->get_regular_price(); // Product reg price
// New price calculation
$new_price = $base_price * $custom_price/100;
// Set the custom amount in cart object
$cart_item_data['custom_data']['rental'] = (float) $custom_price;
$cart_item_data['custom_data']['new_price'] = (float) $new_price;
$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']['rental']) ) {
$product = $cart_item['data'];
$product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) );
$product_price .= '<br>' . wc_price( $cart_item['custom_data']['rental'] ).' ';
$product_price .= __("rental", "woocommerce" );
}
return $product_price;
}
我需要在购物车中显示用户在日历中 select 编辑的日期。
<input type="date" name="rental_date" value="" class="rental_date" />
当 select 租用一段时间后,一切正常并出现在购物车中。但我需要计算百分比:
<select name="custom_price" class="custom_price">
<option value="30%" selected="selected">2 days</option>
<option value="60%">4 days</option>
</select>
如果用户selects“2天”,则计算基准价格的30%。如果按“4天”计算60%。
例如,产品售价 200 美元。用户 select 的“2 天”等于 30%,并收到 140 美元的产品成本。同样,如果您 select “4 天”。
我该怎么做?
很高兴能为您提供帮助!
更新 4 - 尝试以下操作(随心所欲):
// 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="70" selected="selected">2 days</option>
<option value="40">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, 2 );
function add_custom_field_data( $cart_item_data, $product_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 = 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 * $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;
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']) ) {
$product_price .= $cart_item['custom_data']['rental'] == '70' ? __("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'])),
);
}
return $cart_item_data;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。