在Woocommerce中将自定义字段添加到自定义产品计算价格

时间:2018-08-24 02:08:57

标签: php wordpress woocommerce cart custom-fields

在Woocommerce中,我根据此答案代码Add a custom product calculated price to Woocommerce cart向产品单页添加了一些自定义字段。

这是我的代码:

// 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'] ).'&nbsp;';
    $product_price .= __("rental", "woocommerce" );     
}
return $product_price;
}

我需要在购物车中显示用户在日历中选择的日期。

<input type="date" name="rental_date" value="" class="rental_date" />

选择租赁期后,一切正常,并显示在购物车中。但是我需要设定百分比:

<select name="custom_price" class="custom_price">
    <option value="30%" selected="selected">2 days</option>
    <option value="60%">4 days</option>                
</select>

如果用户选择“ 2天”,则将计算基本价格的30%。如果计算出“ 4天”为60%。

例如,产品价格为200美元。用户选择等于30%的“ 2天”,并收到140美元的产品费用。同样,如果您选择“ 4天”。

我该怎么做?

我将很高兴您的帮助!

1 个答案:

答案 0 :(得分:2)

更新4 -尝试以下操作(可以根据需要进行跳跃):

BadRequest

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

enter image description here