Sauter la navigation

[Résolu] Nested shortcode inside Cred

This support ticket is created Il y a 5 années et 10 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Ce sujet contient 10 réponses, a 2 voix.

Dernière mise à jour par Pat Il y a 5 années et 10 mois.

Assisté par: Christian Cox.

Auteur
Publications
#1209161

Pat

Hello,

I'm using Toolset + Woocommerce + Role Based Pricing plugin.
Everything works fine and I can display the price related to a role in a content template.

Now, I want to use the role based price as a value of a Cred field (in a Cred form) and I'm facing an issue.

The shortcode that is available for displaying the Role Based Pricing is :

[wc_rbp id='[wpv-post-id]' role='current' price='regular_price'] where 'current' represent the role of the current loggedin user.

If I try to use it inside a Cred field :

[cred_field field='tarif-enseignement' force_type='field' class='form-control' value='[wc_rbp id="[wpv-post-id]" role="current" price="regular_price"]' output='bootstrap']

Then the result I'm getting is the following :

<span class="woocommerce-Price-amount amount">47,00<span class="woocommerce-Price-currencySymbol">€</span></span>

It seems that the RBP plugin outputs the price and the currency and thus, have specific classes that are plugged around.

Is there a way to convert this into a numeric value (ie : keep only the 47,00 and forget everything else? My Cred field is a numeric one and should be numeric as I need to make additional operations then after.

Thanks for helping
Regards
Pat

#1209604

There's nothing built-in to Toolset that will strip out all the formatting and extra markup produced by a third-party shortcode. This formatting must be removed if you want to use it in a numeric field. Maybe the role-based pricing plugin offers a way to get just the numeric value instead of the full formatted text? If not, then some custom PHP formatting is probably required.

#1209866

Pat

Hi Christian,

Thanks for your answer.
No problem to code with some php. The only issue is that I'm not sure how the parameter is treated inside the "value" parameter. Let me give you an example. I have first created a shortcode [nettoyage] thanks to this function :
function nettoyage_rbp( $att, $content = "" ) {
$restg='';
$restg = substr($content, 46);
return $restg;
}
add_shortcode( 'nettoyage', 'nettoyage_rbp' );

Here is the code I have placed inside the Cred form :

[cred_field field='tarif-enseignement' force_type='field' class='form-control' value='[nettoyage][wc_rbp id="[wpv-post-id]" role="current" price="regular_price"][/nettoyage]' output='bootstrap']

Normally, the $content should return "<span class="woocommerce-Price-amount amount">47,00<span class="woocommerce-Price-currencySymbol">€</span></span>" and with this function, I should get rid of the left part of the string and have "47,00<span class="woocommerce-Price-currencySymbol">€</span></span>" as a result.

There is no error reported in the function but the result inside the value is "" !

Can you give me some advice to move forward?
Thanks
Pat

#1210120

You can try registering nettoyage and wc_rbp in Toolset > Settings > Third-party shortcode arguments, but I'm not sure if it will help because you have so many nested levels of shortcodes in shortcode attributes. Ideally, there should be only one shortcode inside the value attribute, and it should not have any contents nested inside it. So your nettoyage shortcode should perform the wc_rbp shortcode function, without the need for nested shortcodes.

#1210176

Pat

Hi Christian,

That was the first thing I have made to register the 2 shortcodes, but this is not changing anything.
I can perhaps explain my aim here :
- I have defined some custom product fields. Then, I have a Cred commerce form that is used to create a news 'inscription post' each time a product is bought (the Cred commerce is linked to the dedicated product ID it is displayed on.
.
The group field used for the products is also affected to this 'inscription post'.

I want to retrieve all fields from the selected product and populate the same fields of the 'inscription post'. That's working fine for all standard fields, but I'm facing this issue for this specific field (which is very important as is is the only way to keep a trace of the price paid (we are using different prices depending of the user role and the products have price evolution from one year to another).

The tricky thing is that the rbp shortcode displays the right thing when placed on a page (ie : the price + currency and of course, no classes elements).

Another point is that it is not only linked to rbp plugin. If you use the standard [wpv-woo-product-price] shortcode as the "value", then you're getting the same issue (all classes elements are displayed around the price itself).

So, the question is : is there a way to transform WC prices into numbers?

Let me know
Regards
Pat

#1210262

So, the question is : is there a way to transform WC prices into numbers?
I'm not sure because you're using a 3rd-party plugin to allow different prices for different User roles.

#1210785

Pat

Hi Christian,

I will not say this as Woocommerce is for longs years, integrated into Toolset's strategy !
After some investigation, this is not an issue of third party plugin but and issue between Toolset and Woocommerce.

As mentioned, if I'm trying to use the Views Woocommerce shortcode [wpv-woo-product-price] as the value of the Cred field,, then, I'm getting the same issue ! It seems that the RBP plugin is in line with the WC way of displaying prices.

So my question is how to place a WC price inside a Cred field value?

Regards
Pat

#1210812

I can show you how to insert the _regular_price or _sale_price postmeta value for a simple product in a Form field:

[cred_generic_field type='numeric' field='generic-price']
{
"required":0,
"validate_format":0,
"default":"[wpv-post-field name='_regular_price' id='12345']"
}
[/cred_generic_field]

Replace 12345 with the simple product's ID.

#1210914

Pat

Hi Christian,

Thanks for the info.
I can get the price inside the Cred field with your solution, but I need to use this shortcode : [wpv-woo-product-price] as this is the one which have the right value in this case.
Any idea how I can get it work?
Regards
Pat

#1211397

It is not possible to produce a simple numeric value with the wpv-woo-product-price shortcode. It only produces formatted markup. In the documentation, there are no formatting attributes for this shortcode, so you cannot customize the format produced by the shortcode. There is not even an "id" attribute that allows you to display the price of an arbitrary product, so it's not very flexible: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-woo-product-price

If the variable pricing plugin offers an API to give you the numeric value, you might be able to use the Forms API to perform calculations on the backend without using the price in your Form.

add_action('cred_save_data', 'your_save_data_action',10,2);
function your_save_data_action($post_id, $form_data)
{
  // if a specific form
  if ($form_data['id']==12345)
  {
    $product_id = 67890;
    $price = flexible_price_plugin_api( 67890 ); // this is an example api
    // do something with  $price
  }
}

If the flexible price plugin does not offer an API, I'm not sure. Normally you can access a WooCommerce product's price using the WooCommerce API like this:

$product_id = 12345;
$price = wc_get_product( $product_id ) ) ? $product->get_price() : false );
#1211402

Pat

Hi Christian,

In themeantime, I have continue to analyse the issue and founded the postmeta where the different role based price where recorded.
So, it's quite easy to get them back thanks to a function and define therole for which you want the price.

If anybody interested in this, here isthe code I have used :

function nettoyage_rbp( $atts ) {

$user = wp_get_current_user();
$roles = ( array ) $user->roles;
global $post;
$post->ID;
$post_id = $post->ID;
$role = $roles[0];
$currency ='';
$price = get_post_meta($post_id, '_role_based_price', false);
return $price[0][$role]['regular_price'];

}
add_shortcode( 'nettoyage', 'nettoyage_rbp' );

And then, place the shortcode [nettoyage] as the value of the generic field !

Thanks for you support
Pat