在Woocommerce 3中将产品自定义字段显示为订单项

问题描述:

在Woocommerce中,我根据以下代码使用自定义字段来计算产品的价格-

In Woocommerce, I use custom fields to calculate the price of a product, based on this code - Set a calculated price for variable products in the WooCommerce. Thanks for the help LoicTheAztec.

// 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="30" selected="selected">2 days</option>
    <option value="35">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 * $custom_price/100;
    $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'] == '30' ? __("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'] == '30' ? __("2 days") : __("4 days"),
    );
}
return $cart_item_data;
}

我注意到所有自定义字段都显示在购物车和结帐页面中。如何在谢谢页面上的电子邮件通知和订单中显示这些字段?

I noticed that all custom fields are shown in the cart and on the checkout page. How can I show these fields on the "Thank you" page, in email notifications and in orders?

我添加了代码:

// Save custom field value in order items meta data
add_action( 'woocommerce_add_order_item_meta', 'add_custom_field_to_order_item_meta', 20, 3 );
function add_custom_field_to_order_item_meta( $item_id, $values, $cart_item_key ) {

if ( isset( $values['custom_data']['date'] ) ){

wc_add_order_item_meta( $item_id, __( 'Choosen Date', 'woocommerce' ), $values date('d.m.Y', strtotime['custom_data']['date'] );

}
if ( isset( $values['custom_data']['rental'] ) ){

wc_add_order_item_meta( $item_id, __( 'Period Rental', 'woocommerce' ), $values['custom_data']['rental'] == '30' ? __("2 days") : __("4 days"), );

}
}

add_action( 'woocommerce_email_order_details', 'my_custom_order_details', 5, 4 );
function my_custom_order_details( $order, $sent_to_admin, $plain_text, $email ){

}

代码 $ values ['custom_data'] ['rental'] $ values ['custom_data'] ['date'] 正常工作。

由于代码的原因, $ values date('dmY',strtotime ['custom_data'] ['date']); $ v alues ['custom_data'] ['rental'] ==‘30’? __( 2天):__( 4天),); 显示php语法错误。如何正确添加此代码?

Because of the code, $values date('d.m.Y', strtotime['custom_data']['date']); and $values['custom_data']['rental'] == '30'? __ ("2 days"): __ ("4 days"),); shows the php syntax error. How to correctly add this code?

然后为电子邮件通知添加什么代码?

Аnd what code should I add for email notifications?

我应该是

有一个更好的钩子,因此,以下方法可以解决您的错误:

There is a better hook for that since Woocommerce 3. The following that should solve your errors:

// 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'] == '30' ? __("2 days") : __("4 days");
        $item->update_meta_data( __( 'Period Rental', 'woocommerce' ), $rental );
    }
}

代码会出现在您活跃孩子的function.php文件中主题(或活动主题)。

Code goes in function.php file of your active child theme (or active theme). It should work.

相关:

Woocommerce:使用哪个钩子代替不推荐使用的 woocommerce_add_order_item_meta a>