将自定义元数据保存到 Shipstation 的 WooCommerce 订单

Save custom meta data to WooCommerce orders for Shipstation

这里有一个自定义插件,它采用 box sixes 并根据项目的数量为订单决定最好的:

//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';
}

现在应该可以更好地工作了。