【问题标题】:Save custom meta data to WooCommerce orders for Shipstation将自定义元数据保存到 Shipstation 的 WooCommerce 订单
【发布时间】:2021-05-21 17:16:52
【问题描述】:

这里有一个自定义插件,它可以使用 6 个盒子并根据商品的数量来决定最适合订单的插件:

//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 的设定值运行代码,并且每次都正确显示。拉物品尺寸时我是否遗漏了什么?

【问题讨论】:

    标签: php wordpress woocommerce metadata shipping


    【解决方案1】:

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

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

    【讨论】:

    • 知道了。谢谢!我经常忘记范围是一回事:)
    猜你喜欢
    • 1970-01-01
    • 2021-05-17
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多