How to add a subscription for products in WordPress

Differences ,Pros and Cons of Angular JS 1,2 & 4
February 22, 2018
Step by Step Instructions to Set up an Android App Using Netbeans
February 26, 2018

How to add a subscription for products in WordPress

What is Subscription?

A subscription-based pricing model is a payment structure that allows a customer or organization to purchase or subscribe to a vendor’s IT services for a specific period of time for a set price. Subscribers typically commit to the services or products on a monthly or annual basis.

Lets see Step by step procedure

1.Install WordPress

How to add a subscription for products in WordPress?

Step 1. Download the WordPress installation package

Step 2. Upload the WordPress Files to Your Server

Step 3. Create a MySQL Database for WordPress to use

Step 4. Go through the installation process

2.Next Installing Woo-Commerce in your dashboard

To install it via your dashboard, simply go to Add Plugins and do a search for Woo-Commerce. Once it comes up, just click install.

Uploading to your computer

You can go to Woo-Themes site and download the plugin directly to your computer.

After you have downloaded it, now go to Add Plugin and Install

[box type=”alert”]Make sure the plugin stay compressed or zipped when downloading. You will be able to tell by seeing the extension at the end of it .zip[/box] Once you have uploaded it to your computer, you can now activate the plugin.

Adding Subscription Module

 After activation a message will show up at the top of your dashboard.

  • Go for themes–>woo-commerce–>single-product.php
  • Before Subscription

Add this code

<div class=”show” style=”margin-top:20px;”>

                    <input type=”checkbox” name=”show_subs” id=”show_subs”/>&nbsp;&nbsp;Add Subscription </div>

                    <div class=”subscription” id=”subscription_section”>Subscription:

                    <select id=”sub_view” name=”subs”>

                    <option value=”3″>3 Months</option>   

                    <option value=”6″>6 Months</option>

                    <option value=”9″>9 Months</option>   

                    <option value=”12″>12 Months</option> </select>

                    <span class=” title3″>

                    <label>&#8377;&nbsp;<b id=”sub_price”></b>

                    <!– <?PHP //echo $product->get_price(); ?> –>

                    </label>  </span></script><script >

                    $(document).ready(function(){

                     $(‘#subscription_section’).hide();

 

 //show it when the checkbox is clicked

   $(‘#show_subs’).on(‘click’, function () {

       if ($(this).prop(‘checked’)) {

           $(‘#subscription_section’).fadeIn();

       } else {           

           $(‘#subscription_section’).hide();

            location.reload();

                    }

 });

                        chans();   

                    $(“#sub_view”).change(function(){

                        chans();    });   

                       

                    function chans(){

                    var pro='<?php echo esc_attr($product->id); ?>’;

                    var title = ‘<?php echo $product->post->post_title?>’;   

                    var subsc = $(“#sub_view”).val();

                    var size = $(“#list_view”).val();

                    $.ajax({

                        url:”http://mypetcabin.com/yourajaxurltogetthe price/price.php”,

                        method:”POST”,

                        data:{‘pro_id’:pro,’size’:size,’subscription’:subsc,’title’:title},

                        dataType:”json”,

                        success:function(data1){

                        //console.log(data1);

                       

                        console.log(data1.price);

                        //console.log(subprice);

                        var number = parseInt(data1.price);

                        var subprice = number * subsc;

                        //console.log(number);

                    $(“#sub_price”).html(subprice);   

                    $(“#add-to-cart”).val(data1.ids);

                    }           

                    });   

                    }

                    });

                    </script>

                    </div>

  • After Subscription

This will display the dropdown in your product page with option you have mentioned in requirement.

Next Go for Themes-function.php

ADD THIS CODE

//add subscription from product pages

 

function add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {

// Has our option been selected?

if( ! empty( $_POST[‘subs’] ) ) {

     $sub = $_POST[‘subs’];

if( $sub = 1){

    $product = wc_get_product( $product_id );

$price = $product->get_price();

// Store the overall price for the product, including the cost of the warranty

$cart_item_data[‘sub_price’] = $price;

}

else{

$product = wc_get_product( $product_id );

$price = $product->get_price();

// Store the overall price for the product, including the cost of the warranty

$cart_item_data[‘sub_price’] = $price * $sub;

 $cart_item_data[‘subscription’] = $sub;

}}

    //print_r($cart_item_data);

return $cart_item_data;

}

add_filter( ‘woocommerce_add_cart_item_data’, ‘add_cart_item_data’, 10, 3 );

This will display dropdown of which product we select equal to the price and option selected from dropdown.

 

Next add this code to calculate and proceed with cart products.

function before_calculate_totals( $cart_obj ) {

// Iterate through each cart item

foreach( $cart_obj->get_cart() as $key=>$value ) {

if( isset( $value[‘sub_price’] ) ) {

$price = $value[‘sub_price’];

$value[‘data’]->set_price( ( $price ) );

}

}

}

add_action( ‘woocommerce_before_calculate_totals’, ‘before_calculate_totals’, 10, 1 );

 

add_action( ‘woocommerce_add_cart_item_data’, ‘store_warranty_custom_field’, 10, 2 );

 

function store_warranty_custom_field( $cart_item_data, $product_id ) {

   $warranty_item = $_POST[‘subs’] .” months”;

   if( !empty($warranty_item) ) {

       $cart_item_data[ ‘subscription’ ] = $warranty_item;

 

       // below statement make sure every add to cart action as unique line item

       $cart_item_data[‘unique_key’] = md5( microtime().rand() );

       WC()->session->set( ‘days_manufacture’, $warranty_item );

   }

   return $cart_item_data;

}

 

This will display the exact rate of the product relevant to the requirement we quoted.

 

// Render meta on cart and checkout

add_filter( ‘woocommerce_get_item_data’, ‘rendering_meta_field_on_cart_and_checkout’, 10, 2 );

 

function rendering_meta_field_on_cart_and_checkout( $cart_data, $cart_item ) {

   $custom_items = array();

   // Woo 2.4.2 updates

   if( !empty( $cart_data ) ) {

       $custom_items = $cart_data;

   }

   if( isset( $cart_item[‘subscription’] ) ) {

       $custom_items[] = array( “name” => __( “Subscription”, “woocommerce” ), “value” => $cart_item[‘subscription’] );

   }

   return $custom_items;

}

 

// Add the information in the order as meta data

add_action(‘woocommerce_add_order_item_meta’,’add_waranty_to_order_item_meta’, 1, 3 );

function add_waranty_to_order_item_meta( $item_id, $values, $cart_item_key ) {

   // Retrieving the product id for the order $item_id

   $prod_id = wc_get_order_item_meta( $item_id, ‘_product_id’, true );

   // Getting the warranty value for this product Id

   $warranty = get_post_meta( $product_id, ‘subscription’, true );

   // Add the meta data to the order

   wc_add_order_item_meta($item_id, ‘Subscription’, $warranty, true);

}

This code will render your data in cart and checkout for further processing and finishing Orders.

By the above process subscription can be done for the products in an monthly or yearly basis .

For further updates or information …comment your queries below or e-mail us

Leave a Reply

Please wait...

Find out how we can help you grow

Ready to take a step further?