Advertisement
verygoodplugins

Untitled

Apr 29th, 2020
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.63 KB | None | 0 0
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3.     exit; // Exit if accessed directly
  4. }
  5.  
  6. class WPF_EC_RCP extends WPF_EC_Integrations_Base {
  7.  
  8.     /**
  9.      * Get things started
  10.      *
  11.      * @access public
  12.      * @return void
  13.      */
  14.  
  15.     public function init() {
  16.  
  17.         $this->slug = 'restrict-content-pro';
  18.  
  19.         add_action( 'rcp_create_payment', array( $this, 'send_order_data' ), 10, 2 );
  20.  
  21.         // meta box
  22.         add_action( 'rcp_edit_subscription_form', array( $this, 'rcp_product_select' ) );
  23.         add_action( 'rcp_add_subscription_form', array( $this, 'rcp_product_select' ) );
  24.         add_action( 'rcp_edit_subscription_level', array( $this, 'save_subscription_settings' ), 10, 2 );
  25.         add_action( 'rcp_add_subscription', array( $this, 'save_subscription_settings_new' ), 10, 2 );
  26.  
  27.     }
  28.  
  29.     /**
  30.      * Sends order data to CRM's ecommerce system
  31.      *
  32.      * @access  public
  33.      * @return  bool
  34.      */
  35.  
  36.     public function send_order_data( $payment_id, $payment ) {
  37.  
  38.         $wpf_complete = get_post_meta( $payment_id, 'wpf_ec_complete', true );
  39.  
  40.         if ( ! empty( $wpf_complete ) ) {
  41.             return true;
  42.         }
  43.  
  44.         $products   = array();
  45.         $discount   = $payment['discount_amount'];
  46.         $contact_id = wp_fusion()->user->get_contact_id( $payment['user_id'] );
  47.  
  48.         if ( empty( $payment['gateway'] ) ) {
  49.             $payment['gateway'] = 'Manual';
  50.         }
  51.  
  52.         $settings     = get_option( 'wpf_rcp_tags', array() );
  53.         $subscription = rcp_get_subscription_details( $payment['object_id'] );
  54.  
  55.         if ( isset( $settings[ $subscription->id ][ wp_fusion()->crm->slug . '_product_id' ] ) ) {
  56.             $crm_product_id = $settings[ $subscription->id ][ wp_fusion()->crm->slug . '_product_id' ];
  57.         } else {
  58.             $crm_product_id = false;
  59.         }
  60.  
  61.         $products[] = array(
  62.             'id'             => $subscription->id,
  63.             'crm_product_id' => $crm_product_id,
  64.             'name'           => $subscription->name,
  65.             'price'          => $subscription->price,
  66.             'discount'       => $payment['discount_amount'],
  67.             'qty'            => 1,
  68.             'sku'            => '',
  69.         );
  70.  
  71.         $line_items = array();
  72.  
  73.         if ( ! empty( $discount ) ) {
  74.             $line_items[] = array(
  75.                 'type'        => 'discount',
  76.                 'price'       => -$discount,
  77.                 'title'       => 'Discounts Applied',
  78.                 'description' => 'Used discount codes: ' . $payment['discount_amount'],
  79.             );
  80.         }
  81.  
  82.         $user = get_userdata( $payment['user_id'] );
  83.  
  84.         $order_args = array(
  85.             'order_label'     => 'RCP Order #' . $payment_id,
  86.             'order_number'    => $payment_id,
  87.             'order_edit_link' => admin_url( 'post.php?post=' . $payment_id . '&action=edit' ),
  88.             'payment_method'  => $payment['gateway'],
  89.             'user_email'      => $user->user_email,
  90.             'products'        => $products,
  91.             'line_items'      => $line_items,
  92.             'total'           => floatval( $subscription->price ),
  93.             'currency'        => rcp_get_currency(),
  94.             'order_date'      => strtotime( $payment['date'] ),
  95.             'provider'        => 'restrict-content-pro',
  96.             'user_id'         => $payment['user_id'],
  97.         );
  98.  
  99.         // Add order
  100.         $result = wp_fusion_ecommerce()->crm->add_order( $payment_id, $contact_id, $order_args );
  101.  
  102.         if ( is_wp_error( $result ) ) {
  103.  
  104.             wpf_log( 'error', $contact_id, 'Error adding RCP Order <a href="' . admin_url( 'post.php?post=' . $payment_id . '&action=edit' ) . '" target="_blank">#' . $payment_id . '</a>: ' . $result->get_error_message(), array( 'source' => wp_fusion()->crm->slug ) );
  105.             return false;
  106.  
  107.         }
  108.  
  109.         // Denotes that the WPF actions have already run for this order
  110.         // update_post_meta( $payment_id, 'wpf_ec_complete', true );
  111.         // update_post_meta( $payment_id, 'wpf_ec_' . wp_fusion()->crm->slug . '_invoice_id', $result );
  112.     }
  113.  
  114.  
  115.     /**
  116.      * Adds CRM product association field to edit view for single Subscription
  117.      *
  118.      * @access  public
  119.      * @return  mixed
  120.      */
  121.  
  122.     public function rcp_product_select( $level = false ) {
  123.  
  124.         if ( ! in_array( 'products', wp_fusion_ecommerce()->crm->supports ) ) {
  125.             return;
  126.         }
  127.  
  128.         $settings = get_option( 'wpf_rcp_tags', array() );
  129.  
  130.         if ( is_object( $level ) && isset( $settings[ $level->id ] ) ) {
  131.  
  132.             if ( empty( $settings[ $level->id ]['ontraport_product_id'] ) ) {
  133.                 $settings[ $level->id ]['ontraport_product_id'] = false;
  134.             }
  135.         }
  136.  
  137.         $available_products = get_option( 'wpf_' . wp_fusion()->crm->slug . '_products', array() );
  138.  
  139.         echo '<tr>';
  140.         echo '<th scope="row" style="width: 162px;"><label for="wpf-ec-product">' . wp_fusion()->crm->name . ' product</label>';
  141.         echo '<td>';
  142.  
  143.         echo '<select id="wpf-ec-product" class="select4-search" data-placeholder="None" name="wpf-settings[ontraport_product_id]"' . wp_fusion()->crm->slug . '_product_id">';
  144.  
  145.         echo '<option></option>';
  146.  
  147.         foreach ( $available_products as $id => $name ) {
  148.             echo '<option value="' . $id . '"' . selected( $id, $settings[ $level->id ]['ontraport_product_id'], false ) . '>' . $name . '</option>';
  149.         }
  150.  
  151.         echo '</select>';
  152.  
  153.         echo '</td>';
  154.  
  155.         echo '</tr>';
  156.     }
  157.  
  158.     /**
  159.      * Saves changes to WPF fields on the subscription edit screen
  160.      *
  161.      * @access  public
  162.      * @return  void
  163.      */
  164.  
  165.     public function save_subscription_settings( $id, $args ) {
  166.  
  167.         if ( ! isset( $args['wpf-settings'] ) ) {
  168.             return;
  169.         }
  170.  
  171.         $settings        = get_option( 'wpf_rcp_tags', array() );
  172.         $settings[ $id ] = $args['wpf-settings'];
  173.  
  174.         update_option( 'wpf_rcp_tags', $settings );
  175.  
  176.     }
  177.  
  178.  
  179.     /**
  180.      * Saves WPF settings for new subscription levels
  181.      *
  182.      * @access  public
  183.      * @return  void
  184.      */
  185.  
  186.     public function save_subscription_settings_new( $id, $args ) {
  187.  
  188.         if ( ! isset( $_POST['wpf-settings'] ) ) {
  189.             return;
  190.         }
  191.  
  192.         $settings        = get_option( 'wpf_rcp_tags', array() );
  193.         $settings[ $id ] = $_POST['wpf-settings'];
  194.         update_option( 'wpf_rcp_tags', $settings );
  195.  
  196.     }
  197.  
  198.  
  199. }
  200.  
  201. new WPF_EC_RCP();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement