Categories
Ecommerce Uncategorized Woocommerce Wordpress

How to detect the current user has an active subscription in woocommerce?

Detect the current user has an active subscription in Woocommerce.

The question comes in mind when a user with “subscriber” role with an active subscription login and try to post a content every time he / she has to choose a package even he has an active subscription.

The same can be solved by Using WooCommerce Subscriptions dedicated conditional function. Woocommerce works like magic with its extensions and gave us ability to do with inbuilt functions.
 
So no more hesitation around the code. You don’t need to build a program to achieve that. Please refer to the following  code 

Detect the current user has an active subscription in Woocommerce

function has_active_subscription( $user_id=null ) {
    // When a $user_id is not specified, get the current user Id
    if( null == $user_id && is_user_logged_in() )
        $user_id = get_current_user_id();
    // User not logged in we return false
    if( $user_id == 0 )
        return false;

    // Get all active subscriptions for a user ID
    $active_subscriptions = get_posts( array(
        'numberposts' => 1, // Only one is enough
        'meta_key'    => '_customer_user',
        'meta_value'  => $user_id,
        'post_type'   => 'shop_subscription', // Subscription post type
        'post_status' => 'wc-active', // Active subscription
        'fields'      => 'ids', // return only IDs (instead of complete post objects)
    ) );

    return sizeof($active_subscriptions) == 0 ? false : true;
}
Or
if( has_active_subscription() ){ // Current user has an active subscription
    // do something … here goes your code

    // Example of displaying something
    echo '<p>I have active subscription</p>';
}

Or

if( has_active_subscription(26) ){ // Defined User ID has an active subscription
    // do something … here goes your code

    // Example of displaying something
    echo '<p>User ID "26" have an active subscription</p>';
}

WooCommerce Subscriptions allows you to introduce a variety of subscriptions for physical or virtual products and services. Create product-of-the-month clubs, weekly service subscriptions or even yearly software billing packages. Add sign-up fees, offer free trials, or set expiration periods.

A subscription-based model will allow you to capture more residual revenue — and all you have to do is ship the orders.

Why Use WooCommerce Subscriptions?

( Read More )

 

Read More Posts

Categories
Uncategorized

How to use ob_start() function in php?

PHP | ob_start() Function

PHP is an interpreted language thus each statement is executed one after another, therefore PHP tends to send HTML to browsers in chunks thus reducing performance. Using output buffering the generated HTML gets stored in a buffer or a string variable and is sent to the buffer to render after the execution of the last statement in the PHP script.
But Output Buffering is not enabled by default. In order to enable the Output Buffering one must use the ob_start() function before any echoing any HTML content in a script.

function jassi
{
ob_start();
echo “Hello there!”;
$output = ob_get_contents();
ob_end_clean();
}

OR

function jassi
{
ob_start();
echo “Hello there!”;
$obc = ob_get_contents();
ob_end_clean();
return $obc;
}

Categories
Ecommerce Uncategorized Woocommerce Wordpress

How to check user is logged in woocommerce and wordpress?

Check user is logged in woocommerce and wordpress.

Are you looking for a way to check if user is logged in and show them different content? While there’s probably a plugin for this, we have created a quick code snippet that you can use to check if user is logged in WordPress.

if ( is_user_logged_in() )

{  

 // your code for logged in user

}

else {  

 // your code for logged out user

}

Example

Check user is logged in woocommerce and wordpress

<?php if ( is_user_logged_in() )
 {
 ?>
    <a href="<?php echo wp_logout_url(); ?>">Logout</a>
<?php 
}
 else 
{

 ?>
    <a href="/wp-login.php" title="Members Area Login" rel="home">Members Area</a>
<?php
 }
 ?>

Check it for more imformation ( click me )

 

What are the different types of WooCommerce login?

By ‘WooCommerce login’, I’m talking about the process where customers log into your online store. It’s quite a vague term and there are several ways do to set it up. Which one you choose depends on how you want your store to work.

  1. Public WooCommerce store with guest checkout – This is the most common WooCommerce setup. Anyone can browse your products and there is no hidden WooCommerce content. Customers can buy products as a guest without having to login, or they can optionally create an account during checkout.
  2. Public WooCommerce store with mandatory user accounts – Lots of WooCommerce stores can be browsed by anyone, but customers must register or login to their account in order to purchase.
  3. Public store with WooCommerce social login – To increase convenience for customers, you can let them log into your website using their social media profiles such as Google, Facebook or Twitter.
  4. Protected product categories – Instead of (or as well as) individual customer accounts, you can password protect parts of a WooCommerce store or restrict them to specific users or user roles. Anyone can buy from the public areas of your shop, while only customers with the right credentials can access your protected categories. Once a customer logs into your protected categories, they can view and buy the hidden products inside.
  5. 100% private WooCommerce store – Some websites want to hide a whole WooCommerce store from public view so that customers must login or enter a password to access it.
  6. WooCommerce auto-login links – Let customers login more easily by clicking on a personal login URL. No more usernames or passwords!

Read More Posts

Categories
Uncategorized

How to add cart product in cart with link (URL) in woocommerce?

Add cart product in cart with link (URL) in woocommerce

href=”http://yourtsite.com/?add-to-cart=3083″


href=”http://yourtsite.com/?add-to-cart=3084&quantity=3″

Add cart product in cart with link (URL) in woocommerce

How to create custom “Add to Cart” URLs in WooCommerce?

Simple Products: Add to Cart URL

One of the easiest among them. Simple products are quite easy to add to cart through a custom URL. Just search the product ID by going to product title under WooCommerce>>Products and use the given below links. You can see shown below image.

[image source file: www.businessbloomer.com]

URL: Add one simple product to cart

href=”http://yourdomain.com/?add-to-cart=25″

  • Remember to change the domain name in the above link, and the button will work out of the box. 1 product with id = 25 will be added to the cart.

URL: Add one simple product to cart with quantity = 3

href=”http://yourdomain.com/?add-to-cart=25 &quantity=3″

  • 1 product with ID = 25 and quantity = 3 will be added to the cart.
  • Note: Remember that 2 different products cannot be added to a cart with a URL.

URL: Add one simple product to cart & redirect to cart afterwards

href=”http://yourdomain.com/cart/?add-to-cart=25″

  • In case you have changed the URL for the cart, so make sure that you will have to change “/cart/” into “/basket/” – for example.
  • For this, you will have tick the “Enable AJAX add to cart buttons on the archives” option under WooCommerce>>Settings>>Products>>General.

URL: Add one simple product to cart & redirect to checkout afterwards

href=”http://yourdomain.com/checkout/?add-to-cart=25″

  • For this , tick the ” Enable AJAX add to cart buttons on archives” option under WooCommerce>>Settings>>Products>>General.
  • And disable “Redirect to cart page after successful addition”.

URL: Add one simple product to cart & redirect to any page afterwards

href=”http://yourdomain.com/your_custom_page/?add-to-cart=25″

  • For this, you have to Enable the AJAX add to cart buttons on archives” option under WooCommerce>>Settings>>Products>>General.
  • And disable “Redirect to the cart page after successful addition”.

Variable Products: Add to Cart URL

Here things get quite difficult!

There are 1 or more attributes and a quantity in the variable products. And you will have to find the variation ID, attribute NAME slug and the attribute TERM SLUG along with finding the product ID.

You can see the images shown below!

As you will have a product ID, variation ID, and its specific properties, you can use the following:

URL: Add one variable product to cart (with 1 attribute only)

href=”http://yourdomain.com/?add-to-cart=47&variation_id=88&attribute_pa_colour=blue”

  • Here we are adding Variable Product ID 47, and especially its variation ID = 88, where the color attribute is equal to blue.
  • It’s a bit complicated but once you will do it becomes easy.

URL: Add one variable product to cart (with 2 attributes)

href=”http://yourdomain.com/?add-to-cart=47&variation_id=88&attribute_pa_colour=blue&attribute_pa_size=m”

  • Here we are adding Variable Product ID 47, and especially its variation ID = 88, where the color attribute is blue and size attribute “M”.

URL: Add one variable product to cart (with 2 attributes and Quantity = 3)

href=”http://yourdomain.com/?add-to-cart=47&variation_id=88&quantity=3&attribute_pa_colour=blue&attribute_pa_size=m”

  • In addition, you can also redirect to cart, checkout, and another page in the same way as we have discussed for the simple product page.

URL: Add one variable product to cart & redirect to cart

href=”http://yourdomain.com/cart/?add-to-cart=47&variation_id=88&attribute_pa_colour=blue”

URL: Add one variable product to cart & redirect to checkout

href=”http://yourdomain.com/checkout/?add-to-cart=47&variation_id=88&attribute_pa_colour=blue”

URL: Add one variable product to cart & redirect to any page

href=”http://yourdomain.com/any-page-url/?add-to-cart=47&variation_id=88&attribute_pa_colour=blue”

click here for more information

Read More Posts

Categories
Uncategorized

How to Make Billing and Shipping Fields Optional and required in WooCommerce?

Make Billing and Shipping Fields Optional and required in WooCommerce

 
For not required
  1. country
  2. first_name
  3. last_name
  4. company
  5. address_1
  6. address_2
  7. city
  8. state
  9. postcode
add_filter( 'woocommerce_default_address_fields' , 'optional_default_address_fields' );
 function optional_default_address_fields( $address_fields ) {
 $address_fields['company']['required'] = false;
 $address_fields['postcode']['required'] = false;
 $address_fields['city']['required'] = false;
 $address_fields['state']['required'] = false;
 return $address_fields;
 }
For required
add_filter( 'woocommerce_default_address_fields' , 'optional_default_address_fields' );
 function optional_default_address_fields( $address_fields ) {
 $address_fields['company']['required'] = true;
 $address_fields['postcode']['required'] = true;
 $address_fields['city']['required'] = true;
 $address_fields['state']['required'] = true;
 return $address_fields;
 }

Make Billing and Shipping Fields Optional and required in WooCommerce

t looks like all of the WooCommerce checkout fields are added in the billing form template but I’m not sure where they’re coming from. Is there an easy way to change which of these fields are required? I don’t need the phone, but I do need the company field instead and I’m not seeing where the fields are.


The WooCommerce checkout fields are generated outside of the checkout template, so we have to do a bit of digging to find them. At the top of the billing form template, we notice that we’re using the $checkout global: @global WC_Checkout $checkout

This tells us we need to look at the WC_Checkout class to find where these fields are being generated instead, and we’ll go down a tiny rabbit hole 🙂 .

The checkout class defines the checkout fields for billing and shipping, and it pulls them from WC()->countries->get_address_fields. This means we have to dig down one more level, and go to /includes/class-wc-countries.php. As these fields may change by country or state / province, they’re generated here when we check the country, and then the checkout template outputs the right fields in the right order for the selected country.

The first thing I’m going to do is find the “company” field. I do a bit of searching, and find that it’s generated by get_default_address_fields, and can be modified by the woocommerce_default_address_fields filter, which passes in all of these fields.

Click here to get more information

Read More Posts

Categories
Html/Css Jquery PHP Uncategorized

How to submit form with anchor tag (link) in jquery?

 Submit form with anchor tag (a and link) in jquery
<form method="get" action="">
<input type="text" name="ID" value="jassi">
<input type="text" name="con" value="singh">
<a href="" onclick="this.closest('form').submit();return false;" value="CONSTRUCTION CONTRACT">CONSTRUCTION CONTRACT</a>
</form>

 Submit form with anchor tag (a and link) in jquery

 

Read More Posts

Categories
Uncategorized

How to sorting in descending and ascending in sql?

Sorting in descending order in SQL

Descending

“select * from $dealtable where user_id=’$uid’ AND status=’$status’ ORDER BY ID DESC”

Ascending

“select * from $dealtable where user_id=’$uid’ AND status=’$status’ ORDER BY ID ASC”

Categories
Uncategorized

How to Prevent negative inputs in form input type=“number”?

Prevent negative inputs in form input type=“number”?


<html>
<body>
<form action=”#”>
  <input type=”number” name=”test” min=0 oninput=”validity.valid||(value=”);”><br>
  <input type=”submit” value=”Submit”>
</form>
</body>
</html>
Categories
Uncategorized

How to trigger (use) an event when contenteditable is changed in jQuery?

Trigger an event when contenteditable is changed



<div class=”changeable” contenteditable=”true”> Click this div to edit it </div>
jQuery(document).ready(function($){
var contents = $(‘.changeable’).html();
$(‘.changeable’).blur(function() {
    if (contents!=$(this).html()){
        alert(‘Handler for .change() called.’);
        contents = $(this).html();
    }
});
});
Categories
Uncategorized

How to get warning when exit browser without saving?

Get warning when exit browser without saving

My website is www.wpcodekit.com

<script>
jQuery(document).ready(function($){

$(window).unbind(‘beforeunload’);

$(“input[type=’submit’]”).click(function(){
     $(window).unbind(‘beforeunload’);
});

$(“input”).on(“change paste keyup”, function() {

  $(window).bind(‘beforeunload’, function(){
return ‘Are you sure you want to leave?’;
  });

});

var contenteditable = document.querySelector(‘[contenteditable]’),
    text = contenteditable.textContent;

$(contenteditable).each(function(){

$(this).on(“change paste keyup”, function() {

$(window).bind(‘beforeunload’, function(){
return ‘Are you sure you want to leave?’;
});

});
});

$(“input[type=’submit’]”).click(function(){
     $(window).unbind(‘beforeunload’);
});

});
</script>