/**
  * Save shipping and tax options.
  */
 public function wc_setup_shipping_taxes_save()
 {
     check_admin_referer('wc-setup');
     $enable_shipping = isset($_POST['woocommerce_calc_shipping']);
     $enable_taxes = isset($_POST['woocommerce_calc_taxes']);
     if ($enable_shipping) {
         update_option('woocommerce_ship_to_countries', '');
     } else {
         update_option('woocommerce_ship_to_countries', 'disabled');
     }
     update_option('woocommerce_calc_taxes', $enable_taxes ? 'yes' : 'no');
     update_option('woocommerce_prices_include_tax', sanitize_text_field($_POST['woocommerce_prices_include_tax']));
     if ($enable_shipping && !empty($_POST['shipping_cost_domestic'])) {
         // Create a domestic shipping zone
         $zone = new WC_Shipping_Zone($zone_data['zone_id']);
         $zone->set_zone_name(__('Domestic', 'woocommerce'));
         $zone->set_zone_order(1);
         $zone->add_location(WC()->countries->get_base_country(), 'country');
         $zone->save();
         // Add a flat rate shipping method to this domestic zone
         $instance_id = $zone->add_shipping_method('flat_rate');
         $shipping_method = new WC_Shipping_Flat_Rate($instance_id);
         $option_key = $shipping_method->get_instance_option_key();
         // Update rate settings
         $costs = array();
         $costs[] = wc_format_decimal(sanitize_text_field($_POST['shipping_cost_domestic']));
         if ($item_cost = sanitize_text_field($_POST['shipping_cost_domestic_item'])) {
             $costs[] = $item_cost . ' * [qty]';
         }
         $shipping_method->instance_settings['cost'] = implode(' + ', array_filter($costs));
         $shipping_method->instance_settings['enabled'] = 'yes';
         $shipping_method->instance_settings['type'] = 'order';
         update_option($option_key, $shipping_method->instance_settings);
     }
     if ($enable_shipping && !empty($_POST['shipping_cost_worldwide'])) {
         // Add a flat rate shipping method to the worldwide zone
         $zone = WC_Shipping_Zones::get_zone(0);
         $instance_id = $zone->add_shipping_method('flat_rate');
         $shipping_method = new WC_Shipping_Flat_Rate($instance_id);
         $option_key = $shipping_method->get_instance_option_key();
         // Update rate settings
         $costs = array();
         $costs[] = wc_format_decimal(sanitize_text_field($_POST['shipping_cost_worldwide']));
         if ($item_cost = sanitize_text_field($_POST['shipping_cost_worldwide_item'])) {
             $costs[] = $item_cost . ' * [qty]';
         }
         $shipping_method->instance_settings['cost'] = implode(' + ', array_filter($costs));
         $shipping_method->instance_settings['enabled'] = 'yes';
         $shipping_method->instance_settings['type'] = 'order';
         update_option($option_key, $shipping_method->instance_settings);
     }
     if ($enable_taxes && !empty($_POST['woocommerce_import_tax_rates'])) {
         $locale_info = (include WC()->plugin_path() . '/i18n/locale-info.php');
         $tax_rates = array();
         $country = WC()->countries->get_base_country();
         $state = WC()->countries->get_base_state();
         if (isset($locale_info[$country])) {
             if (isset($locale_info[$country]['tax_rates'][$state])) {
                 $tax_rates = $locale_info[$country]['tax_rates'][$state];
             } elseif (isset($locale_info[$country]['tax_rates'][''])) {
                 $tax_rates = $locale_info[$country]['tax_rates'][''];
             }
             if (isset($locale_info[$country]['tax_rates']['*'])) {
                 $tax_rates = array_merge($locale_info[$country]['tax_rates']['*'], $tax_rates);
             }
         }
         if ($tax_rates) {
             $loop = 0;
             foreach ($tax_rates as $rate) {
                 $tax_rate = array('tax_rate_country' => $rate['country'], 'tax_rate_state' => $rate['state'], 'tax_rate' => $rate['rate'], 'tax_rate_name' => $rate['name'], 'tax_rate_priority' => isset($rate['priority']) ? absint($rate['priority']) : 1, 'tax_rate_compound' => 0, 'tax_rate_shipping' => $rate['shipping'] ? 1 : 0, 'tax_rate_order' => $loop++, 'tax_rate_class' => '');
                 WC_Tax::_insert_tax_rate($tax_rate);
             }
         }
     }
     wp_redirect(esc_url_raw($this->get_next_step_link()));
     exit;
 }
    /**
     * Shipping and taxes
     */
    public function wc_setup_shipping_taxes()
    {
        $domestic = new WC_Shipping_Flat_Rate();
        $international = new WC_Shipping_International_Delivery();
        $shipping_cost_domestic = '';
        $shipping_cost_international = '';
        if ('yes' === $domestic->get_option('enabled')) {
            $shipping_cost_domestic = $domestic->get_option('cost');
        }
        if ('yes' === $international->get_option('enabled')) {
            $shipping_cost_international = $international->get_option('cost');
        }
        ?>
		<h1><?php 
        _e('Shipping &amp; Tax Setup', 'woocommerce');
        ?>
</h1>
		<form method="post">
			<p><?php 
        printf(__('If you will be charging sales tax, or shipping physical goods to customers, you can configure the basic options below. This is optional and can be changed later via %1$sWooCommerce > Settings > Tax%3$s and %2$sWooCommerce > Settings > Shipping%3$s.', 'woocommerce'), '<a href="' . admin_url('admin.php?page=wc-settings&tab=tax') . '" target="_blank">', '<a href="' . admin_url('admin.php?page=wc-settings&tab=shipping') . '" target="_blank">', '</a>');
        ?>
</p>
			<table class="form-table">
				<tr class="section_title">
					<td colspan="2">
						<h2><?php 
        _e('Basic Shipping Setup', 'woocommerce');
        ?>
</h2>
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="woocommerce_calc_shipping"><?php 
        _e('Will you be shipping products?', 'woocommerce');
        ?>
</label></th>
					<td>
						<input type="checkbox" id="woocommerce_calc_shipping" <?php 
        checked(get_option('woocommerce_calc_shipping', 'no'), 'yes');
        ?>
 name="woocommerce_calc_shipping" class="input-checkbox" value="1" />
						<label for="woocommerce_calc_shipping"><?php 
        _e('Yes, I will be shipping physical goods to customers', 'woocommerce');
        ?>
</label>
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="shipping_cost_domestic"><?php 
        _e('<strong>Domestic</strong> shipping costs:', 'woocommerce');
        ?>
</label></th>
					<td>
						<?php 
        printf(__('A total of %s per order and/or %s per item', 'woocommerce'), get_woocommerce_currency_symbol() . ' <input type="text" id="shipping_cost_domestic" name="shipping_cost_domestic" size="5" value="' . esc_attr($shipping_cost_domestic) . '" />', get_woocommerce_currency_symbol() . ' <input type="text" id="shipping_cost_domestic_item" name="shipping_cost_domestic_item" size="5" />');
        ?>
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="shipping_cost_international"><?php 
        _e('<strong>International</strong> shipping costs:', 'woocommerce');
        ?>
</label></th>
					<td>
						<?php 
        printf(__('A total of %s per order and/or %s per item', 'woocommerce'), get_woocommerce_currency_symbol() . ' <input type="text" id="shipping_cost_international" name="shipping_cost_international" size="5" value="' . esc_attr($shipping_cost_international) . '" />', get_woocommerce_currency_symbol() . ' <input type="text" id="shipping_cost_international_item" name="shipping_cost_international_item" size="5" />');
        ?>
					</td>
				</tr>
				<tr class="section_title">
					<td colspan="2">
						<h2><?php 
        _e('Basic Tax Setup', 'woocommerce');
        ?>
</h2>
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="woocommerce_calc_taxes"><?php 
        _e('Will you be charging sales tax?', 'woocommerce');
        ?>
</label></th>
					<td>
						<input type="checkbox" <?php 
        checked(get_option('woocommerce_calc_taxes', 'no'), 'yes');
        ?>
 id="woocommerce_calc_taxes" name="woocommerce_calc_taxes" class="input-checkbox" value="1" />
						<label for="woocommerce_calc_taxes"><?php 
        _e('Yes, I will be charging sales tax', 'woocommerce');
        ?>
</label>
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="woocommerce_prices_include_tax"><?php 
        _e('How will you enter product prices?', 'woocommerce');
        ?>
</label></th>
					<td>
						<label><input type="radio" <?php 
        checked(get_option('woocommerce_prices_include_tax', 'no'), 'yes');
        ?>
 id="woocommerce_prices_include_tax" name="woocommerce_prices_include_tax" class="input-radio" value="yes" /> <?php 
        _e('I will enter prices inclusive of tax', 'woocommerce');
        ?>
</label><br/>
						<label><input type="radio" <?php 
        checked(get_option('woocommerce_prices_include_tax', 'no'), 'no');
        ?>
 id="woocommerce_prices_include_tax" name="woocommerce_prices_include_tax" class="input-radio" value="no" /> <?php 
        _e('I will enter prices exclusive of tax', 'woocommerce');
        ?>
</label>
					</td>
				</tr>
				<?php 
        $locale_info = (include WC()->plugin_path() . '/i18n/locale-info.php');
        $tax_rates = array();
        $country = WC()->countries->get_base_country();
        $state = WC()->countries->get_base_state();
        if (isset($locale_info[$country])) {
            if (isset($locale_info[$country]['tax_rates'][$state])) {
                $tax_rates = $locale_info[$country]['tax_rates'][$state];
            } elseif (isset($locale_info[$country]['tax_rates'][''])) {
                $tax_rates = $locale_info[$country]['tax_rates'][''];
            }
            if (isset($locale_info[$country]['tax_rates']['*'])) {
                $tax_rates = array_merge($locale_info[$country]['tax_rates']['*'], $tax_rates);
            }
        }
        if ($tax_rates) {
            ?>
						<tr>
							<th scope="row"><label for="woocommerce_import_tax_rates"><?php 
            _e('Import Tax Rates?', 'woocommerce');
            ?>
</label></th>
							<td>
								<label><input type="checkbox" id="woocommerce_import_tax_rates" name="woocommerce_import_tax_rates" class="input-checkbox" value="yes" /> <?php 
            _e('Yes, please import some starter tax rates', 'woocommerce');
            ?>
</label>
								<div class="importing-tax-rates">
									<table class="tax-rates">
										<thead>
											<tr>
												<th><?php 
            _e('Country', 'woocommerce');
            ?>
</th>
												<th><?php 
            _e('State', 'woocommerce');
            ?>
</th>
												<th><?php 
            _e('Rate (%)', 'woocommerce');
            ?>
</th>
												<th><?php 
            _e('Name', 'woocommerce');
            ?>
</th>
												<th><?php 
            _e('Tax Shipping', 'woocommerce');
            ?>
</th>
											</tr>
										</thead>
										<tbody>
											<?php 
            foreach ($tax_rates as $rate) {
                ?>
													<tr>
														<td><?php 
                echo esc_attr($rate['country']);
                ?>
</td>
														<td><?php 
                echo esc_attr($rate['state'] ? $rate['state'] : '*');
                ?>
</td>
														<td><?php 
                echo esc_attr($rate['rate']);
                ?>
</td>
														<td><?php 
                echo esc_attr($rate['name']);
                ?>
</td>
														<td><?php 
                echo empty($rate['shipping']) ? '-' : '&#10004;';
                ?>
</td>
													</tr>
													<?php 
            }
            ?>
										</tbody>
									</table>
									<p class="description"><?php 
            _e('Please note: you may still need to add local and product specific tax rates depending on your business location. If in doubt, speak to an accountant.', 'woocommerce');
            ?>
</p>
								</div>
								<p class="description"><?php 
            printf(__('You can edit tax rates later from the %1$stax settings%3$s screen and read more about taxes in %2$sour documentation%3$s.', 'woocommerce'), '<a href="' . admin_url('admin.php?page=wc-settings&tab=tax') . '" target="_blank">', '<a href="http://docs.woothemes.com/document/setting-up-taxes-in-woocommerce/" target="_blank">', '</a>');
            ?>
</p>
							</td>
						</tr>
						<?php 
        }
        ?>
			</table>
			<p class="wc-setup-actions step">
				<input type="submit" class="button-primary button button-large" value="<?php 
        esc_attr_e('Continue', 'woocommerce');
        ?>
" name="save_step" />
				<a href="<?php 
        echo esc_url($this->get_next_step_link());
        ?>
" class="button button-large"><?php 
        _e('Skip this step', 'woocommerce');
        ?>
</a>
				<?php 
        wp_nonce_field('wc-setup');
        ?>
			</p>
		</form>
		<?php 
    }
 /**
  * Initialise settings form fields
  */
 public function init_form_fields()
 {
     parent::init_form_fields();
     $this->form_fields['availability'] = array('title' => __('Availability', 'woocommerce'), 'type' => 'select', 'class' => 'wc-enhanced-select', 'description' => '', 'default' => 'including', 'options' => array('including' => __('Selected countries', 'woocommerce'), 'excluding' => __('Excluding selected countries', 'woocommerce')));
 }
Ejemplo n.º 4
0
 /**
  * WCMp Calculate shipping for order
  *
  * @support flat rate per item 
  * @param int $order_id
  * @param object $order_posted
  * @return void
  */
 function wcmp_checkout_order_processed($order_id, $order_posted)
 {
     global $wpdb, $WCMp;
     $order = new WC_Order($order_id);
     if ($order->has_shipping_method('flat_rate')) {
         $woocommerce_flat_rate_settings = get_option('woocommerce_flat_rate_settings');
         $line_items = $order->get_items('line_item');
         if ($woocommerce_flat_rate_settings['enabled'] == 'yes') {
             if (version_compare(WC_VERSION, '2.5.0', '>=')) {
                 if ($woocommerce_flat_rate_settings['type'] == 'class') {
                     if (!empty($line_items)) {
                         foreach ($line_items as $item_id => $item) {
                             $wc_flat_rate = new WC_Shipping_Flat_Rate();
                             $product = $order->get_product_from_item($item);
                             $shipping_class = $product->get_shipping_class_id();
                             $class_cost_string = $shipping_class ? $wc_flat_rate->get_option('class_cost_' . $shipping_class, '') : $wc_flat_rate->get_option('no_class_cost', '');
                             $cost_item_id = $this->evaluate_flat_shipping_cost($class_cost_string, array('qty' => $item['qty'], 'cost' => $order->get_line_subtotal($item)));
                             $flat_shipping_per_item_val = wc_get_order_item_meta($item_id, 'flat_shipping_per_item', true);
                             if (!$flat_shipping_per_item_val) {
                                 wc_add_order_item_meta($item_id, 'flat_shipping_per_item', round($cost_item_id, 2));
                             }
                         }
                     }
                 }
             } else {
                 if (version_compare(WC_VERSION, '2.4.0', '>')) {
                     if ($woocommerce_flat_rate_settings['type'] == 'class') {
                         if (!empty($line_items)) {
                             foreach ($line_items as $item_id => $item) {
                                 $wc_flat_rate = new WC_Shipping_Flat_Rate();
                                 $product = $order->get_product_from_item($item);
                                 $shipping_class = $product->get_shipping_class();
                                 $class_cost_string = $shipping_class ? $wc_flat_rate->get_option('class_cost_' . $shipping_class, '') : $wc_flat_rate->get_option('no_class_cost', '');
                                 $cost_item_id = $this->evaluate_flat_shipping_cost($class_cost_string, array('qty' => $item['qty'], 'cost' => $order->get_line_subtotal($item)));
                                 $flat_shipping_per_item_val = wc_get_order_item_meta($item_id, 'flat_shipping_per_item', true);
                                 if (!$flat_shipping_per_item_val) {
                                     wc_add_order_item_meta($item_id, 'flat_shipping_per_item', round($cost_item_id, 2));
                                 }
                             }
                         }
                     }
                 } else {
                     $woocommerce_flat_rate_settings_cost = $woocommerce_flat_rate_settings['cost'];
                     $woocommerce_flat_rate_settings_fee = $woocommerce_flat_rate_settings['fee'];
                     $woocommerce_flat_rates = get_option('woocommerce_flat_rates');
                     if ($woocommerce_flat_rate_settings['type'] == 'item') {
                         if (!empty($line_items)) {
                             foreach ($line_items as $item_id => $item) {
                                 $fee = $cost = 0;
                                 $_product = $order->get_product_from_item($item);
                                 $shipping_class = $_product->get_shipping_class();
                                 if (isset($woocommerce_flat_rates[$shipping_class])) {
                                     $cost = $woocommerce_flat_rates[$shipping_class]['cost'];
                                     $fee = $this->get_fee($woocommerce_flat_rates[$shipping_class]['fee'], $_product->get_price());
                                 } elseif ($woocommerce_flat_rate_settings_cost !== '') {
                                     $cost = $woocommerce_flat_rate_settings_cost;
                                     $fee = $this->get_fee($woocommerce_flat_rate_settings_fee, $_product->get_price());
                                     $matched = true;
                                 }
                                 $cost_item_id = ($cost + $fee) * $item['qty'];
                                 $flat_shipping_per_item_val = wc_get_order_item_meta($item_id, 'flat_shipping_per_item', true);
                                 if (!$flat_shipping_per_item_val) {
                                     wc_add_order_item_meta($item_id, 'flat_shipping_per_item', round($cost_item_id, 2));
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($order->has_shipping_method('international_delivery')) {
         $woocommerce_international_delivery_settings = get_option('woocommerce_international_delivery_settings');
         $line_items = $order->get_items('line_item');
         if ($woocommerce_international_delivery_settings['enabled'] == 'yes') {
             if (version_compare(WC_VERSION, '2.5.0', '>=')) {
                 if ($woocommerce_international_delivery_settings['type'] == 'class') {
                     if (!empty($line_items)) {
                         $item_id = false;
                         foreach ($line_items as $item_id => $item) {
                             $wc_international_flat_rate = new WC_Shipping_International_Delivery();
                             $product = $order->get_product_from_item($item);
                             $shipping_class = $product->get_shipping_class_id();
                             $class_cost_string = $shipping_class ? $wc_international_flat_rate->get_option('class_cost_' . $shipping_class, '') : $wc_international_flat_rate->get_option('no_class_cost', '');
                             $cost_item_id = $this->evaluate_flat_shipping_cost($class_cost_string, array('qty' => $item['qty'], 'cost' => $order->get_line_subtotal($item)));
                             $international_flat_shipping_per_item_val = wc_get_order_item_meta($item_id, 'international_flat_shipping_per_item', true);
                             if (!$international_flat_shipping_per_item_val) {
                                 wc_add_order_item_meta($item_id, 'international_flat_shipping_per_item', $cost_item_id);
                             }
                         }
                     }
                 }
             } else {
                 if (version_compare(WC_VERSION, '2.4.0', '>')) {
                     if ($woocommerce_international_delivery_settings['type'] == 'class') {
                         if (!empty($line_items)) {
                             $item_id = false;
                             foreach ($line_items as $item_id => $item) {
                                 $wc_international_flat_rate = new WC_Shipping_International_Delivery();
                                 $product = $order->get_product_from_item($item);
                                 $shipping_class = $product->get_shipping_class();
                                 $class_cost_string = $shipping_class ? $wc_international_flat_rate->get_option('class_cost_' . $shipping_class, '') : $wc_international_flat_rate->get_option('no_class_cost', '');
                                 $cost_item_id = $this->evaluate_flat_shipping_cost($class_cost_string, array('qty' => $item['qty'], 'cost' => $order->get_line_subtotal($item)));
                                 $international_flat_shipping_per_item_val = wc_get_order_item_meta($item_id, 'international_flat_shipping_per_item', true);
                                 if (!$international_flat_shipping_per_item_val) {
                                     wc_add_order_item_meta($item_id, 'international_flat_shipping_per_item', $cost_item_id);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $vendor_shipping_array = get_post_meta($order_id, 'dc_pv_shipped', true);
     $order = new WC_Order($order_id);
     $commission_array = array();
     $mark_ship = 0;
     $items = $order->get_items('line_item');
     foreach ($items as $order_item_id => $item) {
         $comm_pro_id = $product_id = $item['product_id'];
         $variation_id = $item['variation_id'];
         if ($variation_id) {
             $comm_pro_id = $variation_id;
         }
         if ($product_id) {
             $product_vendors = get_wcmp_product_vendors($product_id);
             if ($product_vendors) {
                 if (isset($product_vendors->id) && is_array($vendor_shipping_array)) {
                     if (in_array($product_vendors->id, $vendor_shipping_array)) {
                         $mark_ship = 1;
                     }
                 }
                 $insert_query = $wpdb->query($wpdb->prepare("INSERT INTO `{$wpdb->prefix}wcmp_vendor_orders` ( order_id, commission_id, vendor_id, shipping_status, order_item_id, product_id )\n\t\t\t\t\t\t\t\t\t\t\t\t\t VALUES\n\t\t\t\t\t\t\t\t\t\t\t\t\t ( %d, %d, %d, %s, %d, %d ) ON DUPLICATE KEY UPDATE `created` = now()", $order_id, 0, $product_vendors->id, $mark_ship, $order_item_id, $comm_pro_id));
             }
         }
     }
 }