RSS Feed

Posts Tagged ‘wp ecommerce product by id’

  1. WP eCommerce Product Search for FREE :D

    June 9, 2011 by Dave

    So I flat out refuse to pay 40 bucks for a license that does nothing but save you a couple hours time. Don’t be lazy coders!

    In a previous post I told you how I got into WP eCommerce to get products by ID.

    Now I’m going to show you how to get a product search using the normal WordPress search.

    Firstly create a file in your theme folder called loop-search.php

    Done that? Great, well done.. Now paste the below code into it

      <?php if ( have_posts ( ) ) while ( have_posts ( ) ): the_post ( )?> <i id="post-"> <a class="entry-title" href=""> <?php the_title ( )?> </a> <div class="entry-content"> <a class="product-thumb-price" href=""> <img class="product_search_thumb" src="" alt="" /> </a> <div class="product_search_description"> <p class="product-search-description-text"> <?php #Echo a snippet of the content/description echo substr( get_the_content ( ), 0, 400 ) ?> <a class="product-search-more-link" href="">[...] </p> <span class="product_search_price"> <?php echo wpsc_the_product_price ( )?> </span> <div class="search-product-buttons"> <?php echo wpsc_add_to_cart_button ( get_the_ID ( ) )?> </div> </div> </div> </li> <?php endwhile?> </ul>

    Tada! Product search, that’s probably broken for actual wordpress post searches… But its a work in progress. Only works in WP eCommerce 3.8+ as well so no bitching it doesn’t work, pull our finger out and dig/mess around


  2. WP eCommerce product by id

    June 2, 2011 by Dave

    So I’ve trawled the interwebs for days and hours now trying to find out how you bloody get products by their ID in WP eCommerce (the best WordPress eCommerce solution) and I’ve given in and just edited the core files of WP eCommerce to allow this to work.

    Basically, I edited a few functions to get the top sellers on a friends website on the front page.

    Below are the functions I edited they’re all in wp-includes/product-template.php.

    wpsc_the_product_permalink ( ) on line 716 my function now looks like this

    /**
     * wpsc product permalink function
     * @var int - The ID of the product you want the permalink for (optional)
     * @return string - the URL to the single product page for this product
     */
    function wpsc_the_product_permalink ( $id = false ) {
    	global $wp_query;
    	if ( $id != false ){
    		return get_permalink ( $id );
    	}
    	else return get_permalink ( );
    }
    

    And wpsc_the_product_title ( ) on line 684, my code now looks like this

    /**
     * wpsc the product title function
     * @var int - the product you want the name of (optional)
     * @return string - the product title
     */
    function wpsc_the_product_title ( $id = false) {
    	if ( $id != false )
    		return get_the_title( $id );
    	else return get_the_title ( );
    }
    

    For wpsc_the_product_thumbnail ( ) I just passed the third argument through so I used the function like this

    wpsc_the_product_thumbnail ( null, null, $product_id );
    

    For wpsc_the_product_price ( ) I added an extra argument to the function (I’m not posting the whole thing here, its massive) so line 387 looks like this

    function wpsc_the_product_price( $no_decimals = false, $id = false ) {
    

    I also changed line 394 to look like this

    $product_id = ( $id != false ) ? $id : get_the_ID ( );
    

    For anyone looking for a quick easy “best sellers” function in wp ecommerce use the below code. Piece of cake, I even made it a short code! WIN.

    //Popular products function
    function popular_products ( $atts ) {
    
    	//Expose the Db to the function
    	global $wpdb;
    	
    	//Get the results
    	$pp = $wpdb -> get_results ( "SELECT `prodid`, SUM(quantity) FROM `{$wpdb->prefix}wpsc_cart_contents` GROUP BY `prodid` ORDER BY `quantity` DESC LIMIT {$atts['limit']}", ARRAY_A);
    	
    	//Loop through the results
    	foreach ( $pp as $item ) {
    			//Output it
    		?>
    			<a href="<?php echo wpsc_the_product_permalink ( $item['prodid'] )?>">
    				<img alt="<?php echo wpsc_the_product_title ( $item['prodid'] )?>" src="<?php echo wpsc_the_product_thumbnail ( null, null, $item['prodid'] )?>" />
    				<span><?php echo wpsc_the_product_price ( false, $item['prodid'] )?>>
    				<?php echo wpsc_the_product_title ( $item['prodid'] )?>
    			</a>
    		<?php
    		}
    }
    
    add_shortcode ( 'dm_popular', popular_products );
    

    to use the wp ecommerce best seller shortcode just put into your posts/pages [dm_popular limit=10]