:::: MENU ::::
Browsing posts in: WooCommerce

WooCommerce get current user role

Today we talk about WooCommerce or WordPress get the current user role. In this tutorial, We focus on getting the current user group for example you need to match or check what is current user role. On the basis of that need to give a discount or show some extra text.

Let’s start with our example.

// get Current User Role
$currentUser      = wp_get_current_user();
$currentUserRole = $currentUser->roles[0];
Author: Qaisar Satti
Last Modified: February 23, 2021


WooCommerce get previous URL

Today we talk about WooCommerce or WordPress get the previous URL or referrer URL. In this tutorial, We focus on getting the previous URL for example you need to match or check what was the previous URL. Sometimes you need to perform the action according to the user’s previous page.

Let’s start with our example.

$previousUrl = esc_url(wp_get_referer());
Author: Qaisar Satti
Last Modified: February 23, 2021


WooCommerce get current url

Today we talk about WooCommerce or WordPress get the current URL. In this tutorial, We focus on getting the current URL for example you need to match or check what is current URL of the category Url or product URL or any other page URL.

Let’s start with our example.

global $wp; //get wordpres global variable
$currentUrl = home_url(add_query_arg(array(), $wp->request));
Author: Qaisar Satti
Last Modified: February 23, 2021


WooCommerce Create Order Programmatically

Today we will talk about how to create order programmatically. While working on your WooCommerce Store, You need to create an order programmatically for any reason like creating a custom form for creating the order. Instead of using the WooCommerce interface to create customers or orders, explore this tutorial to do it programmatically.

/* First need address */
$orderAddress = array(
        'first_name' => 'test', //add first name to order address
        'last_name'  => 'test', //add first name to order address
        'company'    => 'test', //add company name to order address
        'email'      => 'test@test.com', //add email to order address
        'phone'      => 123456677, //add phone to order address
        'address_1'  => 'test', //add address to order address
        'address_2'  => '',
        'city'       => 'test', //add city to order address
        'state'      => 'test', //add state to order address
        'postcode'   => 'test', //add postcode to order address
        'country'    => 'US'.  //add country to order address
    );

    $cutomOrder = wc_create_order(); // create new object for order
    $productId = 20; // set product id
    $quantity  = 2;  // set product quantity
    $cutomOrder->add_product( get_product( $productId ), $quantity ); // add product and quantity to order
    $cutomOrder->set_address( $orderAddress, 'billing' ); // set billing address
    $cutomOrder->set_address( $orderAddress, 'shipping' ); // set shipping address
    $cutomOrder->calculate_totals(); // calculate total
    update_post_meta( $cutomOrder->id, '_payment_method', 'cod' ); //set payment method
    update_post_meta( $cutomOrder->id, '_payment_method_title', 'cod' ); //set payment title
Author: Qaisar Satti
Category: WooCommerce
Last Modified: January 21, 2021


WooCommerce get order product detail programatically

Today we talk about how to get WooCommerce order product or item detail programmatically. Sometimes you need to get order product or item information regarding some tweak or change the quantity or any other kind of customization. So let start with our example.

        $orderId = 10;
        $orderDetail = new WC_Order( $order_id );
        $orderItems = $orderDetail->get_items();
        foreach ( $orderItems as $orderItem ) {
            $productId = $orderItem['product_id'];
            $productQuantity = $orderItem['qty'];
        }
Author: Qaisar Satti
Category: WooCommerce
Last Modified: January 21, 2021


WooCommerce change order status programmatically

Today we talk about how to change WooCommerce order status programmatically. Sometimes you need to change order status programmatically. There is seven default status the are available that you can use as per your requirements. So let start with our example.

$orderId = 10;
$orderDetail = new WC_Order( $order_id );
$orderDetail->update_status("wc-completed", 'Completed', TRUE);

The following are the list of available status

wc-pending   For Pending payment
wc-processing   For Processing
wc-on-hold   For On hold
wc-completed   For Completed
wc-cancelled   For Cancelled
wc-refunded   For Refunded
wc-failed   For Failed
Author: Qaisar Satti
Category: WooCommerce
Last Modified: January 21, 2021


WooCommerce get product tax information

Today we talk about how WooCommerce get product tax information? In our example, we will get product tax information like status, and tax class. So let’s start with our example.

If you want to load product information by it id then use the following code.

$p_id = 20;
$product_info = wc_get_product( $p_id );

Now you have product object. You can get tax infromation with the following code.

$product_info->get_tax_status(); // get product tax status
$product_info->get_tax_class(); // get product tax class
Author: Qaisar Satti
Category: WooCommerce
Last Modified: November 17, 2020


Pages:123