示例#1
0
 public function calculate_shipping($package = array())
 {
     $request = array('recipient' => array('address1' => $package['destination']['address'], 'address2' => $package['destination']['address_2'], 'city' => $package['destination']['city'], 'state_code' => $package['destination']['state'], 'country_code' => $package['destination']['country'], 'zip' => $package['destination']['postcode']), 'items' => array(), 'currency' => get_woocommerce_currency());
     foreach ($package['contents'] as $item) {
         if (!empty($item['data']) && ($item['data']->is_virtual() || $item['data']->is_downloadable())) {
             continue;
         }
         $request['items'][] = array('external_variant_id' => $item['variation_id'] ? $item['variation_id'] : $item['product_id'], 'quantity' => $item['quantity']);
     }
     if (!$request['items']) {
         return false;
     }
     try {
         $client = Printful_Integration::instance()->get_client();
     } catch (PrintfulException $e) {
         $this->set_error($e);
         return false;
     }
     try {
         $response = $client->post('shipping/rates', $request, array('expedited' => true));
         foreach ($response as $rate) {
             $rateData = array('id' => $this->id . '_' . $rate['id'], 'label' => $rate['name'], 'cost' => $rate['rate'], 'taxes' => false, 'calc_tax' => 'per_order');
             $this->add_rate($rateData);
         }
     } catch (PrintfulException $e) {
         $this->set_error($e);
         return false;
     }
 }
 /**
  * Allow remotely get plugin version for debug purposes
  */
 public function get_status()
 {
     $error = false;
     try {
         $client = Printful_Integration::instance()->get_client();
         $storeData = $client->get('store');
     } catch (Exception $e) {
         $error = $e->getMessage();
     }
     return array('version' => Printful_Base::VERSION, 'api_key' => !empty(Printful_Integration::instance()->settings['printful_key']), 'store_id' => !empty($storeData['id']) ? $storeData['id'] : false, 'error' => $error);
 }
 public function __construct()
 {
     $this->id = 'printful';
     $this->method_title = 'Printful Integration';
     $this->method_description = 'Enable integration with Printful fulfillment service';
     add_action('woocommerce_update_options_integration_' . $this->id, array($this, 'process_admin_options'));
     $this->init_form_fields();
     $this->init_settings();
     if ($this->get_option('calculate_tax') == 'yes') {
         //Update tax options if taxes are enabled
         if (get_option('woocommerce_calc_taxes') != 'yes') {
             update_option('woocommerce_calc_taxes', 'yes');
         }
         if (get_option('woocommerce_tax_based_on') != 'shipping') {
             update_option('woocommerce_tax_based_on', 'shipping');
         }
         //Show warning in the tax settings section
         add_action('woocommerce_settings_tax_options', array($this, 'show_tax_warning'));
         //Override tax rates calculated by Woocommerce
         add_filter('woocommerce_matched_tax_rates', array($this, 'calculate_tax'), 10, 6);
     }
     self::$_instance = $this;
 }