Update CSV from WooCommerce shop orders in functions.php file
-
I’m trying to create a function that will keep a record of WooCommerce orders for a particular product (in this case a magazine subscription)by appending the details for each order with this product in a CSV file stored on the web server. I cannot see any errors in the code but it is not working and cannot understand why.
add_action('init', 'subs_csv'); function subs_csv( $order_id ) { if ( ! $order_id ) { return; } global $woocommerce; $order = new WC_Order( $order_id ); $items = $order->get_items(); foreach ( $items as $item ) { $product_id = $item['product_id']; if ( $product_id == '18801' ){ $subscriber = 'yes'; } else { $subscriber = 'no'; } } if ( $subscriber == 'yes' ){ // Fields for CSV $order_number = $order_id; $order_total = '£' . $order->get_total(); $order_time_date = date("j F Y, G:i:s A"); // Shipping Details $first_name = $order->shipping_first_name; $last_name = $order->shipping_last_name; $addr_company = $order->shipping_city; $addr_1 = $order->shipping_address_1; $addr_2 = $order->shipping_address_2; $addr_city = $order->shipping_city; $addr_state = $order->shipping_state; $addr_postcode = $order->shipping_postcode; $addr_country = $order->shipping_country; // First / Last Issue $first_issue = 0; $current_mag_args = get_posts(array('posts_per_page' => 1, 'post_type' => 'product')); foreach ( $current_mag_args as $current_mag ): $product_id = $current_mag->id; $issue_number = get_field("issue_number", $product_id); $first_issue += $issue_number; endforeach; $last_issue = $first_issue + 3; // Timestamp $order_timestamp = time(); // Open CSV for writing $line = array($order_number, $order_total, $order_time_date, $first_name, $last_name, $addr_company, $addr_1, $addr_2, $addr_city, $addr_state, $addr_postcode, $addr_country, $first_issue, $last_issue, $order_timestamp); $file_path = site_url() . 'famtrav-subs-export.csv'; $subs_csv = fopen($file_path, "a"); fputcsv($subs_csv, $line); fclose($subs_csv); } else { // do nothing } }
Any help would be much appreciated.
Many thanks in advance.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Update CSV from WooCommerce shop orders in functions.php file’ is closed to new replies.