Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Plugin Name: Business Bloomer WooCommerce: Create an order on another website
- * Description: Create a order on another website when an order is created here.
- * Plugin URI: https://www.businessbloomer.com/shop/plugins/woocommerce-create-order-from-another-website/
- * Update URI: https://www.businessbloomer.com/shop/plugins/woocommerce-create-order-from-another-website/
- * Author: Business Bloomer
- * Author URI: https://www.businessbloomer.com
- * Text Domain: bbloomer-woocommerce-create-order-from-another-website
- * Requires Plugins: woocommerce
- * Version: 0.1.20231024
- */
- defined( 'ABSPATH' ) || exit;
- include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
- if ( ! is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
- return;
- }
- define( 'BBWCOFAW', 'https://www.businessbloomer.com/wp-json/bb/v1/downloads?product_id=XXXXXX' );
- add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'bbwcofaw_action_links', 9999, 4 );
- function bbwcofaw_action_links( $links, $plugin_file, $plugin_data, $context ) {
- if ( ! isset( $plugin_data ) ) return $links;
- $plugin_links = array(
- '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=advanced#bbwcofaw-start' ) . '">' . __( 'Settings', 'woocommerce' ) . '</a>',
- '<a href="' . $plugin_data['PluginURI'] . '#tab-docs" target="_blank">' . __( 'Docs', 'woocommerce' ) . '</a>',
- '<a href="https://businessbloomer.clarityflow.com/i/intake?title=' . rawurlencode( $plugin_data['Name'] ) . rawurlencode( ' ' ) . $plugin_data['Version'] . '" target="_blank">' . __( 'Premium Support', 'woocommerce' ) . '</a>',
- '<a href="' . $plugin_data['PluginURI'] . '#tab-reviews" target="_blank">' . __( 'Add a review', 'woocommerce' ) . '</a>',
- );
- return array_merge( $plugin_links, $links );
- }
- add_filter( 'plugin_row_meta', 'bbwcofaw_hide_view_details', 9999, 4 );
- function bbwcofaw_hide_view_details( $plugin_meta, $plugin_file, $plugin_data, $status ) {
- if ( isset( $plugin_data['TextDomain'] ) && $plugin_data['TextDomain'] == plugin_basename( __DIR__ ) ) unset( $plugin_meta[2] );
- return $plugin_meta;
- }
- add_filter( 'plugins_api', 'bbwcofaw_plugin_view_version_details', 9999, 3 );
- function bbwcofaw_plugin_view_version_details( $res, $action, $args ) {
- if ( 'plugin_information' !== $action ) return $res;
- if ( $args->slug !== plugin_basename( __DIR__ ) ) return $res;
- $response = wp_remote_get( BBWCOFAW, array( 'headers' => array( 'Accept' => 'application/json' ) ) );
- if ( ( ! is_wp_error( $response ) ) && ( 200 === wp_remote_retrieve_response_code( $response ) ) ) {
- $download = json_decode( wp_remote_retrieve_body( $response ), true );
- } else return $res;
- $res = new stdClass();
- $res->name = $download['name'];
- $res->slug = plugin_basename( __DIR__ );
- $res->path = plugin_basename( __DIR__ ) . '/' . plugin_basename( __DIR__ ) . '.php';
- $res->version = $download['version'];
- $res->download_link = $download['download_link'];
- $res->sections = array(
- 'description' => $download['description'],
- );
- return $res;
- }
- add_filter( 'update_plugins_www.businessbloomer.com', function( $update, array $plugin_data, string $plugin_file, $locales ) {
- if ( $plugin_file !== plugin_basename( __DIR__ ) . '/' . plugin_basename( __DIR__ ) . '.php' ) return $update; if ( ! empty( $update ) ) return $update;
- $response = wp_remote_get( BBWCOFAW, array( 'headers' => array( 'Accept' => 'application/json' ) ) );
- if ( ( ! is_wp_error( $response ) ) && ( 200 === wp_remote_retrieve_response_code( $response ) ) ) {
- $download = json_decode( wp_remote_retrieve_body( $response ), true );
- } else return $update;
- if ( ! version_compare( $plugin_data['Version'], $download['version'], '<' ) ) return $update;
- return [
- 'slug' => plugin_basename( __DIR__ ),
- 'version' => $download['version'],
- 'url' => $plugin_data['PluginURI'],
- 'package' => $download['download_link'],
- ];
- }, 9999, 4 );
- // Return the wp_options option names - used in a number of functions.
- function bbwcofaw_settings_option_name() {
- return 'bb_cofaw';
- }
- // Verify and save the information submitted in the above form.
- add_action( 'admin_init', 'bbwcofaw_save_api_details' );
- function bbwcofaw_save_api_details() {
- if ( isset( $_GET['page'], $_GET['tab'], $_POST['consumer_key'], $_POST['consumer_secret'], $_POST['order_status'], $_POST['website'], $_POST['createdby'], $_POST['_wpnonce'] )
- && 'wc-settings' == $_GET['page'] && 'advanced' == $_GET['tab'] && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-settings' ) ) {
- if ( current_user_can( 'manage_woocommerce' ) ) {
- $options = array( 'consumer_key' => '', 'consumer_secret' => '', 'website' => '', 'createdby' => '', 'order_status' => 'completed', 'product_id' => 0 );
- $options['consumer_key'] = sanitize_text_field( $_POST['consumer_key'] );
- $options['consumer_secret'] = sanitize_text_field( $_POST['consumer_secret'] );
- $options['website'] = sanitize_text_field( $_POST['website'] );
- $options['createdby'] = sanitize_text_field( $_POST['createdby'] );
- $options['order_status'] = sanitize_text_field( $_POST['order_status'] );
- $options['product_id'] = sanitize_text_field( absint( $_POST['product_id'] ) );
- update_option( bbwcofaw_settings_option_name(), $options );
- }
- }
- }
- // Settings section displayed in WooCommerce/Settings/Advanced.
- add_action( 'woocommerce_settings_advanced', 'bbwcofaw_api_settings_fields', 20 );
- function bbwcofaw_api_settings_fields( $email_obj ) {
- $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . plugin_basename( __FILE__ ) );
- $quick_links = bbwcofaw_action_links( array(), '', $plugin_data, '' );
- array_shift( $quick_links );
- if ( !isset( $_GET['section'] ) || empty( $_GET['section'] ) ) {
- echo '<div id="bbwcofaw-start" style="border: 2px dashed red; background: #fae6e6; padding: 0 2em 1em;"><h2>', $plugin_data['Name'], ' <small>v ', $plugin_data['Version'], '</small></h2>';
- echo '<p>' . $plugin_data['Description'] . '</p>';
- echo '<h4>Quick links: ' . implode( ' - ', $quick_links ) . '</h4>';
- echo '<table class="form-table"><tbody>';
- $default_options = array( 'consumer_key' => '', 'consumer_secret' => '', 'website' => '', 'createdby' => '', 'order_status' => 'completed', 'product_id' => 0 );
- $stored_options = get_option( bbwcofaw_settings_option_name(), array() );
- $bbaccpc_options = wp_parse_args( $stored_options, $default_options );
- // Display help for creating Consumer key and secret.
- echo '<details><summary><strong>', __( 'How to create a Consumer key and secret', 'bbloomer-woocommerce-create-order-from-another-website' ). '</strong></summary>';
- echo '<p>', __( 'Read the <a href="https://woocommerce.github.io/woocommerce-rest-api-docs/#rest-api-keys" target="_blank">WooCommerce documentation</a> and then copy the Consumer key and secret to the fields below.', 'bbloomer-woocommerce-create-order-from-another-website' ), '</p>';
- echo '</details>';
- $options = array();
- // Password field to specify consumer key from the website that will receive the order.
- $args = array(
- 'title' => __( 'Consumer key', 'woocommerce' ),
- 'desc' => __( 'Enter the Consumer key created on the website that will receive the order', 'bbloomer-woocommerce-create-order-from-another-website' ),
- 'id' => 'consumer_key',
- 'placeholder' => __( 'Enter Consumer key', 'bbloomer-woocommerce-create-order-from-another-website' ),
- 'type' => 'password',
- 'value' => $bbaccpc_options['consumer_key'],
- 'autoload' => false,
- );
- $options[] = $args;
- // Password field to specify consumer secret from the website that will receive the order.
- $args = array(
- 'title' => __( 'Consumer secret', 'woocommerce' ),
- 'desc' => __( 'Enter the Consumer secret created on the website that will receive the order', 'bbloomer-woocommerce-create-order-from-another-website' ),
- 'id' => 'consumer_secret',
- 'placeholder' => __( 'Enter Consumer secret', 'bbloomer-woocommerce-create-order-from-another-website' ),
- 'type' => 'password',
- 'value' => $bbaccpc_options['consumer_secret'],
- 'autoload' => false,
- );
- $options[] = $args;
- // URL field to specify the website that will receive the order.
- $args = array(
- 'title' => __( 'Site Address (URL)' ),
- 'desc' => __( 'Enter the url of the website that will receive the order', 'bbloomer-woocommerce-create-order-from-another-website' ),
- 'id' => 'website',
- 'placeholder' => __( 'Enter website url', 'bbloomer-woocommerce-create-order-from-another-website' ),
- 'type' => 'url',
- 'value' => $bbaccpc_options['website'],
- 'autoload' => false,
- );
- $options[] = $args;
- // Text field to specify the website that pushed the order (i.e. this website).
- $args = array(
- 'title' => __( 'Created by', 'bbloomer-woocommerce-create-order-from-another-website' ),
- 'desc' => __( 'Enter the domain name of this website. It will be added to the new order meta information.', 'bbloomer-woocommerce-create-order-from-another-website' ),
- 'id' => 'createdby',
- 'placeholder' => __( 'Enter the domain name of this site', 'bbloomer-woocommerce-create-order-from-another-website' ),
- 'type' => 'text',
- 'value' => $bbaccpc_options['createdby'],
- 'autoload' => false,
- );
- $options[] = $args;
- // ID of product (or variation) to include in the new order.
- $args = array(
- 'title' => __( 'Product ID', 'bbloomer-woocommerce-create-order-from-another-website' ),
- 'desc' => __( 'Enter the ID of the product to include in the new order.', 'bbloomer-woocommerce-create-order-from-another-website' ),
- 'id' => 'product_id',
- 'placeholder' => __( 'Enter the product ID', 'bbloomer-woocommerce-create-order-from-another-website' ),
- 'type' => 'number',
- 'value' => $bbaccpc_options['product_id'],
- 'autoload' => false,
- );
- $options[] = $args;
- // Dropdown to set the status of the created order.
- $order_statuses = array(
- 'completed' => _x( 'Completed', 'Order status', 'woocommerce' ),
- 'pending' => _x( 'Pending payment', 'Order status', 'woocommerce' ),
- 'processing' => _x( 'Processing', 'Order status', 'woocommerce' ),
- 'on-hold' => _x( 'On hold', 'Order status', 'woocommerce' ),
- 'cancelled' => _x( 'Cancelled', 'Order status', 'woocommerce' ),
- 'refunded' => _x( 'Refunded', 'Order status', 'woocommerce' ),
- 'failed' => _x( 'Failed', 'Order status', 'woocommerce' ),
- );
- $args = array(
- 'title' => __( 'Order status', 'woocommerce' ),
- 'desc' => __( 'Choose the status of the order that is create.', 'bbloomer-woocommerce-apply-coupon-programmatically-product-cart' ),
- 'type' => 'select',
- 'class' => 'wc-enhanced-select',
- 'id' => 'order_status',
- 'options' => $order_statuses,
- 'value' => $bbaccpc_options['order_status'],
- );
- $options[] = $args;
- // Render the fields.
- echo woocommerce_admin_fields( $options );
- echo '</tr>';
- echo '</tbody></table>';
- echo '<p class="submit"><a id="bbacppcsave" class="button-primary">Save Settings</a></p>';
- echo '</div>';
- wc_enqueue_js( "
- $('#bbacppcsave').click(function(e){
- e.preventDefault();
- $('.button-primary.woocommerce-save-button').trigger('click');
- });
- " );
- }
- }
- add_action( 'woocommerce_checkout_order_created', 'bbwcofaw_create_order_on_remote_site', 9999 );
- function bbwcofaw_create_order_on_remote_site( $order ) {
- $default_options = array( 'consumer_key' => '', 'consumer_secret' => '', 'website' => '', 'createdby' => '' );
- $stored_options = get_option( bbwcofaw_settings_option_name(), array() );
- $bbaccpc_options = wp_parse_args( $stored_options, $default_options );
- // Ensure consumer key and secret are set and start with the expected characters (ck_ and cs_).
- if ( empty( $bbaccpc_options['consumer_key'] ) || 0 !== strpos( $bbaccpc_options['consumer_key'], 'ck_' ) ) { return; }
- if ( empty( $bbaccpc_options['consumer_secret'] ) || 0 !== strpos( $bbaccpc_options['consumer_secret'], 'cs_' ) ) { return; }
- // Do a basic check of the other option values.
- if ( empty( $bbaccpc_options['website'] ) ) { return; }
- if ( 0 == absint( $bbaccpc_options['product_id'] ) ) { return; }
- if ( empty( $bbaccpc_options['order_status'] ) ) { $bbaccpc_options['order_status'] = 'completed'; }
- $website_url = $bbaccpc_options['website'] . '/wp-json/wc/v3/orders?consumer_key=' . $bbaccpc_options['consumer_key'] . '&consumer_secret=' . $bbaccpc_options['consumer_secret'];
- $body = array(
- 'status' => $bbaccpc_options['order_status'],
- 'total' => 0,
- 'billing' => array(
- 'first_name' => $order->get_billing_first_name(),
- 'email' => $order->get_billing_email(),
- ),
- 'line_items' => array( array(
- 'product_id' => $bbaccpc_options['product_id'],
- 'quantity' => 1,
- )),
- );
- if ( !empty( $bbaccpc_options['createdby'] ) ) {
- $body['meta_data'] = array( array(
- 'key' => 'createdby',
- 'value' => $bbaccpc_options['createdby'],
- ));
- }
- $raw_response = wp_remote_post( $website_url,
- array(
- 'headers' => array( 'Content-Type' => 'application/json' ),
- 'timeout' => 30,
- 'body' => json_encode( $body ),
- )
- );
- // ToDo: What if the order creation fails?
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement