如何解决将自定义元数据保存到 Shipstation 的 WooCommerce 订单
这里有一个自定义插件,它可以使用六件箱并使用物品的数量来决定订单的最佳选择:
//Initialize Box sizes and volumes
$Boxes = array("10x6x4"=>240,"10x8x6"=>480,"13x9x6"=>702,"12x12x6"=>864,"15x12x8"=>1440,"22x12x8"=>2112,"24x12x12"=>3456);
// Function to get the volume of a product
function get_product_volume( $product ) {
return $product->get_length() * $product->get_width() * $product->get_height();
}
// Save the cart total volume as custom order Meta data
add_action('woocommerce_checkout_create_order','save_order_Box_size');
function save_order_Box_size( $order ) {
$total_volume = 0; // Initializing
// Loop through order items
foreach( $order->get_items() as $item ){
$item_volume = get_product_volume($item->get_product()) * $item->get_quantity();
// For product variations (if volume is not accessible get the volume of the parent variable product)
if( ! $item_volume ) {
$total_volume += get_product_volume( wc_get_product( $item->get_product_id() ) ) * $item->get_quantity();
} else {
$total_volume += $item_volume;
}
}
//Loop through Box sizes and volumes
foreach($Boxes as $Box_size => $Box_volume) {
if($total_volume <= $Box_volume){
// Save total volume as custom order Meta data
$order->update_Meta_data( '_Box_size',$Box_size );
break;
}
}
}
// Add total volume custom field Meta key for export to ship station
add_filter( 'woocommerce_shipstation_export_custom_field_2','shipstation_custom_field_2' );
function shipstation_custom_field_2( $custom_field_key ) {
return '_Box_size';
}
日志中不会抛出任何错误,但当订单到达时,Shipstation 的自定义字段中没有任何值。我使用 $total_volume 的设置值运行代码,并且每次都正确。我在拉取商品尺寸时遗漏了什么吗?
解决方法
第一行代码(盒子数组)应该包含在你的第二个函数中,比如(因为变量 $boxes
没有定义):
// Function to get the volume of a product
function get_product_volume( $product ) {
return $product->get_length() * $product->get_width() * $product->get_height();
}
// Save the cart total volume as custom order meta data
add_action('woocommerce_checkout_create_order','save_order_box_size');
function save_order_box_size( $order ) {
// Define box sizes and volumes
$boxes = array("10x6x4"=>240,"10x8x6"=>480,"13x9x6"=>702,"12x12x6"=>864,"15x12x8"=>1440,"22x12x8"=>2112,"24x12x12"=>3456);
$total_volume = 0; // Initializing
// Loop through order items
foreach( $order->get_items() as $item ){
$item_volume = get_product_volume($item->get_product()) * $item->get_quantity();
// For product variations (if volume is not accessible get the volume of the parent variable product)
if( ! $item_volume ) {
$total_volume += get_product_volume( wc_get_product( $item->get_product_id() ) ) * $item->get_quantity();
} else {
$total_volume += $item_volume;
}
}
//Loop through Box sizes and volumes
foreach($boxes as $box_size => $box_volume) {
if($total_volume <= $box_volume){
// Save total volume as custom order meta data
$order->update_meta_data( '_box_size',$box_size );
break;
}
}
}
// Add total volume custom field meta key for export to ship station
add_filter( 'woocommerce_shipstation_export_custom_field_2','shipstation_custom_field_2' );
function shipstation_custom_field_2( $custom_field_key ) {
return '_box_size';
}
现在应该更好用了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。