当前位置: 首页 > 知识库问答 >
问题:

将WooCommerce订单总量保存为Shipstation的自定义字段

年风华
2023-03-14

我一直在编码一个WP插件,用于计算订单的平方英寸总体积,并通过自定义字段将其发送到Shipstation:

<?php
   

  global $woocommerce;
  $items = $woocommerce->cart->get_cart();

  //Debugging function
  function console_log( $data ){
    echo '<script>';
    echo 'console.log('. json_encode( $data ) .')';
    echo '</script>';
  }
  console_log('$items');
  //Custoom field for shipstation
  add_filter( 'woocommerce_shipstation_export_custom_field_2', 'shipstation_custom_field_2' );

  //Function for processing volume
  function shipstation_custom_field_2() {
    $cart_prods_in3 = array();
    foreach($items as $item => $values) { 
        $_product =  wc_get_product( $values['data']->get_id());
        //GET GET PRODUCT IN3 
        $prod_in3 = $_product->get_length() * 
                   $_product->get_width() * 
                   $_product->get_height();
        $prod_in3 = $prod_m3 * $values['quantity'];
        //PUSH RESULT TO ARRAY
        array_push($cart_prods_in3, $prod_in3);
    } 
    return $cart_prods_in3; // Key for your custom field
}

console_log('shipstation_custom_field_2');

?>

我通过SFTP上传到网站文件,它显示在WP上,但当我点击激活我得到一个致命的错误:

致命错误:未捕获错误:调用成员函数get_cart()在 /nas/content/live/sfmstaging/wp-content/plugins/boxCalc/boxCalc.php:10堆栈跟踪空:#0 /nas/content/live/sfmstaging/wp-admin/includes/plugin.php(2299):包括()#1 /nas/content/live/sfmstaging/wp-admin/plugins.php(191):plugin_sandbox_scrape('box Calc/box Calc...')#2{main}抛入第10行 /nas/content/live/sfmstaging/wp-content/plugins/boxCalc/boxCalc.php

我拉车数据正确吗?Woocommerce global是否存在问题?

共有1个答案

马渊
2023-03-14

请注意,购物车是实时客户对象,您不能像实际操作那样获取它。

现在在ShipStation插件文档中,woocommerce\u ShipStation\u export\u custom\u field\u 2filter hook只是返回现有订单自定义字段的元键。

这意味着当客户下订单时,您需要将购物车总容量保存为自定义订单元数据。

尝试以下方法:

// Custom 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_total_volume');
function save_order_total_volume( $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 variabl product)
        if( ! $item_volume ) {
            $total_volume += get_product_volume( wc_get_product( $item->get_product_id() ) ) * $item->get_quantity();
        } else {
            $total_volume += $item_volume;
        }
    }
    $order->update_meta_data( '_total_volume', $total_volume ); // Save total volume as custom order meta data
}

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

代码functions.php活动子主题(或活动主题)的文件或插件文件中。测试和工作。

 类似资料:
  • 在Woocommerce 3应答代码中使用将自定义产品元数据传递到订单,当从后端手动创建订单时,从后端手动添加产品时,是否可以保存和显示自定义元数据? 这是我的代码(略有改动): 当客户机从前端创建订单时,这种方法可以正常工作。但是,当管理员从后端手动创建订单并添加产品时,自定义元数据不可见。 对于手动创建的订单,如何解决此问题,允许添加产品自定义字段作为自定义订单项目数据?

  • 我对woocommerce(最新版本)中的自定义字段有问题。我需要你的帮助。 我的代码 现在我在从自定义字段获取数据时遇到问题。字段被保存到数据库中,因为在我编辑产品时,值在字段中,但在完成订单后,我无法在钩子中获取它:(我尝试使用get_post_meta、get_meta和其他方法,但都不起作用。有人知道原因吗?

  • WooCommerce默认使用post ID作为订单号,post可以是订单,也可以是产品、页面、文章或其它的custom post type,所以这个ID是不连续的,人们并不能简单的通过订单号去猜测你网站的销量。但这也给商店管理者带来一定困扰,拿到一个订单号,却无法知道是什么时候的订单,是哪个商店的订单。所以要给订单号加前缀,前缀里带上具体时间和一个序号,就能解决这个问题。 用代码自定义订单号的显

  • 我正在使用WooCommerce REST API(http://woocommerce.github.io/woocommerce-rest-api-docs/#introduction),能够成功下载客户,订单等。 我的客户在结帐页面中添加了一些自定义字段,例如: 我希望在请求订单时能够获得这些自定义字段,例如: 目前,当我收到订单时,没有返回任何自定义字段。我试着加上 到请求URL,但这没有

  • 在该商业商店订购特定产品时,将向订单添加两个元值。 存储元值的两个字段位于 元键是: 我希望在下新订单时以编程方式创建一个新的自定义字段,并将此新字段的值设置为,前提是已订购的产品存在元键assemblycost。 经过一些研究,我发现是一个钩子,在订单保存到数据库并更新元数据后执行。所以这似乎是我应该使用的钩子。 参考:为Woocommerce中的订单添加额外的元数据: 我尝试在函数中添加以下代

  • 使用以下代码,我可以在WooCommerce产品上获得一个自定义字段,但是如何在订单、结帐和管理订单(后端)上显示它呢? 这是我在每个产品上的自定义字段 这是我的管理订单(后端)详细信息。我尝试显示我在每个产品帖子中输入的正确元数据。 如何在管理订单中显示我在自定义字段中输入的值?我也希望有人能帮助我显示在订单和结帐页面的元。。。或者引导我走向正确的方向。我意识到,为了在管理订单中显示它,我必须确