Skip Navigation

[Resolved] Display onsale custom shortcode

This thread is resolved. Here is a description of the problem and solution.

Problem:
Display onsale custom shortcode

Solution:
To get the product object based on the current post in the view's loop, I've added the following line of code as you can see with the above shortcode:

$product = wc_get_product($post->ID);    

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/display-onsale-custom-shortcode/page/2/#post-1779645

Relevant Documentation:

This support ticket is created 3 years, 7 months ago. 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 16 replies, has 2 voices.

Last updated by ralphG 3 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#1769783
PrtScr capture_5.jpg

Tell us what you are trying to do?
I have created a shortcode which takes the product regular price and sale price, and then display the percentage of the promotion:

function show_onsale_percentage_shortcode() {
	global $product;
	$percent = 0;
	if($product){
		if ( $product->is_on_sale() && $product->is_type('simple') ) {
			$percent = floor((1 - ( $product->get_sale_price() / $product->get_regular_price() )) * 100);
		}	
	}
	return $percent;
}
add_shortcode('onsale-percent', 'show_onsale_percentage_shortcode');

The shortcode is then displayed conditionally in the view if there is a promotion.

[wpv-if onsale="views_woo_on_sale" evaluate="$onsale = '1'"]
      	<div class="promo-tag">
          <span>-[onsale-percent]%</span>
        </div>
[/wpv-if]

It seems to work correctly except that the value of the percentage is displayed on the next product of the loop, please see screenshot for better understanding.

Is there any documentation that you are following?
https://toolset.com/forums/topic/calculate-woo-fields/
https://toolset.com/documentation/user-guides/views/woocommerce-views-calculated-fields-and-batch-update/

Is there a similar example that we can see?
No.

What is the link to your site?
It's a development website.

#1770497

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

First of all - I would like to pay your attention to the [wpv-if] conditional shortcode. This shortcode is legacy shortcode.

To display the conditional content, you should use the shortcode [wpv-conditional]. Please chech the following doc for more info:
=> https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/using-shortcodes-in-conditions/#checking-custom-shortcodes

For example:

[wpv-conditional if="( '[wpv-post-field name='views_woo_on_sale']' eq '1' )"]
<div class="promo-tag">
          <span>-[onsale-percent]%</span>
        </div>
[/wpv-conditional]

Can you please try and check if the above solution help you to resolve your issue.

#1770935

Hi,
thanks for pointing this out.

I updated the code but the problem still persist.

#1771075

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

That is strange.

Can you please share problem URL as well as access details. Also, please share where you added the custom shortcode you created.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1772139

Hello,
I updated the private message with more information.

#1773029

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm on content template listing page bug I do not see the content template you mentioned "Loop item to display product" in your reply:
=> hidden link

Can you please share the link of your content template.

#1773233
PrtScr capture_6.jpg

Hi,
Indeed, I don't know why the content template don't appear.

But you can still access it in the views (please see screenshot).

#1773291

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

are you making any changes as I do not see any login form with the subsite link: hidden link

It shows 404 page not found error.

#1773301

Ok, try with this url :
hidden link

#1773303

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - that link works now.

But the following link shows - 404 page not found error.
hidden link

#1773317

Yes, I just noticed that.
I just updated wpml plugins this morning, it might be the culprit.
Can you log off while I restore a backup please ?

#1773319

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - I logout from admin.

#1773579

Hi,
sorry the backup failed, but it's working now,
you can login again.

And it is solved for the 404 error, permalink settings changed... It's now on "%postname%" again.

#1777165

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you for the update.

However - when I try to login to admin section, it shows error "Username or password wrong!". Can you please send me working admin access details.

I have set the next reply to private which means only you and I have access to it.

#1778855

Hi, did you found a solution ?

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.