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

时间:2021-02-18 17:45:05

标签: php wordpress woocommerce metadata shipping

这里有一个自定义插件,它可以使用六件箱并使用物品的数量来决定订单的最佳选择:

//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 的设置值运行代码,并且每次都正确。我在拉取商品尺寸时遗漏了什么吗?

1 个答案:

答案 0 :(得分:2)

第一行代码(盒子数组)应该包含在你的第二个函数中,比如(因为变量 $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';
}

现在应该更好用了。