WooCommerce

Create Coupons Programmatically

The inspiration for this tutorial was another tutorial I found where it was recommended to create WooCommerce coupons using wp_insert_post() function and update_post_meta(). When working with WooCommerce data since 3.0 version it is always recommended to use CRUD objects. And coupons are not an exception.

In order to create a coupon programmatically in code we have to use WC_Coupon object. Once the object is created we are able to use its methods for coupon configuration.

Let’s do it and begin with something simple.

Create Products Programmatically

In this tutorial I am going to guide you through the process of creating products in WooCommerce in code.

It is the most correct way of creating products in latest WooCommerce versions. So, please forget about wp_insert_post() and update_post_meta() functions. Yes, I perfectly remember that products are WordPress custom post types and product prices are post meta but it doesn’t allow us to use those functions anyway, because there is a little more than that and it could cause bugs.

In this tutorial we are going to use CRUD objects that were introduced in WooCommerce 3.0 (CRUD is an abbreviation of Create, Read, Update, Delete). The CRUD objects we are interested in right now are:

  • WC_Product_Simple – for simple products,
  • WC_Product_External – for external products,
  • WC_Product_Grouped – for grouped products,
  • WC_Product_Variable – variable ones.

We are also going to use plenty of methods of these objects, that allow us to configure our product data.

Setup Stripe Payments on your Website with Payment Intents

Almost 7 years ago I created a tutorial about Stripe integration on a website. When I created it, it seemed quite difficult to me, but now it looks easy-breezy.

The thing is that it is obsolete right now. Well, it actually works for some cards, but mainly for those cards that are operating in North America. Today our goal is to implement it with brand new Stripe API which is called Payment Intents.

And I am going to guide your through it step by step.

Payment Complete Hooks

There are three different WooCommerce hooks after payment completed I would like to talk about.

  • woocommerce_pre_payment_complete,
  • woocommerce_payment_complete,
  • woocommerce_payment_complete_order_status_$status;

All of those hooks are fired when an order is either paid or doesn’t require a payment (Cash on Delivery for example). They also apply for custom payment gateways.

Hide Payment Methods Based on Shipping Class in the Cart

As you probably know, shipping classes apply to every product individually.

What does it mean for us? It means that we have to loop through all the products in the cart in our code and check their shipping classes using either get_shipping_class() or get_shipping_class_id() methods.

WooCommerce product shipping classes
In this example I created a new shipping class “No PayPal” (in WooCommerce > Settings > Shipping > Shipping Classes), so if there are products in the cart with this shipping class we are going to deactivate PayPal payment gateway.

Also we have two options here – if any product in the cart has a specific shipping class or if all the products in the cart have the specific shipping class.

How to Find Shipping Class ID?

When I create conditions with shipping classes in my code I usually use get_shipping_class() method, so I can check against shipping classes slugs, example. Slugs can easily be found in WooCommerce > Settings > Shipping > Shipping Classes.

But what you have to use get_shipping_class_id() method? Where and how to get a specific shipping class ID then?

First of all – you can inspect the code in your browser and find it there. In order to do that – please open product edit page and find “Product data” metabox. Go to “Shipping” tab and inspect “Shipping class” dropdown element.

Redirect to a Different Thank You Page based on Payment Method

In this tutorial I will show how to perform a redirect to a custom order received paged depending on a payment gateway used by customer.