Category Archives: Tips & Techniques
Shopp: Custom Discount Price for Products
I’m not sure if anyone will ever have a need for this, but we had a random request from a client regarding price discounts in Shopp. Shopp has a built in discount function that you can set on a product by product basis, but we needed to show a 20% discount automatically based on the list price for each product.
How to:
So the first thing we did was open the product.php template (wp-content > plugins > shopp > core > model > product.php) and create a new case for the product table called “pricediscounted”. This will check the list price of a product and deduct 20%.
1 2 3 4 5 |
case "pricediscounted": if (empty($this->prices)) $this->load_data(array('prices')); $list_price = $this-> min['price'] + ($this-> min['price']*$taxrate); return money($list_price - ($list_price*0.20)); break; |
Next we wanted to show the exact amount the user would be saving. So we created another case that would calculate what 20% would amount to.
1 2 3 4 5 |
case "pricepercent": if (empty($this->prices)) $this->load_data(array('prices')); $list_price = $this->min['price'] + ($this->min['price']*$taxrate); return money($list_price - ($list_price*0.80)); break; |
Finally, here is an example of how you can display this information on your site. Simply place the following anywhere you are using the product loop.
1 2 3 |
List Price: <?php shopp('product','price'); ?> Online Price: <span><?php shopp('product','pricediscounted'); ?> A savings of: <?php shopp('product','pricepercent'); ?> |
Display Content Based on Timeframe
Post Date
Here is a quick way to display content if a post was created within a certain timeframe. For example, if you wanted to mark a post as “new”, this is one way you could accomplish this.
First we need to check the timestamp by using strtotime. This will compare the date of the post to today’s date. Then we set a range, in this example we are checking to see if the post was created in last 30 days and if it was we display the content.
1 2 3 4 |
<?php if (strtotime($post->post_date) > strtotime('-30 days')) { // Posted in the last 30 days } ?> |
Member Registration Date
Maybe you want to display information based on a user’s registration date. Here is a quick example of how using strtotime we can accomplish this.
In the example below we are simply creating a function to retrive information for the current user and then comparing the user_registered date to the current date. We are also determining if the user has registered in the last 30 days, and if so then we display the content.
1 2 3 4 5 6 7 |
<?php get_currentuserinfo(); $user_data = get_userdata($user_ID); $registered_date = $user_data->user_registered; if (strtotime($registered_date) > strtotime('-30 days')) { //User registered in the last 30 days } ?> |
Show Related Posts by Term Value
Here’s a another quick tip. I needed to create a simple WordPress query that would find related posts tagged with the same term as the current post. Also, I needed to make sure it wouldn’t include the current post in the return. This worked for me, but feel free to offer suggestions that can make this better.
The Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php global $post; $terms = get_the_terms( $post->ID , 'taxonomy_slug_goes_here', 'string'); // Insert the slug of the taxonomy in which you would like to check for related terms $current_post[] = $post->ID; if(!empty($terms)){ foreach ($terms as $term) { query_posts( array( 'taxonomy_slug_goes_here' => $term->slug, // Insert the slug of the taxonomy in which you would like to check for related terms 'showposts' => 3, // Set how many posts you would like to return 'caller_get_posts' => 1, 'post__not_in' => $current_post ) ); // Makes sure to not include the current post if(have_posts()){ while ( have_posts() ) : the_post(); $current_post[] = $post->ID; ?> Related post content goes here <?php endwhile; wp_reset_query(); } } } ?> |
WordPress: Query Posts by Taxonomy Term Value
Here’s a simple way to create a custom query for posts by term value. It’s pretty straight forward but if you see a way to improve it, feel free to let me know.
The Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php // Define the arguments for the query here. $args=array( 'taxonomy' => 'taxonomy_slug_here', 'term' => 'term_slug_here', 'post_type' => 'custom_post_type_slug_here', 'posts_per_page' => -1, 'caller_get_posts'=> 1, 'orderby'=> 'post_date', 'order'=> 'ASC', ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> Call the post content here <?php endwhile; } wp_reset_query(); ?> |
Wishlist Member: Check Membership Level
Have you ever wanted to display different content based on Wishlist Member levels in WordPress? I did. After starting development on a recent project using WLM, I had a hard time finding documentation for specific hooks or functions. I even tried contacting the dev team, but never received a reply… and their documentation is pretty much non-existent. Basically if you are wanting anything other than out-the-box functionality it’s like pulling teeth as you won’t find much out there.
Anyway, if you are running a membership site via WLM and are looking to offer your content in a drip feed on the same page, and not just on a page by page basis, here’s what you can do:
The Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php // Get the current user level from WP $user = wp_get_current_user(); // Get user levels from WishlistMembers $levels = WLMAPI::GetUserLevels($user->ID); // Then run the check for the level you want like this: if(in_array('silver', $levels)){ // Display silver level content here } elseif (in_array('gold', $levels)){ // Display gold level content here } ?> |