Change currency position according to currency in wordpress - switch-statement

Googled and searched but I couldn't find what I was looking for.
I am building a woocommerce website which has 2 currencies: KRW and USD.
I have two buttons which switch between these currencies.
When KRW is selected, price display 10,000W and when USD is selected, it displays 100$.
What I want is to display $100 when USD is selected.
I tried in my functions.php:
add_filter('woocommerce_price_format','change_currency_pos');
function change_currency_pos( $currency_pos, $currency ) {
switch( $currency ) {
case 'USD': $currency_pos = '%1$s%2$s'; break;
case 'KRW': $currency_pos = '%2$s%1$s'; break;
}
return $currency_pos;
}
also tried:
function change_currency_pos() {
$currency_pos = get_option('woocommerece_currency');
switch($currency_pos) {
case 'USD': $format = '%1$s%2$s'; break;
case 'KRW': $format = '%2$s%1$s'; break;
}
return $currency_pos;
}
add_filter('woocommerce_price_format','change_currency_pos');
Both didn't work. :(
Can somebody help please. Thank you.

This is just a guess since I don't know what plugin you are using or how it works. I am going to assume that it adds a $_GET variable named currency to the end of your URL. www.example.com&currency=KRW But idea is that you set a value for the woocommerce_currency_pos option based on some data provided by the currency plugin.
add_filter( 'pre_option_woocommerce_currency_pos', 'change_currency_position' );
function change_currency_position(){
if( ! isset( $_GET['currency'] ) {
return false;
}
if ( 'USD' == $_GET['currency'] ){
return 'left';
} elseif ( 'KRW' == $_GET['currency'] ){
return 'right';
}
}
Alternatively, I could assume that "right" is the default currency position. And that you only need to filter the option in the instance where the site is displayed in USD-mode. In which case you would only need the following
add_filter( 'pre_option_woocommerce_currency_pos', 'change_currency_position' );
function change_currency_position(){
if( isset( $_GET['currency'] && 'USD' == $_GET['currency'] ){
return 'left';
}
}

For woocommerce_price_format to work you need to set the format like this
add_filter('woocommerce_price_format', 'woo_custom_format_position', 999, 2);
function woo_custom_format_position($format, $currency_pos)
{
/*'left':$format = '%1$s%2$s';
'right':$format = '%2$s%1$s';
'left_space':$format = '%1$s %2$s';
'right_space':$format = '%2$s %1$s';
*/
$format = '%1$s%2$s';//Change your position
return $format;
}

For some strange reason the 'position' setting wasn't working for EUR currency. I had to add this to my functions.php in order to fix it:
add_filter('woocommerce_price_format', 'woo_custom_format_position', 999, 2);
function woo_custom_format_position($format, $currency_pos)
{
/*'left':$format = '%1$s%2$s';
'right':$format = '%2$s%1$s';
'left_space':$format = '%1$s %2$s';
'right_space':$format = '%2$s %1$s';
*/
switch ($currency_pos) {
case 'left':
return '%1$s%2$s';
case 'right':
return '%2$s%1$s';
case 'left_space':
return '%1$s %2$s';
case 'right_space':
return '%2$s %1$s';
};
}
Now it actually uses the position chosen in the settings.
Hope it helps somebody!

Related

Getting Gravity Forms field attributes by label text

I am working on a function to find a field from any form based on the label text. I'd like to return different attributes of the field so that I can use them later.
Originally, I just needed the value of the field, so I used the work here to return the value of a field based on the label:
function itsg_get_value_by_label( $form, $entry, $label ) {
foreach ( $form['fields'] as $field ) {
$lead_key = $field->label;
if ( strToLower( $lead_key ) == strToLower( $label ) ) {
return $entry[ $field->id ];
}
}
return false;
}
I get my value by setting a variable and passing in the field label that I'm looking for:
$mobile_phone = itsg_get_value_by_label( $form, $entry, "Mobile Phone" );
Later on, as I continued to work on my solution, I found that I also needed to find those fields and return the ID. Initially, I wrote the same function and just returned the ID, but I'd like to make the solution more efficient by rewriting the function to return multiple field attributes in an array, as such:
function get_field_atts_by_label( $form, $entry, $label ) {
foreach ( $form['fields'] as $field ) {
$lead_key = $field->label;
if ( strToLower( $lead_key ) == strToLower( $label ) ) {
$field_atts = array(
'value' => $entry[ $field->id ],
'id' => $field->id,
);
return $field_atts;
}
}
return false;
}
My problem now is that I am not quite sure how to retrieve the specific attributes from my function and set them to a variable.
Well, I'll go ahead and answer my own question. Such a simple solution to this one. Had a momentary brain fart.
$mobile_phone = get_field_atts_by_label( $form, $entry, "Mobile Phone" );
$mobile_phone_id = $mobile_phone['id'];
$mobile_phone_value = $mobile_phone['value'];

Pagination link not work properly?

Pagination not work properly?
I saved this code in "Forum-Com.php" file and when i open this file, pagination works properly and correctly but when I include this file in another page, its open first page correctly but when I click second or another page link it open same comments which are on first page. Please help me. ( I am using Scriptsmill comments script v1.06 and function make_pages_string from admin.php )
$COM_CONF['full_path'] = dirname(__FILE__);
function make_pages_string ($all_count, $records_per_page, $cur_page, $base_url) {
if ($all_count > $records_per_page) {
if ($cur_page > 0) { $cur_page=$cur_page-1; }
$first_record = ($cur_page) * $records_per_page;
$limit_string = "LIMIT $first_record, $records_per_page";
$pages=$all_count/$records_per_page;
if ($pages > (int) $pages) { $pages=(int)$pages+1; }
}
if ($pages>1) {
$pages_string.="Page: ";
if ($cur_page>10 && $pages>20) { $first_page=$cur_page-9; }
else { $first_page=1; }
if ($pages>20 && ($cur_page+10)<$pages) { $last_page=$first_page+19; }
else { $last_page=$pages; }
if ($cur_page+1>1) {
$prev=$cur_page;
$pages_string.="<a href='$base_url&page=$prev'>&lt</a> ";
}
for ($i=$first_page; $i<=$last_page; $i++){
if ($i != $cur_page+1) {
$pages_string.="<a href='$base_url&page=$i'>$i</a> ";
}
else {
$pages_string.="<b>$i</b> ";
}
}
if ($cur_page+1<$pages) {
$next=$cur_page+2;
$pages_string.="<a href='$base_url&page=$next'>&gt</a> ";
}
}
return array ($pages_string, $limit_string);
}
function smcom_view()
{
global $comments_db_link, $COM_CONF, $COM_LANG;
$result = mysql_query("select COUNT(id) from {$COM_CONF['dbmaintable']}", $comments_db_link);
list ($all_count) = mysql_fetch_row($result);
list ($pages_string, $limit_string) = make_pages_string ($all_count, 10, $_REQUEST['page'], "{$COM_CONF['base_url']}?action=view");
$result = mysql_query("select time, text, author, email, dont_show_email from {$COM_CONF['dbmaintable']} order by time {$COM_CONF['sort_order']} $limit_string", $comments_db_link);
$comments_count=0;
$id=$time=$text=$author=$email=$dont_show_email=$ip=array();
while (list($id[$comments_count], $time[$comments_count], $text[$comments_count], $author[$comments_count], $email[$comments_count], $dont_show_email[$comments_count], $ip[$comments_count])=mysql_fetch_array($result)) {
$comments_count++;
}
require("{$COM_CONF['full_path']}/templates/Forum-default.php");
}
The code given above has no problem at all. The problem is with the server configuration which turned off $_REQUEST global variable from direct access.

Sub Grid Total In Crm

I have a primary Entity (Self-Insurance) and a secondary entity (Compensation). They have a 1:N relationship. So in my main form of Self Insurance I have a sub-grid with the name 'Worker_Compensation' where i am adding up some payroll values.
I have 2 questions. . .
1: The thing I want is that when I add some values in the sub-grid. I need to show a sum of all payrolls in the text below of my main form named as 'TOTAL'.
2: Where should i call this java script(On which event) Onload or Onsave of form ? or else where because I can seems to locate the events on Subgrid.
I am using a java script for this purpose.
enter code here
function setupGridRefresh() {
var targetgrid = document.getElementById("Worker_Compensation");
// If already loaded
if (targetgrid.readyState == 'complete') {
targetgrid.attachEvent("onrefresh", subGridOnload);
}
else {
targetgrid.onreadystatechange = function applyRefreshEvent() {
var targetgrid = document.getElementById("Worker_Compensation");
if (targetgrid.readyState == 'complete') {
targetgrid.attachEvent("onrefresh", subGridOnload);
}
}
}
subGridOnload();
}
function subGridOnload() {
//debugger;
var grid = Xrm.Page.ui.controls.get('Worker_Compensation')._control;
var sum = 0.00;
if (grid.get_innerControl() == null) {
setTimeout(subGridOnload, 1000);
return;
}
else if (grid.get_innerControl()._element.innerText.search("Loading") != -1) {
setTimeout(subGridOnload, 1000);
return;
}
var ids = grid.get_innerControl().get_allRecordIds();
var cellValue;
for (i = 0; i < ids.length; i++) {
if (grid.get_innerControl().getCellValue('new_estannualpayroll', ids[i]) != "") {
cellValue = grid.get_innerControl().getCellValue('new_estannualpayroll', ids[i]);
cellValue = cellValue.substring(2);
cellValue = parseFloat(cellValue);
sum = sum + cellValue;
}
}
var currentSum = Xrm.Page.getAttribute('new_payrolltotal').getValue();
if (sum > 0 || (currentSum != sum && currentSum != null)) {
Xrm.Page.getAttribute('new_payrolltotal').setValue(sum);
}
}
This piece of code is not working. after i add values in the grid my textbox remains empty!
Thanks in advance
If you are upgrading to Microsoft CRM 2015 soon or are already on Microsoft CRM 2015, you can do this without any JavaScript by simply creating a new calculated rollup field and placing that underneath the sub grid, or wherever you wish to place it on the form. Note that this field is calculated ever 12 hours, but if you wish to, it could be calculated on form load via JavaScript. You can see details about that at https://msdn.microsoft.com/en-us/library/dn817863.aspx -"Calculated and Rollup Attributes". The TechNet document, "Define rollup fields" at https://technet.microsoft.com/library/dn832162.aspx has some good examples, scenarios, and discussion about the limitations of the rollup fields.
You can do it with subgrid's onRefresh. This is also unsupportted way but it works. You must add this functions to your javascript
function AddEventToGridRefresh(gridName, functionToCall) {
// retrieve the subgrid
var grid = document.getElementById(gridName);
// if the subgrid still not available we try again after 1 second
if (grid == null) {
setTimeout(function () {AddEventToGridRefresh(gridName, functionToCall);}, 1000);
return;
}
// add the function to the onRefresh event
grid.control.add_onRefresh(functionToCall);
}
// function used in this example
function AdviseUser() {
alert("Sub-Grid refreshed");
}
For more information, here is the link

cakephp illegal offset string in php 5.4

MyHtmlHelper
public function url($url = null, $full = false) {
if(empty($url['lang']) && isset($this->params['lang'])) {
$url['lang'] = $this->params['lang'];
}
return parent::url($url, $full);
}
AppHelper
public function url($url = null, $full = false) {
if (empty($url['lang'])) {
$url['lang'] = $this->params['lang'];
}
return parent::url($url, $full);
}
when I ran . application warnings cakephp illegal offset lang .....
how do I fix that errors
Check the values you test are actually set first.
Change this:
if(empty($url['lang']) && isset($this->params['lang'])) {
$url['lang'] = $this->params['lang'];
}
To:
if ( ( ! isset( $url['lang'] ) || empty( $url['lang'] ) ) && isset( $this->params['lang'] ) ) {
$url['lang'] = $this->params['lang'];
}
I'm not sure whether I have this the right way round for what you're trying to achieve by the way. In my example I'm checking if the value of $url['lang'] is either not set or empty. It may be that you want to check that it is set and empty.
Use the same technique on the AppHelper as well to resolve your issue.
You need to add is_array($url) to your validation.
Or you might get “Illegal string offset ‘language'”
function url($url = null, $full = false) {
if(!isset($url['language']) && is_array($url) && isset($this->params['language']))
{
$url['language'] = $this->params['language'];
}
return parent::url($url, $full);
}

Plug-in for Smart Search on Joomla: no results

I'm writing a plug-in for my component. For this component I have table "#__radiocatalog_item" with columns id, name, description, and I need to lookup at column name. For this, I wrote this plugin:
<?php
defined('JPATH_BASE') or die;
require_once JPATH_ADMINISTRATOR.'/components/com_finder/helpers/indexer/adapter.php';
class PlgFinderRadioitem extends FinderIndexerAdapter
{
protected $context = 'Radioitem';
protected $extension = 'com_radiocatalog';
protected $layout = 'item';
protected $type_title = 'item';
protected $table = '#__radiocatalog_item';
protected $state_field = 'parent';
protected $autoloadLanguage = true;
protected function setup()
{
return true;
}
public function onFinderDelete($context, $table)
{
if ($context == 'com_radiocatalog.item')
{
$id = $table->id;
}
elseif ($context == 'com_finder.index')
{
$id = $table->id;
}
else
{
return true;
}
return $this->remove($id);
}
public function onFinderChangeState($context, $pks, $value)
{
if ($context == 'com_radiocatalog.item')
{
$this->itemStateChange($pks, $value);
}
if ($context == 'com_plugins.plugin' && $value === 0)
{
$this->pluginDisable($pks);
}
}
protected function index(FinderIndexerResult $item, $format = 'html')
{
if (JComponentHelper::isEnabled($this->extension) == false)
{
return;
}
$item->url = $this->getURL($item->id, 'com_radiocatalog&layout=item', $this->layout);
$item->route = 'index.php?option=com_radiocatalog&view=item&layout=item&id='.$item->id;
$item->addTaxonomy('Type', 'Radioitems');
$item->addTaxonomy('Language', $item->language);
$this->indexer->index($item);
}
protected function getListQuery($sql = null)
{
$db = JFactory::getDbo();
$sql = $sql instanceof JDatabaseQuery ? $sql : $db->getQuery(true);
$sql->select('a.id as id, a.name as title, a.description as description');
$sql->from('#__radiocatalog_item AS a');
return $sql;
}
protected function getStateQuery()
{
$sql = $this->db->getQuery(true);
$sql->select($this->db->quoteName('a.id'));
$sql->select($this->db->quoteName('a.name').' as title');
$sql->from($this->db->quoteName('#__radiocatalog_item') . ' AS a');
return $sql;
}
}
?>
After full indexing, search on the site does not work.
I was struggling with the same problem. So I enabled Joomla debugging {Global Configuration / System / Debug System = true} and tried to search for a term "myterm" using public site SmartSearch module. Then I checked the performed SQL queries. First, the term was found:
SELECT t.term, t.term_id
FROM j_finder_terms AS t
WHERE t.term = 'myterm'
AND t.phrase = 0
with ID=653 (used later):
SELECT l.link_id,m.weight AS ordering
FROM `j_finder_links` AS l
INNER JOIN `j_finder_links_terms2` AS m
ON m.link_id = l.link_id
WHERE l.access IN (1,1)
AND l.state = 1
AND (l.publish_start_date = '0000-00-00 00:00:00' OR l.publish_end_date <= '2014-01-04 17:34:00')
AND (l.publish_end_date = '0000-00-00 00:00:00' OR l.publish_end_date >= '2014-01-04 17:34:00')
AND m.term_id IN (653)
But this query didn't return any result, because j_finder_links.access and j_finder_links.state values were set to 0 instead of 1.
So my suggest you to check the queries and if you have the same problem, try to change your query from getStateQuery() method or select "1 AS access, 1 AS state" in the getListQuery() query and leave the $state_field variable unset.
I'm sorry for a vague explanation, I don't know much about how the SmartSearch work, I'm just trying to make it work somehow with my component.

Resources