Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将自定义元数据保存到Shipstation的商业订单_Php_Wordpress_Woocommerce_Metadata_Shipping - Fatal编程技术网

Php 将自定义元数据保存到Shipstation的商业订单

Php 将自定义元数据保存到Shipstation的商业订单,php,wordpress,woocommerce,metadata,shipping,Php,Wordpress,Woocommerce,Metadata,Shipping,这里有一个自定义插件,它接受方框6,并使用商品数量决定订单的最佳插件: //Initialize box sizes and volumes $boxes = array("10x6x4"=>240, "10x8x6"=>480, "13x9x6"=>702, "12x12x6"=>864, "15x12x8"=>1440, "22x12x8"=&

这里有一个自定义插件,它接受方框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';
}
//初始化框大小和卷
$box=阵列(“10x6x4”=>240,“10x8x6”=>480,“13x9x6”=>702,“12x12x6”=>864,“15x12x8”=>1440,“22x12x8”=>2112,“24x12x12”=>3456);
//函数获取产品的体积
函数获取产品体积($product){
返回$product->get_length()*$product->get_width()*$product->get_height();
}
//将购物车总容量另存为自定义订单元数据
添加操作(“woocommerce\u checkout\u create\u order”、“save\u order\u box\u size”);
功能保存\订单\框\大小($order){
$total_volume=0;//正在初始化
//循环浏览订单项
foreach($order->get_items()作为$item){
$item\u volume=get\u product\u volume($item->get\u product())*$item->get\u quantity();
//对于产品变量(如果无法访问卷,则获取父变量产品的卷)
如果(!$item\u音量){
$total_volume+=获取产品数量(wc_获取产品($item->get_product_id())*$item->get_quantity();
}否则{
$total_volume+=$item_volume;
}
}
//循环浏览框大小和卷
foreach($box作为$box\u大小=>$box\u体积){
if($total_volume update_meta_data)('u box_size',$box_size);
打破
}
}
}
//添加用于导出到装运站的总体积自定义字段元键
添加_过滤器('woocommerce_shipstation_export_custom_field_2'、'shipstation_custom_field_2');
函数shipstation_custom_field_2($custom_field_key){
返回“_-box_-size”;
}
日志中没有抛出错误,但订单到达时,Shipstation的自定义字段中没有值。我使用$total_volume的设置值运行代码,每次都显示正确。拉动项目维度时是否缺少一些内容?

第一行代码(框数组)应要求包含在第二个函数中,如(未定义变量
$box
):

//获取产品体积的函数
函数获取产品体积($product){
返回$product->get_length()*$product->get_width()*$product->get_height();
}
//将购物车总容量另存为自定义订单元数据
添加操作(“woocommerce\u checkout\u create\u order”、“save\u order\u box\u size”);
功能保存\订单\框\大小($order){
//定义长方体大小和体积
$box=阵列(“10x6x4”=>240,“10x8x6”=>480,“13x9x6”=>702,“12x12x6”=>864,“15x12x8”=>1440,“22x12x8”=>2112,“24x12x12”=>3456);
$total_volume=0;//正在初始化
//循环浏览订单项
foreach($order->get_items()作为$item){
$item\u volume=get\u product\u volume($item->get\u product())*$item->get\u quantity();
//对于产品变量(如果无法访问卷,则获取父变量产品的卷)
如果(!$item\u音量){
$total_volume+=获取产品数量(wc_获取产品($item->get_product_id())*$item->get_quantity();
}否则{
$total_volume+=$item_volume;
}
}
//循环浏览框大小和卷
foreach($box作为$box\u大小=>$box\u体积){
if($total_volume update_meta_data)('u box_size',$box_size);
打破
}
}
}
//添加用于导出到装运站的总体积自定义字段元键
添加_过滤器('woocommerce_shipstation_export_custom_field_2'、'shipstation_custom_field_2');
函数shipstation_custom_field_2($custom_field_key){
返回“_-box_-size”;
}

现在应该更好了。

明白了。谢谢!我经常忘记范围是一件事:)
// 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';
}