【问题标题】:Save WooCommerce order total volume as a custom field for Shipstation将 WooCommerce 订单总量保存为 Shipstation 的自定义字段
【发布时间】:2021-05-17 20:53:15
【问题描述】:

我一直在编写一个 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 上,但是当我单击激活时出现致命错误:

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

我是否正确地提取了购物车数据? Woocommerce global 有问题吗?

【问题讨论】:

    标签: php wordpress woocommerce plugins orders


    【解决方案1】:

    请注意,购物车是实时客户对象,您无法按照实际操作获得。

    现在在ShipStation plugin documentation 中,woocommerce_shipstation_export_custom_field_2 过滤器挂钩只是返回现有订单自定义字段的元键。

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

    请尝试以下方法:

    // 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 文件或插件文件中。经过测试并且可以工作。

    【讨论】:

    • 能把它做成插件吗?
    猜你喜欢
    • 2021-05-21
    • 2023-03-15
    • 2019-09-16
    • 2019-09-16
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多