/** * Returns shop's base currency. This method implements some simple caching, * to avoid calling get_option() too many times. * * @return string Shop's base currency. */ protected static function shop_base_currency() { if (empty(self::$_base_currency)) { self::$_base_currency = get_option('woocommerce_currency'); } return self::$_base_currency; }
public function product_settings_fields() { global $woocommerce, $post; echo '<div id="fees_product_data" class="fee_panel panel woocommerce_options_panel wc-metaboxes-wrapper">'; echo '<div class="options_group">'; // Text Field - Fee Name woocommerce_wp_text_input(array('id' => 'product-fee-name', 'label' => __('Fee Name', 'woocommerce-product-fees'), 'data_type' => 'text', 'placeholder' => __('Product Fee', 'placeholder', 'woocommerce-product-fees'), 'desc_tip' => 'true', 'description' => __('This will be shown at checkout descriping the added fee.', 'woocommerce-product-fees'))); // Load product's base currency. It will be used to show the Admin which // fees can be calculated automatically $product_base_currency = WooCommerce_Product_Fees_Currency_Helper::get_product_base_currency($post->ID); // Get a list of the enabled currencies. We will need to display one price // field for each. By using the merge/unique trick, we can make sure that // product's base currency is always the first in the list (the enabled_currencies() // method returns them sorted alphabetically) $enabled_currencies = array_unique(array_merge(array($product_base_currency), WooCommerce_Product_Fees_Currency_Helper::enabled_currencies())); $placeholder = ''; foreach ($enabled_currencies as $currency) { $field_description = __('Enter a monetary decimal without any currency symbols or thousand separators. This field also accepts percentages.', 'woocommerce-product-fees'); // For additional currencies, add an extra description to explain that the // fees can be calculated automatically if ($currency != $product_base_currency) { $field_description = ' ' . sprintf(__('If you leave this field empty, its value will be calculated automatically, by converting the base amount in %s to this currency.', 'woocommerce-product-fees'), $product_base_currency); } // If we have more than one currency, show a different placeholder for // the base currency and the additional ones, to help the admin to understand // which ones can be calculated automatically. When a single currency is // used, the placeholder can be left empty, as the field's purpose is // obvious if (count($enabled_currencies) > 1) { $placeholder = $currency === $product_base_currency ? __('Base amount', 'woocommerce-product-fees') : __('Auto', 'woocommerce-product-fees'); } // Text Field - Fee Amount woocommerce_wp_text_input(array('id' => 'product-fee-amount-' . $currency, 'label' => __('Fee Amount', 'woocommerce-product-fees') . ' (' . $currency . ')', 'data_type' => 'price', 'desc_tip' => 'true', 'description' => $field_description, 'name' => 'product-fee-amount[' . $currency . ']', 'placeholder' => $placeholder)); } echo '</div>'; echo '<div class="options_group">'; // Check Box - Fee Multiply Option woocommerce_wp_checkbox(array('id' => 'product-fee-multiplier', 'label' => __('Multiply Fee by Quantity', 'woocommerce-product-fees'), 'desc_tip' => 'true', 'description' => __('Multiply the fee by the quanitity of this product that is added to the cart.', 'woocommerce-product-fees'))); echo '</div>'; echo '</div>'; }
/** * Returns the fee associated with a product, if any. This method is * multi-currency aware, and it's able to retrieve the specific fee that applies * for the active currency. If none is found, then the fee in product's base * currency is taken and converted automatically, using exchange rates. * * @param int product_id A product ID. * @return float|null The product fee, if any, or an empty value if no fee * applies. * @author Aelia <*****@*****.**> */ protected function get_product_fee($product_id) { // This check was copied from the original code, and it seems to verify that // the product has fees associated to it. It may be superflous, though // TODO Review check and (eventually) remove it if (!get_post_meta($product_id, 'product-fee-name', true)) { return false; } $active_currency = get_woocommerce_currency(); $product_fee = get_post_meta($product_id, 'product-fee-amount-' . $active_currency, true); // If there isn't a product fee specified for the active currency, check if // there is a fee for product's base currency if (empty($product_fee)) { // Load product's base currency. It will be used to show the Admin which // fees can be calculated automatically $product_base_currency = WooCommerce_Product_Fees_Currency_Helper::get_product_base_currency($product_id); // If there is a fee in base currency, retrieve it and convert it to the // active currency $product_fee = get_post_meta($product_id, 'product-fee-amount-' . $product_base_currency, true); if (!empty($product_fee)) { // If the fee is a percentage, there is no need to convert it. If it's // a fixed value, then we can convert it to the active currency if (!strpos($product_fee, '%')) { $product_fee = WooCommerce_Product_Fees_Currency_Helper::convert($product_fee, $active_currency, $product_base_currency); } return $product_fee; } } return $product_fee; }