Advertisement
daymobrew

Business Bloomer WooCommerce: Create an order on another website

Apr 26th, 2024
774
0
19 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.41 KB | Source Code | 0 0
  1. <?php
  2.  
  3. /*
  4.  * Plugin Name: Business Bloomer WooCommerce: Create an order on another website
  5.  * Description: Create a order on another website when an order is created here.
  6.  * Plugin URI: https://www.businessbloomer.com/shop/plugins/woocommerce-create-order-from-another-website/
  7.  * Update URI: https://www.businessbloomer.com/shop/plugins/woocommerce-create-order-from-another-website/
  8.  * Author: Business Bloomer
  9.  * Author URI: https://www.businessbloomer.com
  10.  * Text Domain: bbloomer-woocommerce-create-order-from-another-website
  11.  * Requires Plugins: woocommerce
  12.  * Version: 0.1.20231024
  13.  */
  14.  
  15. defined( 'ABSPATH' ) || exit;
  16.  
  17. include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  18. if ( ! is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
  19.     return;
  20. }
  21.  
  22. define( 'BBWCOFAW', 'https://www.businessbloomer.com/wp-json/bb/v1/downloads?product_id=XXXXXX' );
  23.  
  24. add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'bbwcofaw_action_links', 9999, 4 );
  25.  
  26. function bbwcofaw_action_links( $links, $plugin_file, $plugin_data, $context ) {
  27.     if ( ! isset( $plugin_data ) ) return $links;
  28.     $plugin_links = array(
  29.         '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=advanced#bbwcofaw-start' ) . '">' . __( 'Settings', 'woocommerce' ) . '</a>',
  30.         '<a href="' . $plugin_data['PluginURI'] . '#tab-docs" target="_blank">' . __( 'Docs', 'woocommerce' ) . '</a>',
  31.         '<a href="https://businessbloomer.clarityflow.com/i/intake?title=' . rawurlencode( $plugin_data['Name'] ) . rawurlencode( ' ' ) . $plugin_data['Version'] . '" target="_blank">' . __( 'Premium Support', 'woocommerce' ) . '</a>',
  32.         '<a href="' . $plugin_data['PluginURI'] . '#tab-reviews" target="_blank">' . __( 'Add a review', 'woocommerce' ) . '</a>',
  33.     );
  34.     return array_merge( $plugin_links, $links );
  35. }
  36.  
  37. add_filter( 'plugin_row_meta', 'bbwcofaw_hide_view_details', 9999, 4 );
  38.  
  39. function bbwcofaw_hide_view_details( $plugin_meta, $plugin_file, $plugin_data, $status ) {
  40.     if ( isset( $plugin_data['TextDomain'] ) && $plugin_data['TextDomain'] == plugin_basename( __DIR__ ) ) unset( $plugin_meta[2] );
  41.     return $plugin_meta;
  42. }
  43.  
  44. add_filter( 'plugins_api', 'bbwcofaw_plugin_view_version_details', 9999, 3 );
  45.  
  46. function bbwcofaw_plugin_view_version_details( $res, $action, $args ) {
  47.     if ( 'plugin_information' !== $action ) return $res;
  48.     if ( $args->slug !== plugin_basename( __DIR__ ) ) return $res;
  49.     $response = wp_remote_get( BBWCOFAW, array(  'headers' => array( 'Accept' => 'application/json' ) ) );
  50.     if ( ( ! is_wp_error( $response ) ) && ( 200 === wp_remote_retrieve_response_code( $response ) ) ) {
  51.         $download = json_decode( wp_remote_retrieve_body( $response ), true );
  52.     } else return $res;
  53.     $res = new stdClass();
  54.     $res->name = $download['name'];
  55.     $res->slug = plugin_basename( __DIR__ );
  56.     $res->path = plugin_basename( __DIR__ ) . '/' . plugin_basename( __DIR__ ) . '.php';
  57.     $res->version = $download['version'];
  58.     $res->download_link = $download['download_link'];
  59.     $res->sections = array(
  60.         'description' => $download['description'],
  61.     );
  62.     return $res;
  63. }
  64.  
  65. add_filter( 'update_plugins_www.businessbloomer.com', function( $update, array $plugin_data, string $plugin_file, $locales ) {
  66.     if ( $plugin_file !== plugin_basename( __DIR__ ) . '/' . plugin_basename( __DIR__ ) . '.php' ) return $update;    if ( ! empty( $update ) ) return $update;
  67.     $response = wp_remote_get( BBWCOFAW, array(  'headers' => array( 'Accept' => 'application/json' ) ) );
  68.     if ( ( ! is_wp_error( $response ) ) && ( 200 === wp_remote_retrieve_response_code( $response ) ) ) {
  69.         $download = json_decode( wp_remote_retrieve_body( $response ), true );
  70.     } else return $update;
  71.     if ( ! version_compare( $plugin_data['Version'], $download['version'], '<' ) ) return $update;
  72.     return [
  73.         'slug' => plugin_basename( __DIR__ ),
  74.         'version' => $download['version'],
  75.         'url' => $plugin_data['PluginURI'],
  76.         'package' => $download['download_link'],
  77.     ];
  78. }, 9999, 4 );
  79.  
  80. // Return the wp_options option names - used in a number of functions.
  81. function bbwcofaw_settings_option_name() {
  82.     return 'bb_cofaw';
  83. }
  84.  
  85. // Verify and save the information submitted in the above form.
  86. add_action( 'admin_init', 'bbwcofaw_save_api_details' );
  87.  
  88. function bbwcofaw_save_api_details() {
  89.     if ( isset( $_GET['page'], $_GET['tab'], $_POST['consumer_key'], $_POST['consumer_secret'], $_POST['order_status'], $_POST['website'], $_POST['createdby'], $_POST['_wpnonce'] )
  90.         && 'wc-settings' == $_GET['page'] && 'advanced' == $_GET['tab'] && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-settings' ) ) {
  91.         if ( current_user_can( 'manage_woocommerce' ) ) {
  92.             $options = array( 'consumer_key' => '', 'consumer_secret' => '', 'website' => '', 'createdby' => '', 'order_status' => 'completed', 'product_id' => 0 );
  93.  
  94.             $options['consumer_key'] = sanitize_text_field( $_POST['consumer_key'] );
  95.             $options['consumer_secret'] = sanitize_text_field( $_POST['consumer_secret'] );
  96.             $options['website'] = sanitize_text_field( $_POST['website'] );
  97.             $options['createdby'] = sanitize_text_field( $_POST['createdby'] );
  98.             $options['order_status'] = sanitize_text_field( $_POST['order_status'] );
  99.             $options['product_id'] = sanitize_text_field( absint( $_POST['product_id'] ) );
  100.  
  101.             update_option( bbwcofaw_settings_option_name(), $options );
  102.         }
  103.     }
  104. }
  105.  
  106. // Settings section displayed in WooCommerce/Settings/Advanced.
  107. add_action( 'woocommerce_settings_advanced', 'bbwcofaw_api_settings_fields', 20 );
  108.  
  109. function bbwcofaw_api_settings_fields( $email_obj ) {
  110.     $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . plugin_basename( __FILE__ ) );
  111.     $quick_links = bbwcofaw_action_links( array(), '', $plugin_data, '' );
  112.     array_shift( $quick_links );
  113.  
  114.     if ( !isset( $_GET['section'] ) || empty( $_GET['section'] ) ) {
  115.         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>';
  116.         echo '<p>' . $plugin_data['Description'] . '</p>';
  117.         echo '<h4>Quick links: ' . implode( ' - ', $quick_links ) . '</h4>';
  118.         echo '<table class="form-table"><tbody>';
  119.  
  120.         $default_options = array( 'consumer_key' => '', 'consumer_secret' => '', 'website' => '', 'createdby' => '', 'order_status' => 'completed', 'product_id' => 0 );
  121.         $stored_options = get_option( bbwcofaw_settings_option_name(), array() );
  122.         $bbaccpc_options = wp_parse_args( $stored_options, $default_options );
  123.  
  124.         // Display help for creating Consumer key and secret.
  125.         echo '<details><summary><strong>', __( 'How to create a Consumer key and secret', 'bbloomer-woocommerce-create-order-from-another-website' ). '</strong></summary>';
  126.         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>';
  127.         echo '</details>';
  128.  
  129.         $options = array();
  130.         // Password field to specify consumer key from the website that will receive the order.
  131.         $args = array(
  132.                     'title'       => __( 'Consumer key', 'woocommerce' ),
  133.                     'desc'        => __( 'Enter the Consumer key created on the website that will receive the order', 'bbloomer-woocommerce-create-order-from-another-website' ),
  134.                     'id'          => 'consumer_key',
  135.                     'placeholder' => __( 'Enter Consumer key', 'bbloomer-woocommerce-create-order-from-another-website' ),
  136.                     'type'        => 'password',
  137.                     'value'       => $bbaccpc_options['consumer_key'],
  138.                     'autoload'    => false,
  139.                 );
  140.         $options[] = $args;
  141.  
  142.         // Password field to specify consumer secret from the website that will receive the order.
  143.         $args = array(
  144.                     'title'       => __( 'Consumer secret', 'woocommerce' ),
  145.                     'desc'        => __( 'Enter the Consumer secret created on the website that will receive the order', 'bbloomer-woocommerce-create-order-from-another-website' ),
  146.                     'id'          => 'consumer_secret',
  147.                     'placeholder' => __( 'Enter Consumer secret', 'bbloomer-woocommerce-create-order-from-another-website' ),
  148.                     'type'        => 'password',
  149.                     'value'       => $bbaccpc_options['consumer_secret'],
  150.                     'autoload'    => false,
  151.                 );
  152.         $options[] = $args;
  153.  
  154.         // URL field to specify the website that will receive the order.
  155.         $args = array(
  156.                     'title'       => __( 'Site Address (URL)' ),
  157.                     'desc'        => __( 'Enter the url of the website that will receive the order', 'bbloomer-woocommerce-create-order-from-another-website' ),
  158.                     'id'          => 'website',
  159.                     'placeholder' => __( 'Enter website url', 'bbloomer-woocommerce-create-order-from-another-website' ),
  160.                     'type'        => 'url',
  161.                     'value'       => $bbaccpc_options['website'],
  162.                     'autoload'    => false,
  163.                 );
  164.         $options[] = $args;
  165.  
  166.         // Text field to specify the website that pushed the order (i.e. this website).
  167.         $args = array(
  168.                     'title'       => __( 'Created by', 'bbloomer-woocommerce-create-order-from-another-website' ),
  169.                     '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' ),
  170.                     'id'          => 'createdby',
  171.                     'placeholder' => __( 'Enter the domain name of this site', 'bbloomer-woocommerce-create-order-from-another-website' ),
  172.                     'type'        => 'text',
  173.                     'value'       => $bbaccpc_options['createdby'],
  174.                     'autoload'    => false,
  175.                 );
  176.         $options[] = $args;
  177.  
  178.         // ID of product (or variation) to include in the new order.
  179.         $args = array(
  180.                     'title'       => __( 'Product ID', 'bbloomer-woocommerce-create-order-from-another-website' ),
  181.                     'desc'        => __( 'Enter the ID of the product to include in the new order.', 'bbloomer-woocommerce-create-order-from-another-website' ),
  182.                     'id'          => 'product_id',
  183.                     'placeholder' => __( 'Enter the product ID', 'bbloomer-woocommerce-create-order-from-another-website' ),
  184.                     'type'        => 'number',
  185.                     'value'       => $bbaccpc_options['product_id'],
  186.                     'autoload'    => false,
  187.                 );
  188.         $options[] = $args;
  189.  
  190.         // Dropdown to set the status of the created order.
  191.         $order_statuses = array(
  192.                 'completed'  => _x( 'Completed', 'Order status', 'woocommerce' ),
  193.                 'pending'    => _x( 'Pending payment', 'Order status', 'woocommerce' ),
  194.                 'processing' => _x( 'Processing', 'Order status', 'woocommerce' ),
  195.                 'on-hold'    => _x( 'On hold', 'Order status', 'woocommerce' ),
  196.                 'cancelled'  => _x( 'Cancelled', 'Order status', 'woocommerce' ),
  197.                 'refunded'   => _x( 'Refunded', 'Order status', 'woocommerce' ),
  198.                 'failed'     => _x( 'Failed', 'Order status', 'woocommerce' ),
  199.         );
  200.         $args = array(
  201.                 'title'   => __( 'Order status', 'woocommerce' ),
  202.                 'desc'    => __( 'Choose the status of the order that is create.', 'bbloomer-woocommerce-apply-coupon-programmatically-product-cart' ),
  203.                 'type'    => 'select',
  204.                 'class'   => 'wc-enhanced-select',
  205.                 'id'      => 'order_status',
  206.                 'options' => $order_statuses,
  207.                 'value' => $bbaccpc_options['order_status'],
  208.         );
  209.         $options[] = $args;
  210.  
  211.         // Render the fields.
  212.         echo woocommerce_admin_fields( $options );
  213.         echo '</tr>';
  214.  
  215.         echo '</tbody></table>';
  216.         echo '<p class="submit"><a id="bbacppcsave" class="button-primary">Save Settings</a></p>';
  217.         echo '</div>';
  218.  
  219.         wc_enqueue_js( "
  220.             $('#bbacppcsave').click(function(e){
  221.                 e.preventDefault();
  222.                 $('.button-primary.woocommerce-save-button').trigger('click');
  223.             });
  224.         " );
  225.     }
  226. }
  227.  
  228.  
  229. add_action( 'woocommerce_checkout_order_created', 'bbwcofaw_create_order_on_remote_site', 9999 );
  230.    
  231. function bbwcofaw_create_order_on_remote_site( $order ) {
  232.     $default_options = array( 'consumer_key' => '', 'consumer_secret' => '', 'website' => '', 'createdby' => '' );
  233.     $stored_options = get_option( bbwcofaw_settings_option_name(), array() );
  234.     $bbaccpc_options = wp_parse_args( $stored_options, $default_options ); 
  235.  
  236.     // Ensure consumer key and secret are set and start with the expected characters (ck_ and cs_).
  237.     if ( empty( $bbaccpc_options['consumer_key'] ) || 0 !== strpos( $bbaccpc_options['consumer_key'], 'ck_' ) ) { return; }
  238.     if ( empty( $bbaccpc_options['consumer_secret'] ) || 0 !== strpos( $bbaccpc_options['consumer_secret'], 'cs_' ) ) { return; }
  239.     // Do a basic check of the other option values.
  240.     if ( empty( $bbaccpc_options['website'] ) ) { return; }
  241.     if ( 0 == absint( $bbaccpc_options['product_id'] ) ) { return; }
  242.     if ( empty( $bbaccpc_options['order_status'] ) ) { $bbaccpc_options['order_status'] = 'completed'; }
  243.  
  244.     $website_url = $bbaccpc_options['website'] . '/wp-json/wc/v3/orders?consumer_key=' . $bbaccpc_options['consumer_key'] . '&consumer_secret=' . $bbaccpc_options['consumer_secret'];
  245.  
  246.     $body = array(
  247.         'status' => $bbaccpc_options['order_status'],
  248.         'total' => 0,
  249.         'billing' => array(
  250.             'first_name' => $order->get_billing_first_name(),
  251.             'email' => $order->get_billing_email(),
  252.         ),
  253.         'line_items' => array( array(
  254.             'product_id' => $bbaccpc_options['product_id'],
  255.             'quantity' => 1,
  256.         )),
  257.     );
  258.     if ( !empty( $bbaccpc_options['createdby'] ) ) {
  259.         $body['meta_data'] = array( array(
  260.             'key' => 'createdby',
  261.             'value' => $bbaccpc_options['createdby'],
  262.         ));
  263.     }
  264.  
  265.     $raw_response = wp_remote_post( $website_url,
  266.         array(
  267.             'headers' => array( 'Content-Type' => 'application/json' ),
  268.             'timeout' => 30,                    
  269.             'body' => json_encode( $body ),
  270.         )
  271.     );
  272.  
  273.     // ToDo: What if the order creation fails?
  274. }
  275.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement