问题
In Woocommerce, I added some custom fields to my product single pages, based on this answer code: Add a custom product calculated price to Woocommerce cart.
This is my code:
// 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;
}
I need to show the date in the cart, that the user has selected in the calendar.
<input type="date" name="rental_date" value="" class="rental_date" />
When selected a rental period, everything works and appears in the shopping cart. But I need to make percentages:
<select name="custom_price" class="custom_price">
<option value="30%" selected="selected">2 days</option>
<option value="60%">4 days</option>
</select>
If the user selects "2 days", 30% of the base price is calculated. If "4 days" is calculated 60%.
For example, the product costs $200. The user selects "2 days" equal to 30% and receives a product cost of $140. Similarly, if you select "4 days".
How can I do that?
I will be glad to your help!
回答1:
Update 4 - Try the following (hopping is that you want):
// 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;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
来源:https://stackoverflow.com/questions/51996543/add-custom-fields-to-custom-product-calculated-price-in-woocommerce