Tag Archives: WordPress
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 } ?> |
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 } ?> |
WordPress Taxonomies: Displaying Terms as Text
While using custom post types on a recent site, I ran into a situation where I wanted to list the terms(taxonomies) associated to a post. The function get_the_terms worked great, but I wanted to list the terms as text and not URLs. The first thing that came to mind was to use “strip_tags”, but that just felt dirty… so here’s a simple solution to display a post’s terms as text and not URLs.
The Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php function get_language_terms($args) { // Get terms for post $terms = get_the_terms( $post->ID , $args ); if ( !empty( $terms ) ): // Loop over each item since it's an array foreach( $terms as $term ) { // Print the name method from $term which is an OBJECT echo $term->name; // Get rid of the other data stored in the object, since it's not needed unset($term); } endif; } ?> |
Then place this inside the loop and specify the argument for which taxonomy you are wanting to list terms for.
1 |
<?php get_language_terms('taxonomy_slug_goes_here'); ?> |
I hope this can help someone else as well!