This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 6 replies, has 2 voices.

Last updated by Bruno Kos 4 months, 1 week ago.

Assigned support staff: Bruno Kos.

Author Posts
June 5, 2019 at 2:29 pm #3961489

jamesP-15

I have the following code to allow me to manually specify prices in 3 different currencies via a Toolset Form...

// Save Primary Currency at Required Price
add_action('cred_save_data', 'save_currencies',10,2);
function save_currencies($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==84)
    {
		error_log("Form is Correct");
		
	 // IF Primary Currency is USD	
	    if (($_POST['wpcf-primary-currency']) == 'USD') {
			
			   error_log("Primary Currency Set");
			// Get latest exchange rates for converting USD
			   $rateUSDtoGBP = 1.27104;
			   $rateUSDtoEUR = 1.12631;
			   $regularPriceUSD = $_POST['wpcf-price-in-primary-currency'];
			   error_log("USD");
			   error_log(print_r($regularPriceUSD,true));
			   $regularPriceGBP = $regularPriceUSD * $rateUSDtoGBP;
			   error_log("GBP");
			   error_log(print_r($regularPriceGBP,true));
			   $regularPriceEUR = $regularPriceUSD * $rateUSDtoEUR;
			   error_log("EUR");
			   error_log(print_r($regularPriceEUR,true));
			
			   add_post_meta($post_id, '_wcml_custom_prices_status', 1, true);
			// Save price values in each currency
			   add_post_meta($post_id, '_regular_price_USD', $regularPriceUSD, true);
			   add_post_meta($post_id, '_regular_price', $regularPriceGBP, true); // GBP is Default Store Currency so _regular_price is used
			   add_post_meta($post_id, '_regular_price_EUR', $regularPriceEUR, true);
			
			// Populate the calculated price fields too
			   add_post_meta($post_id, '_price_USD', $regularPriceUSD, true);
			   add_post_meta($post_id, '_price', $regularPriceGBP, true); // GBP is Default Store Currency so _price is used
			   add_post_meta($post_id, '_price_EUR', $regularPriceEUR, true);
			
		} else { error_log("Primary Currency Not Set"); }

    }
}

I display the prices on the front end using the shortcode [wpv-woo-product-price]

Originally I simply saved values to the fields _regular_price, _regular_price_USD and _regular_price_EUR, but then the shortcode did not output any value, despite the fields appearing populated in the wordpress product edit backend.

I then added code to populate the fields _price, _price_USD and _price_EUR in addition to the above. This then resulted in the secondary currency prices showing as on sale even though no sale prices were entered (they crossed out the regular price but then showed that same price as the sale price).

Please can you advise on what the issue may be?

June 6, 2019 at 8:14 am #3966931

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

Thank you for contacting WPML support!

In order to debug this issue further quickly, would you be willing to provide me with temporary WordPress admin username so I could investigate the issue directly? I have marked your next reply private so you can safely add this information.

I would like to check on how this works in the front-end, but I can see that the website is under maintenance at the moment.

https://wpml.org/purchase/support-policy/privacy-and-security-when-providing-debug-information-for-support/

Please make a full backup of your site before sharing this info!

You can use plugins such as UpdraftPlus WordPress Backup Plugin (https://wordpress.org/plugins/updraftplus/) or ask your host to perform a site backup.

Regards,
Bruno Kos

June 6, 2019 at 1:14 pm #3969349

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

I have asked our 2nd tier about this ticket - I will let you know as soon as I will get some feedback from them.

Regards,
Bruno Kos

June 6, 2019 at 2:53 pm #3970103

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

I went on to check this more thoroughly and also asked some of my Toolset support colleagues. The reason you are using this custom code is because you cannot create a Toolset form that would allow you custom prices, correct?

From what I understand, the main issue here is that the form you have coded:
hidden link
does not work as expected using the current code?

Only simple products can be created, as by default, we are not aware of any options to set different prices:
https://toolset.com/documentation/user-guides/creating-woocommerce-products-using-cred-forms/

Regards,
Bruno Kos

June 6, 2019 at 3:44 pm #3970617

jamesP-15

The form and the php (below) work successfully to add prices in each different currency to the new product, the issue is that even though the values are saved to the database, they do not display on the front end in the [wpv-woo-product-price] shortcode output. See the screenshot attached for reference.

   add_post_meta($post_id, '_wcml_custom_prices_status', 1, true);
			// Save price values in each currency
			   add_post_meta($post_id, '_regular_price_USD', $regularPriceUSD, true);
			   add_post_meta($post_id, '_regular_price', $regularPriceGBP, true); // GBP is Default Store Currency so _regular_price is used
			   add_post_meta($post_id, '_regular_price_EUR', $regularPriceEUR, true);
June 6, 2019 at 4:38 pm #3971059

jamesP-15

Adding the below fields to the php function appear to have helped progress toward a solution.

// Save _regular_price fields for each currency
			// These are the fields we see displayed in the back end
			   add_post_meta($post_id, '_regular_price', $regularPriceGBP, true); // GBP is Default Store Currency so _regular_price is used // Check Debug Log
			   add_post_meta($post_id, '_regular_price_USD', $regularPriceUSD, true);
			   add_post_meta($post_id, '_regular_price_EUR', $regularPriceEUR, true);
			
			// Save _price Fields for each currency
			// These are the fields we see displayed on the front end
			   add_post_meta($post_id, '_price_USD', $regularPriceUSD, true);
			   add_post_meta($post_id, '_price', $regularPriceGBP, true); // GBP is Default Store Currency so _price is used
			   add_post_meta($post_id, '_price_EUR', $regularPriceEUR, true);
			
			// Save _sale_price Fields for each currency except Default price (GBP)
			   add_post_meta($post_id, '_sale_price', '', true); // Check Debug Log
			   add_post_meta($post_id, '_sale_price_USD', '', true);
			   add_post_meta($post_id, '_sale_price_EUR', '', true);

The only remaining issue is that I now see the lines below in my debug log...

[06-Jun-2019 16:32:16 UTC] PHP Notice:  Undefined index: _regular_price in /home/ahoyproject/public_html/wp-content/plugins/woocommerce-multilingual/inc/currencies/class-wcml-custom-prices.php on line 507
[06-Jun-2019 16:32:16 UTC] PHP Notice:  Undefined index: _sale_price in /home/ahoyproject/public_html/wp-content/plugins/woocommerce-multilingual/inc/currencies/class-wcml-custom-prices.php on line 507

Do you know why this is happening?

June 7, 2019 at 9:46 am #3975369

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

We have examined your code and the issue in whole and we see nothing that could go wrong.

Your code needs to be properly debugged and traced from possible errors, but I am afraid that it is beyond the scope of our support.
Please consider contacting one of our certified partners from this link: http://wpml.org/documentation/support/wpml-contractors/

They should be able to do the job for you and properly test it in all scenarios. By reaching out to them, you will get the custom assistance you need to get on with your project.
Before doing so, please have a look also here: http://wpml.org/documentation/support/wpml-contractors/guidelines-for-people-looking-for-consulting-help/

Regards,
Bruno Kos