Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.19 KB | None | 0 0
  1. <?php
  2. /**
  3.  * PLEASE READ AND FOLLOW ALL INSTRUCTIONS IN CAPS
  4.  *
  5.  * IN ORDER FOR THIS TO WORK YOU NEED TO ADD A CUSTOM QUESTION
  6.  * BY LOGGING INTO THE WORDPRESS ADMIN AND GOING TO :
  7.  *        Event Espresso > Registration Form
  8.  * AND THEN CLICKING ON "Add New Question"
  9.  *
  10.  * FOR THIS EXAMPLE CODE I CREATED TWO NEW QUESTIONS:
  11.  *
  12.  *      ONE NAMED "T-shirt Size"
  13.  *      SET ITS TYPE TO "Dropdown" AND GAVE IT THE FOLLOWING OPTIONS:
  14.  *            "small"
  15.  *            "medium"
  16.  *            "large"
  17.  *      (ANSWER VALUES ARE CASE SENSITIVE)
  18.  *
  19.  *      THEN CLICKED "Save and Close"
  20.  *
  21.  *      AND A SECOND QUESTION NAMED "T-shirt Quantity"
  22.  *      AND SET ITS TYPE TO "Number"
  23.  *
  24.  *      THEN CLICKED "Save and Close"
  25.  *
  26.  * ADDITIONAL QUESTIONS COULD BE ADDED IN A SIMILAR FASHION.
  27.  *
  28.  * I ALSO CREATED A QUESTION GROUP CALLED "Products"
  29.  * AND ADDED THE "T-shirt Size" AND "T-shirt Quantity" QUESTIONS TO THAT GROUP
  30.  *
  31.  * !!! IMPORTANT !!!
  32.  * YOUR QUANTITY QUESTION MUST APPEAR >> AFTER << THE OPTION QUESTION
  33.  * WHEN ORDERING THE QUESTIONS IN THE QUESTION GROUP
  34.  *
  35.  * THEN ON MY EVENT ( Event Espresso > Events > Edit Event ),
  36.  * I CHECKED OFF THE  "Products" QUESTION GROUP
  37.  * IN THE "Questions for Primary Registrant" SIDEBAR METABOX
  38.  * AS WELL AS THE "Questions for Additional Registrants" SIDEBAR METABOX
  39.  *
  40.  * THIS WAY, ALL REGISTRANTS WILL BE ASKED BOTH QUESTIONS,
  41.  * WHICH WILL THEN CONTROL THE EXTRA CHARGES ADDED TO THE TRANSACTION.
  42.  *
  43.  * PLZ FOLLOW ALL ADDITIONAL INSTRUCTIONS BELOW THAT ARE WRITTEN IN CAPS
  44.  */
  45. /**
  46.  * EDIT THIS ARRAY TO HOLD THE DETAILS FOR YOUR PRODUCT OPTIONS.
  47.  * THIS CAN HANDLE ANY NUMBER OF ITEMS AS LONG AS THE CORRESPONDING QUESTION IDs ARE CORRECT
  48.  * AND THE PRODUCT OPTION DETAIL KEYS MATCH THE QUESTION VALUES
  49.  */
  50. $products = array(
  51.     0 => array(
  52.         // CHANGE NUMBER VALUE TO MATCH THE ID OF YOUR PRODUCT QUESTION
  53.         'product_question_id'     => 11,
  54.         // CHANGE NUMBER VALUE TO MATCH THE ID OF YOUR QUANTITY QUESTION
  55.         'product_qty_question_id' => 13,
  56.         // AN ARRAY THAT HOLDS DETAILS FOR EACH PRODUCT OPTION YOU ADDED AS QUESTION OPTIONS
  57.         'product_option_details'  => array(
  58.             // KEYS FOR THIS ARRAY SHOULD MATCH THE VALUES YOU ENTERED FOR YOUR QUESTION OPTION VALUES
  59.             'Sunglasses'  => array(
  60.                 // THE REST OF THESE VALUES WILL BE USED FOR GENERATING LINE ITEMS
  61.                 'name'        => 'Sunglasses',
  62.                 'code'        => 'Sunglasses',
  63.                 // THE REGISTRANTS NAME WILL GET PREPENDED TO THE PRODUCT DESCRIPTION WHEN DISPLAYED IN LINE ITEMS
  64.                 // ie: Small T-shirt: for Derek Zoolander. What is this? A T-shirt for ants?"
  65.                 'description' => 'Sunglasses',
  66.                 'unit_price'  => 5.00,
  67.                 'taxable'     => false,
  68.             ),
  69.            
  70.             // ADD ADDITIONAL PRODUCT OPTIONS HERE
  71.         ),
  72.  
  73.     ),
  74.     // ADD ADDITIONAL PRODUCTS HERE
  75.     1 => array(
  76.         // CHANGE NUMBER VALUE TO MATCH THE ID OF YOUR PRODUCT QUESTION
  77.         'product_question_id'     => 12,
  78.         // CHANGE NUMBER VALUE TO MATCH THE ID OF YOUR QUANTITY QUESTION
  79.         'product_qty_question_id' => 14,
  80.         // AN ARRAY THAT HOLDS DETAILS FOR EACH PRODUCT OPTION YOU ADDED AS QUESTION OPTIONS
  81.         'product_option_details'  => array(
  82.             // KEYS FOR THIS ARRAY SHOULD MATCH THE VALUES YOU ENTERED FOR YOUR QUESTION OPTION VALUES
  83.             'Determination'  => array(
  84.                 // THE REST OF THESE VALUES WILL BE USED FOR GENERATING LINE ITEMS
  85.                 'name'        => 'Determination H-Band',
  86.                 'code'        => 'determination-h-Band',
  87.                 'description' => 'Determination H-Band',
  88.                 'unit_price'  => 10.00,
  89.                 'taxable'     => false,
  90.             ),
  91.             'Perserverance' => array(
  92.                 'name'        => 'Perserverance H-Band',
  93.                 'code'        => 'perserverance-h-Band',
  94.                 'description' => 'Perserverance H-Band',
  95.                 'unit_price'  => 10.00,
  96.                 'taxable'     => false,
  97.             ),
  98.             'Resilience'  => array(
  99.                 'name'        => 'Resilience H-Band',
  100.                 'code'        => 'resilience-h-Band',
  101.                 'description' => 'Resilience H-Band',
  102.                 'unit_price'  => 10.00,
  103.                 'taxable'     => false,
  104.             ),
  105.            
  106.             // ADD ADDITIONAL PRODUCT OPTIONS HERE
  107.         ),
  108.     ),
  109.  
  110. );
  111. // THE FOLLOWING CAN BE COMMENTED OUT AND THEN ADDED ANYWHERE IN YOUR SYSTEM CODE (ASSUMING THIS FILE IS LOADED)
  112. // AS LONG AS IT IS CALLED >> BEFORE << THE WORDPRESS "pre_get_posts" HOOK AT PRIORITY 10
  113. // THIS MEANS THAT THE ARRAY OF PRODUCTS CAN BE GENERATED DYNAMICALLY USING PRODUCT DATA FROM ANOTHER CART.
  114. // TO UPDATE THE SYSTEM AFTER A PRODUCT IS SOLD, YOU CAN HOOK INTO THE FOLLOWING FILTER:
  115. //      "AHEE__bc_ee_add_product_surcharge__add_product__product_added"
  116. // WHICH IS FOUND IN THE `bc_ee_add_product_surcharge::add_product()` METHOD
  117. new bc_ee_add_product_surcharge($products);
  118. /*
  119. * !!! STOP EDITING !!!
  120. * DON'T GO ANY FURTHER UNLESS YOU ARE REALLY CONFIDENT THAT YOU KNOW WHAT YOU ARE DOING.
  121. */
  122. /**
  123.  * bc_ee_add_product_transaction_surcharge
  124.  *
  125.  * @package               Event Espresso
  126.  * @subpackage            EE Code Snippets Library
  127.  * @author                Brent Christensen
  128.  */
  129. class bc_ee_add_product_surcharge
  130. {
  131.     /**
  132.      * @var array $products
  133.      */
  134.     private $products;
  135.     /**
  136.      * @var EE_Checkout $checkout
  137.      */
  138.     private $checkout;
  139.     /**
  140.      * @var EE_Line_Item $grand_total
  141.      */
  142.     private $grand_total;
  143.     /**
  144.      * @var EE_Line_Item $pre_tax_subtotal
  145.      */
  146.     private $pre_tax_subtotal;
  147.     /**
  148.      * DO NOT EDIT!
  149.      *
  150.      * @param array $products
  151.      */
  152.     public function __construct($products = array())
  153.     {
  154.         $this->products = $products;
  155.         add_filter(
  156.             'FHEE__Single_Page_Checkout___check_form_submission__request_params',
  157.             array($this, 'check_for_products')
  158.         );
  159.         add_action(
  160.             'AHEE__Single_Page_Checkout__after_attendee_information__process_reg_step',
  161.             array($this, 'add_products')
  162.         );
  163.     }
  164.     /**
  165.      * @return array
  166.      */
  167.     public function products()
  168.     {
  169.         return (array)apply_filters(
  170.             'FHEE__bc_ee_add_product_surcharge__products',
  171.             $this->products
  172.         );
  173.     }
  174.     /**
  175.      * DO NOT EDIT!
  176.      *
  177.      * @param array $request_params
  178.      * @return array
  179.      */
  180.     public function check_for_products(array $request_params)
  181.     {
  182.         if (isset($request_params['ee_reg_qstn'])) {
  183.             foreach ($request_params['ee_reg_qstn'] as $registrations) {
  184.                 if (! empty($registrations)) {
  185.                     foreach ($registrations as $QST_ID => $response) {
  186.                         foreach ($this->products() as $product) {
  187.                             if ($product['product_question_id'] === $QST_ID) {
  188.                                 // we found a product, so toggle the following filter switch to trigger processing
  189.                                 add_filter(
  190.                                     'FHEE__bc_ee_add_product_surcharge__add_products', '__return_true'
  191.                                 );
  192.                                 return $request_params;
  193.                             }
  194.                         }
  195.                     }
  196.                 }
  197.             }
  198.         }
  199.         return $request_params;
  200.     }
  201.     /**
  202.      * DO NOT EDIT!
  203.      *
  204.      * @param EE_SPCO_Reg_Step $reg_step
  205.      * @return void
  206.      * @throws EE_Error
  207.      */
  208.     public function add_products(EE_SPCO_Reg_Step $reg_step)
  209.     {
  210.         // apply the surcharge ?
  211.         if (
  212.             ! apply_filters('FHEE__bc_ee_add_product_surcharge__add_products', false)
  213.             || ! $this->verify_objects($reg_step)
  214.         ) {
  215.             return;
  216.         }
  217.         $registrations = $this->checkout->transaction->registrations();
  218.         $product_added = false;
  219.         foreach ($registrations as $REG_ID => $registration) {
  220.             if (! $registration instanceof EE_Registration) {
  221.                 continue;
  222.             }
  223.             //Intialize product variables
  224.             $product = null;
  225.             $product_qty = null;
  226.             $product_qty_question_id = null;
  227.             $answers = $registration->answers();
  228.             foreach ($answers as $answer) {  
  229.                 if ($answer instanceof EE_Answer) {
  230.                     if ($product === null) {
  231.                         $product = $this->get_product($answer->question_ID(), $answer->value());
  232.                         $product_qty_question_id = isset($product['product_qty_question_id'])
  233.                             ? $product['product_qty_question_id']
  234.                             : null;
  235.                     }
  236.                     if ($answer->question_ID() === $product_qty_question_id) {
  237.                         $product_qty = $answer->value();
  238.                     }
  239.                 }
  240.            
  241.                 if ($product !== null && $product_qty !== null) {
  242.                     $product_added = $this->add_product($product, $product_qty, $registration)
  243.                         ? true // toggle to true
  244.                         : $product_added; // or maintain existing value
  245.                    
  246.                     //Reset product variables
  247.                     $product = null;
  248.                     $product_qty = null;
  249.                     $product_qty_question_id = null;
  250.                 }
  251.             }
  252.         }
  253.         if ($product_added) {
  254.             $this->grand_total->recalculate_total_including_taxes();
  255.         }
  256.     }
  257.     /**
  258.      * DO NOT EDIT!
  259.      *
  260.      * @param EE_SPCO_Reg_Step $reg_step
  261.      * @return bool
  262.      * @throws EE_Error
  263.      */
  264.     private function verify_objects(EE_SPCO_Reg_Step $reg_step)
  265.     {
  266.         $this->checkout = $reg_step->checkout;
  267.         // verify checkout && transaction
  268.         if (
  269.             ! (
  270.                 $this->checkout instanceof EE_Checkout
  271.                 && $this->checkout->transaction instanceof EE_Transaction
  272.             )
  273.         ) {
  274.             return false;
  275.         }
  276.         // verify cart
  277.         $cart = $this->checkout->cart;
  278.         if (! $cart instanceof EE_Cart) {
  279.             return false;
  280.         }
  281.         // verify grand total line item
  282.         $this->grand_total = $cart->get_grand_total();
  283.         if (! $this->grand_total instanceof EE_Line_Item) {
  284.             return false;
  285.         }
  286.         $this->pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($this->grand_total);
  287.         return true;
  288.     }
  289.     /**
  290.      * DO NOT EDIT!
  291.      *
  292.      * @param int    $product_id
  293.      * @param string $option
  294.      * @return null
  295.      */
  296.     private function get_product($product_id, $option)
  297.     {
  298.         write_log($product_id . ' ' . $option);
  299.         foreach ($this->products() as $product) {
  300.             if ($product['product_question_id'] === $product_id && isset($product['product_option_details'][$option])) {
  301.                 $product_details = $product['product_option_details'][$option];
  302.                 $product_details['product_question_id'] = $product['product_question_id'];
  303.                 $product_details['product_qty_question_id'] = $product['product_qty_question_id'];
  304.                 return $product_details;
  305.             }
  306.         }
  307.         return null;
  308.     }
  309.     /**
  310.      * DO NOT EDIT!
  311.      *
  312.      * @param array           $product
  313.      * @param int             $product_qty
  314.      * @param EE_Registration $registration
  315.      * @return bool
  316.      * @throws EE_Error
  317.      */
  318.     private function add_product(array $product, $product_qty = 0, EE_Registration $registration)
  319.     {
  320.         $product['code'] .= '-' . $registration->reg_code();
  321.         $product['description'] = sprintf(
  322.             esc_html_x(
  323.                 'for %1$s.',
  324.                 '{Product Name} for {Customer Name}',
  325.                 'event_espresso'
  326.             ),
  327.             $registration->attendee()->full_name()
  328.         ) . ' ' . $product['description'];
  329.         $product_added = $this->add_line_item(
  330.             $product['code'],
  331.             $product['name'],
  332.             $product['description'],
  333.             $product['unit_price'],
  334.             $product['taxable'],
  335.             $product_qty
  336.         );
  337.         if($product_added) {
  338.             do_action(
  339.                 'AHEE__bc_ee_add_product_surcharge__add_product__product_added',
  340.                 $product,
  341.                 $product_qty,
  342.                 $registration
  343.             );
  344.         }
  345.         return $product_added;
  346.     }
  347.     /**
  348.      * DO NOT EDIT!
  349.      *
  350.      * @param string $product_code
  351.      * @param string $product_name
  352.      * @param string $product_description
  353.      * @param float  $product_unit_price
  354.      * @param bool   $product_taxable
  355.      * @param        $product_qty
  356.      * @return bool
  357.      * @throws EE_Error
  358.      */
  359.     private function add_line_item(
  360.         $product_code = '',
  361.         $product_name = '',
  362.         $product_description = '',
  363.         $product_unit_price = 0.00,
  364.         $product_taxable = true,
  365.         $product_qty
  366.     ) {
  367.         // has surcharge already been applied ?
  368.         $existing_surcharge = $this->grand_total->get_child_line_item($product_code);
  369.         if ($existing_surcharge instanceof EE_Line_Item) {
  370.             return false;
  371.         }
  372.         return $this->pre_tax_subtotal->add_child_line_item(
  373.             EE_Line_Item::new_instance(
  374.                 array(
  375.                     'LIN_name'       => $product_name,
  376.                     'LIN_desc'       => $product_description,
  377.                     'LIN_unit_price' => (float)$product_unit_price,
  378.                     'LIN_quantity'   => $product_qty,
  379.                     'LIN_is_taxable' => $product_taxable,
  380.                     'LIN_order'      => 0,
  381.                     'LIN_total'      => (float)$product_unit_price,
  382.                     'LIN_type'       => EEM_Line_Item::type_line_item,
  383.                     'LIN_code'       => $product_code,
  384.                 )
  385.             )
  386.         );
  387.     }
  388. }
  389. // End of file  bc_ee_add_product_surcharge.php
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement