【发布时间】: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