douzhi7451 2017-06-19 01:20
浏览 28

PHP woocommerce CSV数组项目计数

I'm trying to exclude/ignore a specific item from a 'count' in an array which generates a CSV file.

I generate a daily CSV file of orders from Woocommerce; Each Order is made up of a quantity of varying single products; I define the CSV Headers manually -eg:

$csv_header = array('Order ID','Company Name','Contact Name','Address 1','Address 2','Suburb','Product 1', 'Product 2', 'Product 3','#Items')

I pull the metadata based on Order IDs:

$result = $wpdb->get_results('SELECT * FROM `'.$wpdb->prefix.'postmeta` WHERE post_id = '. (int)$order_id->ID);

I create an instance of each order and get the items:

    $order = new WC_Order((int)$order_id->ID);
    $items = $order->get_items();

After getting the details and setting the column for those - eg:

if( $res->meta_key == '_shipping_company'){
                $order_line[2] = $res->meta_value;

I then loop to create the invidual item product count and the #items total

    foreach ($items as $item ) {
      $_count = array_search( $item['name'], $csv_header );
      $order_line[$_count] = $item['item_meta']['_qty'][0];
      $row_items += $order_line[$_count];
}

        $order_line[10] = $row_items;

and add blanks to products not in the order:

for ($i = 0; $i < 37; $i++) {

            if( !isset($order_line[$i]) && empty($order_line[$i]) ) {

                $order_line[$i] = '';

            }

This all works well, but now I need to EXCLUDE a product NOT defined in the $csv_header from the count - bearing in mind that the Order will contain defined products. eg: count products 1-3 but not Product 4.

At the moment if a Product is not defined in $csv-header, it will put a number in my first column in place of the order-id instead of ignoring it.

Any suggestions appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测