• My other post I hit send before I was finished writing it and I cant go back and edit it now. There were some errors in what did post as well that I needed to be corrected so forgive me for reposting it here. (I am very new to this)

    if you could possibly delete the incorrect post
    http://wordpress.org/support/topic/shipping-insurance-for-all-items-in-cart-hack?replies=1

    ***If you’re advanced or just wanna skip straight to the “how to and where to add code” feel free to skip to it.There are two ways to pull this off***

    I’d like to state I am not coder and I am very new to php but I thought I would post this information because it has been an ongoing problem with no real solution for a while now.

    My Problem:
    Basically I added my own flat rate shipping price to each of my products based on the item I’m shipping (weight size etc). I’m not using a shipping calculator and I only ship through usps and process payments through paypal. So my issue was that there was no option to add shipping insurance to the entire order (which is $1.95 through usps) at checkout. I thought maybe a variation would work but then it would be annoying if I had too many products and had to go and individually add that in to each one of them and that would also confuse people if they had multiple products in their cart. So here we go to get to my solution.

    My solution:
    I created a “Shipping Insurance” product that could be added to cart and then added to the checkout just like any other product. That created the issue of the “Shipping Insurance” product showing on the product page which I did NOT want. So I found a code to block certain product categories from showing on the product page. I put the “Shipping Insurance” product in a “shipping” category and blocked it from being displayed on the product page. Now I had the issue of somehow creating a button to add the “Shipping Insurance” to the checkout page and having the cart update to the correct price. I ended up needing to hack my wpsc_shopping_cart_page.php (in my theme folder) and I needed to hack one of my other core files to pull this off. (/plugins/wp-e-commerce/wpsc-includes/shortcode.functions.php)

    I created two ways to make this happen.

    1. A button on the checkout page that you click and it opens the “thickbox” window and displays an html page with a button to add the shipping insurance and sends you back to a refreshed checkout page with the new total. The shipping insurance is a product so it is added to the list with the rest of the items in your cart. With this popup you are also able to leave a message with the button explaining anything about the insurance you would like to. (this can be used for other things as well)

    2. Buttons on the checkout page that when clicked directly add shipping insurance and refresh the page displaying the new total.


    So here are the instructions and codes to pull this off.


    1. Create a product called “Shipping Insurance”

    -Disable shipping for product
    -Upload a product picture
    -Create a category called “shipping” and put it in that category

    2. Hide the category from showing on your product page with this code. You will place this in your theme files (wpsc-products_page.php wpsc-grid_view.php)

    I am using gridview so I edited wpsc-grid_view.php and found the code to replace at around line 60 you should see this…

    <br />
    <div class="product_grid_display group"><br />
    		<?php while (wpsc_have_products()) :  wpsc_the_product(); ?><br />
    			<div class="product_grid_item product_view_<?php echo wpsc_the_product_id(); ?>"></p>
    <p>				<?php if(wpsc_the_product_thumbnail()) :?><br />
    					<div class="item_image"><br />

    Replace it with:

    <br />
    <div class="product_grid_display group"><br />
    <?php while (wpsc_have_products()) : wpsc_the_product(); ?><br />
    <?php<br />
    $prod_id = wpsc_the_product_id();<br />
    $category = get_the_product_category( $prod_id );<br />
    //echo('<br />
    <pre>'.print_r($category,1).'</pre>
    ');<br />
    if($category[0]->term_id == 95){<br />
    //skip<br />
    } else {?></p>
    <p><div class="product_grid_item product_view_ <?php echo wpsc_the_product_id(); ?>"></p>
    <p><?php if(wpsc_the_product_thumbnail()) :?><br />
    <div class="item_image"><br />

    where it says term_id == 95)
    you need to change that to the category ID for your shipping product you created.

    You need to click save and view the page. You will get a php parse error and it will tell you the line its on It’ll be aline containing a <?php endwhile;?> before the <?php endwhile;?> code where you received the error place a <?php } ?> to close the tag.

    The error should be gone and the products in the shipping category should now be hidden from the product page.

    Now onto the fun part editing a core file /plugins/wp-e-commerce/wpsc-includes/shortcode.functions.php

    3. you will open up the file and before the closing php tag ?> you will place this code.

    /**<br />
     * Custom shortcode function added to create a button that adds product to cart and takes customer directly to checkout page.<br />
     */<br />
    function wpsc_add_and_go_direct_to_cart_shortcode( $atts ) {<br />
        // extract parameters and substitute defaults if necessary<br />
        extract( shortcode_atts(<br />
            array(<br />
                'class' => '',<br />
                    'id' => '0',<br />
                    'name' => '',<br />
                    'value' => 'Add to Cart'<br />
                    ),<br />
            $atts )<br />
            );<br />
        // build form for button<br />
        $output = array(<br />
            "<form target='_parent' action='" . get_option( 'shopping_cart_url' ) . "' method='post'>",<br />
            "<input type='hidden' name='wpsc_ajax_action' value='add_to_cart'>",<br />
            "<input type='hidden' name='product_id' value='" . $id . "'>",<br />
            "<input type='submit' id='product__submit_button' class='" . $class . "' name='" . $name . "' value='" . $value . "'>",<br />
            "</form>"<br />
            );<br />
        // return form for output<br />
        return implode( "", $output );<br />
    }</p>
    <p>add_shortcode( $tag = 'add_and_go_direct_to_cart', $func = 'wpsc_add_and_go_direct_to_cart_shortcode');<br />

    This is basically creating a custom shortcode to make a button you can name anything you want for a product (your shipping insurance) that will onclick add it to cart and take you back to the checkout page. Basically refreshing and updating the total on the check out page.

    Now we need to add the shortcode to the wpsc-shopping_cart_page.php since shortcode wont work in the php file we need to use php to make the short code work.

    4. Add this code after editing the fields for the specific product and style you want for your button. I paced this code on my checkout page right under the shipping calculator. you can take a look at my site to see a live example (DEMO LINK)

    <?php echo do_shortcode("[add_and_go_direct_to_cart class='my_button_class' id=568 name='Add Shipping' value='Add Insurance']"); ?>

    (id = your WPSC product id, class = your CSS button class, and value = your desired button label.)

    5. Your buttons should now be working. All you have to do is click them and test them out. What should happen is: on click the button should refresh the checkout page and you should see the shipping insurance in the list with the other product being purchased.

    Now if you want to add buttons on the checkout page that instead open a popup window with a messgae or any other info where you can specify the window size you can use this code.

    <a href="http://yoursite.com/yourpage/?newWindow=true&TB_iframe=true&height=100&width=200&modal=true" title="Shipping Insurance"><FORM><br />
    <INPUT Type="BUTTON" VALUE="NAME OF BUTTON"></FORM></a>

    I created pages in my wordpress with custom templates that head no header,footer or side bar.Basically a white page that only displayed the content I put into the page.

    Then I placed the custom shortcode button that we created into the wordpress page

    <br />
    [add_and_go_direct_to_cart class='my_button_class' id=568 name='Button Test' value='BUTTON NAME HERE']<br />

    I wrote a little message with the button. You can use this pop up for whatever you want it will work anywhere on wordpress on a post or page etc.

    Here is a demo of it working on my page (DEMO LINK)

    if you need help or have questions feel free.

  • The topic ‘[Plugin: Wp e-commerce] Add shipping insurance button to checkout page (hack)’ is closed to new replies.