将 WooCommerce 订单的总订单项目量保存为自定义元数据
Save total order items volume of WooCommerce orders as custom meta data
我需要计算单个订单的数量,并将总计结果存储在数据库中,然后通过 Rest Api 检索它并在那里访问此参数。
我试图写下一些东西,但在结帐时我收到内部服务器错误。
这就是我想要做的(在我的想象中):
// Store volume in the database
add_action('woocommerce_checkout_update_order_meta', 'woo_add_cart_volume');
function woo_add_cart_volume( $order_id ) {
$order = wc_get_order( $order_id ); // <== Was missing
$total_volume = 0; // Initializing variable
foreach( $order->get_items() as $item ){
$product = $item['data'];
$qty = $item['quantity'];
// Get product dimensions
$length = $product->get_length();
$width = $product->get_width();
$height = $product->get_height();
// Calculations a item level
$total_volume += $length * $width * $height * $qty;
}
update_post_meta( $order_id, '_item_volume', $total_volume );
}
感谢您宝贵的帮助,请耐心等待。再次感谢
这足以将总音量存储在 wp_postmeta
table.
$product = $item['data'];
不正确,导致您在代码 Uncaught Error: Call to a member function get_length() on null
中进一步得到以下错误。使用 $product = $item->get_product();
代替
- 通过代码中添加的注释标签进行补充说明
// Store total volume in the database (wp_postmeta table)
function action_woocommerce_checkout_update_order_meta( $order_id ) {
// Get $order object
$order = wc_get_order( $order_id );
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Initializing variable
$total_volume = 0;
// Loop through order items
foreach( $order->get_items() as $item ) {
// Get product object
$product = $item->get_product();
// Get quantity
$qty = $item->get_quantity();
// Get product dimensions
$length = $product->get_length();
$width = $product->get_width();
$height = $product->get_height();
// Calculations a item level
$total_volume += $length * $width * $height * $qty;
}
// Store in wp_postmeta table
update_post_meta( $order_id, '_item_volume', $total_volume );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'action_woocommerce_checkout_update_order_meta', 10, 1 );
您可以先将每个订单商品量保存为自定义订单商品元数据,如下:
add_action('woocommerce_checkout_create_order_line_item', 'woo_order_item_volume', 10, 4 );
function woo_order_item_volume( $item, $cart_item_key, $values, $order ) {
$product = $values['data'];
$qty = $values['quantity'];
$length = $product->get_length();
$width = $product->get_width();
$height = $product->get_height();
$item->update_meta_data( '_volume', $length * $width * $height * $qty ); // Save item volume
}
然后您将订单总量保存为订单元数据如下:
add_action('woocommerce_checkout_create_order', 'woo_order_total_volume');
function woo_order_total_volume( $order ) {
$total_volume = 0; // Initializing
// Loop through order items
foreach( $order->get_items() as $item ){
$total_volume += $item->get_meta( '_volume' ); // Add item volume to total
}
$order->update_meta_data( '_total_volume', $total_volume ); // Save total volume for order
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
我需要计算单个订单的数量,并将总计结果存储在数据库中,然后通过 Rest Api 检索它并在那里访问此参数。 我试图写下一些东西,但在结帐时我收到内部服务器错误。
这就是我想要做的(在我的想象中):
// Store volume in the database
add_action('woocommerce_checkout_update_order_meta', 'woo_add_cart_volume');
function woo_add_cart_volume( $order_id ) {
$order = wc_get_order( $order_id ); // <== Was missing
$total_volume = 0; // Initializing variable
foreach( $order->get_items() as $item ){
$product = $item['data'];
$qty = $item['quantity'];
// Get product dimensions
$length = $product->get_length();
$width = $product->get_width();
$height = $product->get_height();
// Calculations a item level
$total_volume += $length * $width * $height * $qty;
}
update_post_meta( $order_id, '_item_volume', $total_volume );
}
感谢您宝贵的帮助,请耐心等待。再次感谢
这足以将总音量存储在 wp_postmeta
table.
$product = $item['data'];
不正确,导致您在代码Uncaught Error: Call to a member function get_length() on null
中进一步得到以下错误。使用$product = $item->get_product();
代替- 通过代码中添加的注释标签进行补充说明
// Store total volume in the database (wp_postmeta table)
function action_woocommerce_checkout_update_order_meta( $order_id ) {
// Get $order object
$order = wc_get_order( $order_id );
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Initializing variable
$total_volume = 0;
// Loop through order items
foreach( $order->get_items() as $item ) {
// Get product object
$product = $item->get_product();
// Get quantity
$qty = $item->get_quantity();
// Get product dimensions
$length = $product->get_length();
$width = $product->get_width();
$height = $product->get_height();
// Calculations a item level
$total_volume += $length * $width * $height * $qty;
}
// Store in wp_postmeta table
update_post_meta( $order_id, '_item_volume', $total_volume );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'action_woocommerce_checkout_update_order_meta', 10, 1 );
您可以先将每个订单商品量保存为自定义订单商品元数据,如下:
add_action('woocommerce_checkout_create_order_line_item', 'woo_order_item_volume', 10, 4 );
function woo_order_item_volume( $item, $cart_item_key, $values, $order ) {
$product = $values['data'];
$qty = $values['quantity'];
$length = $product->get_length();
$width = $product->get_width();
$height = $product->get_height();
$item->update_meta_data( '_volume', $length * $width * $height * $qty ); // Save item volume
}
然后您将订单总量保存为订单元数据如下:
add_action('woocommerce_checkout_create_order', 'woo_order_total_volume');
function woo_order_total_volume( $order ) {
$total_volume = 0; // Initializing
// Loop through order items
foreach( $order->get_items() as $item ){
$total_volume += $item->get_meta( '_volume' ); // Add item volume to total
}
$order->update_meta_data( '_total_volume', $total_volume ); // Save total volume for order
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。