将自定义元数据保存到 Woocommerce 订单
Save custom metadata to Woocommerce orders
在 WooCommerce 中,我使用工具集类型向产品添加了 2 个字段。因此,当您添加产品时,您可以设置预计交货时间(以天或周为单位)。
在购物车和评论页面上,我写了一些代码来显示总计中最长的交货时间 table。
我在 cart-totals.php 和 review-order.php 中的结束 table 标记之前添加了以下代码(当然我已将其添加到我的 child 主题中。
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
$amount_of_days = 0;
$delivery_amount = get_post_meta($product_id, 'wpcf-aantal', true);
$delivery_week_or_days = get_post_meta($product_id, 'wpcf-dagen-of-weken', true);
if($delivery_week_or_days == "weken"){
$amount_of_days = $delivery_amount * 7;
} else {
$amount_of_days = $delivery_amount;
}
$array_of_days[] = $amount_of_days;
}
$most_days = max($array_of_days);
if ($most_days % 7 == 0){
$most_time_string = $most_days/7 . ' weken';
} else {
$most_time_string = $most_days . ' dagen';
}
if (!empty($most_days)){
echo '<tr class="custom-cart-delivery-time">';
echo '<th>Geschatte levertijd</th>';
echo '<td>' . $most_time_string. '</td>';
echo '</tr>';
}
我的问题是我如何真正将其安全地保存到订单中?我希望在订单和发票中显示预计的运输时间(例如:“3 周”或“2 天”)。
您可以尝试使用以下(注释):
// Utility function to get the "most day" value from cart object (cart items)
function get_the_most_day(){
$array_of_days = []; // Initializing;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
$delivery_amount = get_post_meta($cart_item['product_id'], 'wpcf-aantal', true);
$delivery_week_or_days = get_post_meta($cart_item['product_id'], 'wpcf-dagen-of-weken', true);
$amount_of_days = 0;
$array_of_days[] = $delivery_week_or_days == "weken" ? $delivery_amount * 7 : $delivery_amount;
}
$most_days = max($array_of_days);
return $most_days % 7 == 0 ? $most_days / 7 . ' weken' : $most_days . ' dagen';
}
// Save the most day in Woocommerce session (on checkout page)
add_action( 'template_redirect', 'save_most_days_in_wc_session' );
function save_most_days_in_wc_session() {
// Only checkout page
if( is_checkout() && ! is_wc_endpoint_url() ){
WC()->session->set( 'most_day', get_the_most_day() );
}
}
// Save the most day as custom meta data in the order
add_action('woocommerce_checkout_create_order', 'save_the_most_day_as_order_meta_data', 20, 2);
function save_the_most_day_as_order_meta_data( $order, $data ) {
if( $most_day = WC()->session->get( 'most_day' ) ){
$order->update_meta_data( '_most_day', $most_day );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该有效。
得到这个"most day"值(2种方式):
1) 来自 WC_Order
$order
变量对象
$most_day_value = get_meta( '_most_day' );
2) 来自动态订单 ID 变量
$most_day_value = get_post_meta( $order_id, '_most_day', true );
在 WooCommerce 中,我使用工具集类型向产品添加了 2 个字段。因此,当您添加产品时,您可以设置预计交货时间(以天或周为单位)。
在购物车和评论页面上,我写了一些代码来显示总计中最长的交货时间 table。
我在 cart-totals.php 和 review-order.php 中的结束 table 标记之前添加了以下代码(当然我已将其添加到我的 child 主题中。
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
$amount_of_days = 0;
$delivery_amount = get_post_meta($product_id, 'wpcf-aantal', true);
$delivery_week_or_days = get_post_meta($product_id, 'wpcf-dagen-of-weken', true);
if($delivery_week_or_days == "weken"){
$amount_of_days = $delivery_amount * 7;
} else {
$amount_of_days = $delivery_amount;
}
$array_of_days[] = $amount_of_days;
}
$most_days = max($array_of_days);
if ($most_days % 7 == 0){
$most_time_string = $most_days/7 . ' weken';
} else {
$most_time_string = $most_days . ' dagen';
}
if (!empty($most_days)){
echo '<tr class="custom-cart-delivery-time">';
echo '<th>Geschatte levertijd</th>';
echo '<td>' . $most_time_string. '</td>';
echo '</tr>';
}
我的问题是我如何真正将其安全地保存到订单中?我希望在订单和发票中显示预计的运输时间(例如:“3 周”或“2 天”)。
您可以尝试使用以下(注释):
// Utility function to get the "most day" value from cart object (cart items)
function get_the_most_day(){
$array_of_days = []; // Initializing;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
$delivery_amount = get_post_meta($cart_item['product_id'], 'wpcf-aantal', true);
$delivery_week_or_days = get_post_meta($cart_item['product_id'], 'wpcf-dagen-of-weken', true);
$amount_of_days = 0;
$array_of_days[] = $delivery_week_or_days == "weken" ? $delivery_amount * 7 : $delivery_amount;
}
$most_days = max($array_of_days);
return $most_days % 7 == 0 ? $most_days / 7 . ' weken' : $most_days . ' dagen';
}
// Save the most day in Woocommerce session (on checkout page)
add_action( 'template_redirect', 'save_most_days_in_wc_session' );
function save_most_days_in_wc_session() {
// Only checkout page
if( is_checkout() && ! is_wc_endpoint_url() ){
WC()->session->set( 'most_day', get_the_most_day() );
}
}
// Save the most day as custom meta data in the order
add_action('woocommerce_checkout_create_order', 'save_the_most_day_as_order_meta_data', 20, 2);
function save_the_most_day_as_order_meta_data( $order, $data ) {
if( $most_day = WC()->session->get( 'most_day' ) ){
$order->update_meta_data( '_most_day', $most_day );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该有效。
得到这个"most day"值(2种方式):
1) 来自 WC_Order
$order
变量对象
$most_day_value = get_meta( '_most_day' );
2) 来自动态订单 ID 变量
$most_day_value = get_post_meta( $order_id, '_most_day', true );