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_taxnow_add_tax_rates($tax_rates, $schedule_id) { include_once WP_PLUGIN_DIR . '/taxnow_woo/taxnow-woo.class.php'; include_once WP_PLUGIN_DIR . '/woocommerce-autoship/classes/wc-autoship-schedule.php'; if (class_exists('class_taxNOW_woo') && class_exists('WC_Autoship_Schedule')) { // Create TaxNOW instance $taxnow_woo = new class_taxNOW_woo(); // Get autoship schedule $schedule = new WC_Autoship_Schedule($schedule_id); // Get autoship customer $customer = $schedule->get_customer(); // Create service $service = $taxnow_woo->create_service('TaxServiceSoap', false); $request = new GetTaxRequest(); $request->setDocDate(date('Y-m-d', current_time('timestamp'))); $request->setDocCode(''); $request->setCustomerCode($customer->get_email()); $request->setCompanyCode(get_option('tnwoo_company_code')); $request->setDocType(DocumentType::$SalesOrder); $request->setDetailLevel(DetailLevel::$Tax); $request->setCurrencyCode(get_option('woocommerce_currency')); $request->setBusinessIdentificationNo(get_option('tnwoo_business_vat_id')); // Origin address $origin = new Address(); $origin->setLine1(get_option('tnwoo_origin_street')); $origin->setCity(get_option('tnwoo_origin_city')); $origin->setRegion(get_option('tnwoo_origin_state')); $origin->setPostalCode(get_option('tnwoo_origin_zip')); $origin->setCountry(get_option('tnwoo_origin_country')); $request->setOriginAddress($origin); // Destination address $destination = new Address(); $destination->setLine1($customer->get('shipping_address_1')); $destination->setCity($customer->get('shipping_city')); $destination->setRegion($customer->get('shipping_state')); $destination->setPostalCode($customer->get('shipping_postcode')); $destination->setCountry($customer->get('shipping_country')); $request->setDestinationAddress($destination); // Lines items $items = $schedule->get_items(); $lines = array(); $global_tax_code = get_option('tnwoo_default_tax_code'); foreach ($items as $i => $item) { // Get WooCommerce product ID $product_id = $item->get_product_id(); // Create line item $line = new Line(); $line->setItemCode($product_id); $line->setDescription($product_id); $tax_code = get_post_meta($product_id, '_taxnow_taxcode', true); $line->setTaxCode(!empty($tax_code) ? $tax_code : $global_tax_code); $line->setQty((int) $item->get_quantity()); $line->setAmount((double) $item->get_autoship_price()); $line->setNo($i + 1); $line->setDiscounted(0); $lines[] = $line; } $request->setLines($lines); // Pretax discount $discount_pretax = 0.0; // Send request $taxnow_woo->log_add_entry('calculate_tax', 'request', $request); try { $response = $service->getTax($request); $taxnow_woo->log_add_entry('calculate_tax', 'response', $response); if ($response->getResultCode() == SeverityLevel::$Success) { foreach ($response->GetTaxLines() as $l => $TaxLine) { foreach ($TaxLine->getTaxDetails() as $d => $TaxDetail) { // Create WooCommerce tax rate $tax_rate = array('rate' => 100.0 * $TaxDetail->getRate(), 'label' => $TaxDetail->getTaxName(), 'shipping' => 'no', 'compound' => 'no'); $tax_rates["wc_autoship_taxnow_{$l}_{$d}"] = $tax_rate; } } } } catch (Exception $e) { $taxnow_woo->log_add_entry('calculate_tax', 'exception', $e->getMessage()); } } // Return tax rates return $tax_rates; }