currency = wp_strip_all_tags(html_entity_decode(get_woocommerce_currency_symbol())); $this->currencyPosition = get_option('woocommerce_currency_pos'); } } protected function isItemValid($item) { return is_a($item, \WP_Post::class); } protected function getStandardPostField($post, $field) { if ('name' === $field['value']) { $field['value'] = 'post_title'; } return isset($post->{$field['value']}) ? $post->{$field['value']} : ''; } protected function getCustomFieldsValues($post, &$field) { $customFields = array_map('trim', explode(',', $field['value'])); $values = array(); foreach ($customFields as $customField) { if (empty($customField) && !$this->shouldAddCurrency(array_merge($field, array('value' => $customField)))) { continue; } $values[] = ($this->shouldAddCurrency(array_merge($field, array('value' => $customField)))) ? $this->getValueWithCurrency($this->getProductMeta($post, $customField, true), $field) : $this->getProductMeta($post, $customField, true); $values = array_filter($values); } return implode(',', $values); } public function shouldAddCurrency($field) { return (isset($field['args']['is_price']) && 'true' === $field['args']['is_price']) || in_array($field['value'], array( )); } public function round($value, &$field) { if ( is_plugin_active('woocommerce/woocommerce.php') && false !== strpos($value, wc_get_price_decimal_separator()) ) { $tmpValue = str_replace(wc_get_price_decimal_separator(), '.', $value); } else { $tmpValue = $value; } if (isset($field['args']['round-number']) && 'true' === $field['args']['round-number'] && is_numeric($tmpValue)) { unset($field['args']['round-number']); $result = round((float)$tmpValue); } else { $result = $value; } return $result; } public function getValueWithCurrency($value, &$field = array()) { if (empty($value)) { $value = 0; } if (!is_plugin_active('woocommerce/woocommerce.php')) { return $value; } $value = number_format($value, wc_get_price_decimals(), wc_get_price_decimal_separator(), wc_get_price_thousand_separator()); if (isset($field['args']['show-only-decimal']) && 'true' === $field['args']['show-only-decimal']) { if (false !== strpos($value, wc_get_price_decimal_separator())) { $parts = explode(wc_get_price_decimal_separator(), $value); $result = $parts[1]; } else { $result = str_repeat('0', wc_get_price_decimals()); } } else { if (isset($field['args']['show-decimal']) && 'false' === $field['args']['show-decimal']) { if (false !== strpos($value, wc_get_price_decimal_separator())) { $parts = explode(wc_get_price_decimal_separator(), $value); $value = $parts[0]; } } else { $value = $this->round($value, $field); } if ( isset($field['profile']) && !empty($field['profile']->displayPriceWithCurrency) ) { switch ($this->currencyPosition) { case 'left': $result = $this->currency . $value; break; case 'left_space': $result = $this->currency . ' ' . $value; break; case 'right': $result = $value . $this->currency; break; case 'right_space': $result = $value . ' ' . $this->currency; break; default: $result = $this->currency . $value; } } else { $result = $value; } } return $result; } public function getProductMeta($post, $param, $single = true) { $customKeys = get_post_custom_keys($post->ID); if ($customKeys && in_array($param, $customKeys)) { return get_post_meta($post->ID, $param, $single); } else { return ''; } } }