function wc_autoship_fees_example_add_fees($fees, $schedule_id) { // Get the autoship schedule $schedule = new WC_Autoship_Schedule($schedule_id); // Add a fee for each autoship schedule item $items = $schedule->get_items(); foreach ($items as $i => $item) { // Get the WooCommerce product $product = $item->get_product(); if (!empty($product)) { // Create a fee for the product $fee = new stdClass(); $fee->id = "wc_autoship_example_fee_{$i}"; $fee->name = $product->get_title() . ' Fee'; $fee->amount = 5.99; $fee->tax_class = ''; $fee->taxable = false; $fee->tax = 0; $fee->tax_data = array(); // Append the fee $fees[] = $fee; } } // Return fees return $fees; }
function wc_autoship_import_muenster_file() { set_time_limit(300); header('Content-Type: application/json'); // Check if file is valid if (empty($_FILES['uploadedFiles']['error']) || $_FILES['uploadedFiles']['error'][0] !== UPLOAD_ERR_OK) { echo json_encode(array('error' => 'Upload error code: ' . $_FILES['uploadedFiles']['error'][0])); die; } // Open file $file = fopen($_FILES['uploadedFiles']['tmp_name'][0], 'r'); if (!$file) { echo json_encode(array('error' => 'Could not open file')); die; } // Read column headers $column_headers = fgetcsv($file); if (empty($column_headers)) { echo json_encode(array('error' => 'Invalid file format')); die; } $columns = array(); foreach ($column_headers as $index => $name) { $columns[$name] = $index; } $column_names = array('Customer Email', 'Next Shipping Date', 'Shipping Method', 'Frequency', 'Quantity', 'Product SKU'); foreach ($column_names as $name) { if (!isset($columns[$name])) { echo json_encode(array('error' => 'Missing column: ' . $name)); die; } } // Include dependencies require_once WP_PLUGIN_DIR . '/woocommerce-autoship/classes/wc-autoship-customer.php'; require_once WP_PLUGIN_DIR . '/woocommerce-autoship/classes/wc-autoship-schedule.php'; require_once WP_PLUGIN_DIR . '/woocommerce-autoship/classes/wc-autoship-schedule-item.php'; // Process file $result = array(); while ($row = fgetcsv($file)) { // Get email $email = $row[$columns['Customer Email']]; if (empty($email)) { // Email is empty, skip this record continue; } $result_item = array('data' => array_combine($column_headers, $row)); // Find existing user $user_id = 0; $user = get_user_by('email', $email); if (!$user) { $result_item['error'] = "User not found for email {$email}"; $result[] = $result_item; continue; } else { $user_id = $user->ID; } $result_item['data']['user_id'] = $user_id; if (empty($user_id) || is_object($user_id)) { $result_item['error'] = 'Invalid user_id'; $result[] = $result_item; continue; } // Create autoship customer $customer = new WC_Autoship_Customer($user_id); $customer->set('shipping_method', $row[$columns['Shipping Method']]); if (false === $customer->save()) { // Error creating autoship customer $result_item['error'] = 'Error creating autoship customer'; $result[] = $result_item; continue; } // Create autoship schedule item $product_id = wc_get_product_id_by_sku($row[$columns['Product SKU']]); if (empty($product_id)) { // Product does not exist $result_item['error'] = 'Product SKU not found'; $result[] = $result_item; continue; } $result_item['data']['product_id'] = $product_id; $product = wc_get_product($product_id); $item = new WC_Autoship_Schedule_Item(); if ($product->is_type('simple')) { $item->set('product_id', $product_id); } elseif ($product->is_type('variation')) { $item->set('variation_id', $product_id); $item->set('product_id', $product->get_parent()); } $item->set('qty', $row[$columns['Quantity']]); // Create autoship schedule $frequency_desc = $row[$columns['Frequency']]; $frequency_matches = null; if (!preg_match('/(\\d+) Month/', $frequency_desc, $frequency_matches)) { // Error saving autoship schedule $result_item['error'] = "Invalid Frequency: {$frequency_desc}"; $result[] = $result_item; continue; } $frequency = 30 * (int) $frequency_matches[1]; $schedule = WC_Autoship_Schedule::get_schedule($user_id, $frequency); $schedule->set_autoship_status(WC_Autoship::STATUS_PAUSED); $schedule->set_next_order_date(date('Y-m-d', strtotime($row[$columns['Next Shipping Date']]))); $schedule->add_item($item); if (false === $schedule->save()) { // Error saving autoship schedule $result_item['error'] = 'Error saving autoship schedule'; $result[] = $result_item; continue; } $result[] = $result_item; } fclose($file); echo json_encode($result); die; }
function wc_autoship_import_paymentxp_file() { set_time_limit(300); header('Content-Type: application/json'); // Check if file is valid if (empty($_FILES['uploadedFiles']['error']) || $_FILES['uploadedFiles']['error'][0] !== UPLOAD_ERR_OK) { echo json_encode(array('error' => 'Upload error code: ' . $_FILES['uploadedFiles']['error'][0])); die; } // Open file $file = fopen($_FILES['uploadedFiles']['tmp_name'][0], 'r'); if (!$file) { echo json_encode(array('error' => 'Could not open file')); die; } // Read column headers $column_headers = fgetcsv($file); if (empty($column_headers)) { echo json_encode(array('error' => 'Invalid file format')); die; } $columns = array(); foreach ($column_headers as $index => $name) { $columns[$name] = $index; } $column_names = array('email', 'autoship frequency', 'next order date', 'product sku', 'item quantity', 'username', 'password', 'pxp_customer_id', 'pmt_desc', 'display_name', 'user_nicename', 'first_name', 'last_name', 'phone', 'billing_phone', 'billing_address', 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_zip', 'billing_postcode', 'billing_country', 'billing_last_name', 'billing_first_name', 'shipping_address_1', 'shipping_address_2', 'shipping_city', 'shipping_state', 'shipping_postcode', 'shipping_country', 'shipping_last_name', 'shipping_first_name', 'shipping_method', 'role', 'affwp_lc_email', 'affwp_lc_affiliate_id'); $usermeta_column_names = array('phone', 'billing_phone', 'billing_address', 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_zip', 'billing_postcode', 'billing_country', 'billing_last_name', 'billing_first_name', 'shipping_address_1', 'shipping_address_2', 'shipping_city', 'shipping_state', 'shipping_postcode', 'shipping_country', 'shipping_last_name', 'shipping_first_name', 'affwp_lc_email', 'affwp_lc_affiliate_id'); foreach ($column_names as $name) { if (!isset($columns[$name])) { echo json_encode(array('error' => 'Missing column: ' . $name)); die; } } // Include dependencies require_once WP_PLUGIN_DIR . '/woocommerce-autoship/classes/wc-autoship-customer.php'; require_once WP_PLUGIN_DIR . '/woocommerce-autoship/classes/wc-autoship-schedule.php'; require_once WP_PLUGIN_DIR . '/woocommerce-autoship/classes/wc-autoship-schedule-item.php'; // Process file $result = array(); while ($row = fgetcsv($file)) { // Get email $email = $row[$columns['email']]; if (empty($email)) { // Email is empty, skip this record continue; } $result_item = array('data' => array_combine($column_headers, $row)); // Find existing user $user_id = 0; $user = get_user_by('email', $email); if (!$user) { // Create new user $user_id = @wp_create_user($row[$columns['username']], $row[$columns['password']], $email); if (!$user_id) { $result_item['error'] = 'Error creating user'; $result[] = $result_item; continue; } @wp_update_user(array('ID' => $user_id, 'user_nicename' => $row[$columns['user_nicename']], 'display_name' => $row[$columns['display_name']], 'first_name' => $row[$columns['first_name']], 'last_name' => $row[$columns['last_name']], 'role' => $row[$columns['role']])); // Add user meta foreach ($usermeta_column_names as $name) { add_user_meta($user_id, $name, $row[$columns[$name]], true); } } else { $user_id = $user->ID; } $result_item['data']['user_id'] = $user_id; if (empty($user_id) || is_object($user_id)) { $result_item['error'] = 'Invalid user_id'; $result[] = $result_item; continue; } // Create autoship customer $customer = new WC_Autoship_Customer($user_id); $customer->store_payment_method('wc_autoship_paymentxp', $row[$columns['pxp_customer_id']], array('CustomerID' => $row[$columns['pxp_customer_id']], 'CardNumber' => $row[$columns['pmt_desc']])); $customer->set('shipping_method', $row[$columns['shipping_method']]); if (false === $customer->save()) { // Error creating autoship customer $result_item['error'] = 'Error creating autoship customer'; $result[] = $result_item; continue; } // Create autoship schedule item $product_id = wc_get_product_id_by_sku($row[$columns['product sku']]); if (empty($product_id)) { // Product does not exist $result_item['error'] = 'Product SKU not found'; $result[] = $result_item; continue; } $result_item['data']['product_id'] = $product_id; $product = wc_get_product($product_id); $item = new WC_Autoship_Schedule_Item(); if ($product->is_type('simple')) { $item->set('product_id', $product_id); } elseif ($product->is_type('variation')) { $item->set('variation_id', $product_id); $item->set('product_id', $product->get_parent()); } $item->set('qty', $row[$columns['item quantity']]); // Create autoship schedule $schedule = WC_Autoship_Schedule::get_schedule($user_id, $row[$columns['autoship frequency']]); $schedule->set_autoship_status(WC_Autoship::STATUS_ACTIVE); $schedule->set_next_order_date(date('Y-m-d', strtotime($row[$columns['next order date']]))); $schedule->add_item($item); if (false === $schedule->save()) { // Error saving autoship schedule $result_item['error'] = 'Error saving autoship schedule'; $result[] = $result_item; continue; } $result[] = $result_item; } fclose($file); echo json_encode($result); die; }
function wc_autoship_taxnow_order_processed($order_id, $schedule_id) { // Get autoship schedule $schedule = new WC_Autoship_Schedule($schedule_id); // Calculate tax $tax = $schedule->get_tax_total(); update_post_meta($order_id, '_order_tax', number_format((double) $tax, 2, '.', '')); // Update total $total = $tax + get_post_meta($order_id, '_order_total', true); update_post_meta($order_id, '_order_total', number_format((double) $total, 2, '.', '')); }