Thursday, February 8, 2024
HomeeCommerce MarketingfsBuilds: Offloading WooCommerce Checkout & Subscriptions to FastSpring (Video)

fsBuilds: Offloading WooCommerce Checkout & Subscriptions to FastSpring (Video)


Estimated learn time: 3 minutes, 38 seconds

fsBuilds options developer-built tasks displaying step-by-step examples, code snippets, and recommendation on the way to method constructing with FastSpring. The aim of this sequence is to not solely aid you construct with FastSpring, but additionally to encourage you with new and distinctive methods to leverage FastSpring’s programs to finest serve your organization. As at all times, our help and gross sales engineering specialists can be found to assist in the event you want help alongside the way in which.

Offload the Complexities of Funds, Subscriptions, and Subscription Administration to FastSpring

Job to be carried out: You wish to use WooCommerce and WordPress as a CMS, however wish to leverage a Service provider of Document to handle funds, subscriptions, and subscription administration.

For this entry, we’re that includes a construct by Topher DeRosia displaying how one can simply use FastSpring’s javascript library together with FastSpring’s webhooks to each activate subscriptions when a purchase order is made, or disable subscription entry when a fee fails whereas nonetheless utilizing WooCommerce as your CMS.

Assets

Serious about making an attempt the construct your self? We’ve put collectively the entire assets used within the video under that can assist you in your means.

Add a Subscription to Cart

PHP & Javascript

<?php
/**
 * Subscription Product Add to Cart
 *
 * @creator  Prospress
 * @bundle WooCommerce-Subscriptions/Templates
 * @model 1.0.0 - Migrated from WooCommerce Subscriptions v2.6.0
 */

outlined( 'ABSPATH' ) || exit;

international $product;

// Bail if the product is not purchasable and that is not as a result of it is restricted.
if ( ! $product->is_purchasable() && ( ! is_user_logged_in() || 'no' === wcs_get_product_limitation( $product ) ) ) {
	return;
}

$user_id = get_current_user_id();

echo wp_kses_post( wc_get_stock_html( $product ) );

if ( $product->is_in_stock() ) : ?>

	<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>

<script>
    var QueryString = perform () {
  // This perform is nameless, is executed instantly and
  // the return worth is assigned to QueryString!
  var query_string = {};
  var question = window.location.search.substring(1);
  var vars = question.cut up("&");
  for (var i=0;i<vars.size;i++) {
    var pair = vars[i].cut up("=");
        // If first entry with this title
    if (typeof query_string[pair[0]] === "undefined") {
      query_string[pair[0]] = decodeURIComponent(pair[1]);
        // If second entry with this title
    } else if (typeof query_string[pair[0]] === "string") {
      var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
      query_string[pair[0]] = arr;
        // If third or later entry with this title
    } else {
      query_string[pair[0]].push(decodeURIComponent(pair[1]));
    }
  }
  return query_string;
}();
</script>
    <script>
        jQuery( doc ).prepared(perform() {

                        var s = {};

            if(QueryString.product) {
                s = {
                    'merchandise' : [
                        {
                            'path':QueryString.product,
                            'quantity':1
                        }
                    ]
                }
            }

            if(QueryString.e mail && QueryString.fname && QueryString.lname) {
                                s.paymentContact = {};
                                s.paymentContact.e mail = QueryString.e mail;
                                s.paymentContact.firstName = QueryString.fname;
                                s.paymentContact.lastName = QueryString.lname;
                        }

            if(QueryString.coupon) {
                                s.coupon = QueryString.coupon;
            }

                        if (s) fastspring.builder.push(s);
        });
    </script>

<!--
        Initialize the Retailer Builder Library
-->
<script
        id="fsc-api"
        src="https://sbl.onfastspring.com/sbl/0.9.2/fastspring-builder.min.js"
        sort="textual content/javascript"
        data-storefront="mediaforge.check.onfastspring.com/popup-mediaforge">
</script>

<!--
        So as to add a product to the acquisition session we have to outline the info-fsc-merchandise-path-worth
        with the product path and in addition outline the info-fsc-motion the place we first Add this merchandise to the
        session after which provoke Checkout
-->
<button data-fsc-item-path-value='<?php echo esc_attr( $product->get_slug() ); ?>' data-fsc-action="Add, Checkout" >
        Subscribe Now With FastSpring
</button>

	<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>

<?php endif; ?>

Subscription Creation through FastSpring Webhook

PHP & Javascript

<?php
/**
 * Plugin Title: WooCommerce Subscription Creation
 * Description: Creation of a subscription from a webhook hit from FastSpring
 * Writer: Topher
 * Model: 1.0
 * License: GPL2+
 * License URI: https://www.gnu.org/licenses/gpl-2.0.txt
 */

if (!function_exists( 'write_log' ) ) {

    perform write_log( $log ) {
        if ( true === WP_DEBUG ) {
            if ( is_array( $log ) || is_object( $log ) ) {
                error_log( print_r( $log, true ) );
            } else {
                error_log( $log );
            }
        }
    }
}


perform fs_create_subscription() {

	$json = file_get_contents('php://enter');
	$object = json_decode($json);

	write_log( 'START TOPHER LOG' );
	write_log( 'PAYLOAD: ' . print_r( $object, true ) );
	write_log( 'END TOPHER LOG' );

}
add_action( 'wp', 'fs_create_subscription' );

Developer Documentation

You probably have one other use case you wish to discover, converse instantly with an answer engineer by signing up for a demo right here.

Braden Steel

Braden Metal

Braden is the Senior Product Advertising and marketing Supervisor for FastSpring. When he isn’t bringing new merchandise to market, he spends his time writing fantasy novels.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments