Pages

Dec 7, 2022

How to Connect your Wordpress website to CakePHP Application

WordPress is a popular content management system (CMS) that allows users to easily create and manage websites. CakePHP is an open-source PHP framework that makes it easy to build web applications.

In this blog post, we will look at how to connect WordPress to CakePHP so that you can use the powerful features of both platforms to build powerful and flexible web applications.

First, you will need to install and set up WordPress on your web server. You can find detailed instructions on how to do this in the WordPress documentation.

Next, you will need to install and set up CakePHP on your web server. You can find detailed instructions on how to do this in the CakePHP documentation.

Once you have installed and set up both WordPress and CakePHP, you can connect them by following these steps:

  1. In your WordPress site, go to the plugin directory and search for the "WordPress REST API" plugin. Install and activate this plugin, which will enable the WordPress REST API on your site.

  2. In your CakePHP application, create a new controller and add the following code to it:


// Import the CakePHP HttpClient library
use Cake\Http\Client;

// Define the WordPress site URL
$siteUrl = 'https://www.example.com/';

// Create a new instance of the HttpClient
$http = new Client();

// Make a request to the WordPress site using the HttpClient
$response = $http->get($siteUrl);

// Check the response status code
if ($response->getStatusCode() == 200) {
  // Parse the response body as JSON
  $data = json_decode($response->getBody());

  // Output the data
  debug($data);
}

This code uses the CakePHP HttpClient library to make a request to the WordPress site and parse the response as JSON data. You can then access the data and use it in your CakePHP application.

With this approach, you can easily connect WordPress to CakePHP and use the data and functionality of both platforms to build powerful and flexible web applications.





Oct 7, 2022

Check if cart contains only one product of a perticular category

 

You can search entire cart and checks if it contains only one product of a particular category.

Here suppose your category slug is 'your-category-slug'


Here is a simple function you can place this in your plugin or theme function file


function check_category_alone_in_cart( $category ) {

        

    // check each cart item for our category

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

        

        // if a product is not in our category, bail out since we know the category is not alone

        if ( ! has_term( $category, 'product_cat', $cart_item['data']->id ) ) {

            return false;

        }

    }

        

    // if we're here, all items in the cart are in our category

    return true;

}



function chk_wc_prevent_checkout_for_category() {

    //  If the cart is empty, then let's hit the ejector seat

    

    if (WC()->cart->is_empty()) {

        return;

    }       


    // set the slug of the category for which we disallow checkout

    $category = 'your-category-slug';

    

    // get the product category

    $product_cat = get_term_by( 'slug', $category, 'product_cat' );

    

    // sanity check to prevent fatals if the term doesn't exist

    if ( is_wp_error( $product_cat ) ) {

        return;

    }

    

    // check if this category is the only thing in the cart

    if ( check_category_alone_in_cart( $category ) ) {

        

        // render a notice to explain why checkout is blocked

        wc_add_notice(  'Cart contains the product which has category "your-category-slug" !!! ', 'error' );

return false;

    }  

return true;

}


add_action( 'woocommerce_check_cart_items', 'chk_wc_prevent_checkout_for_category' );