pvukovic81

Untitled

Sep 17th, 2025
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.79 KB | None | 0 0
  1. public function event_order_updated( $order_id ) {
  2.         $db = JFactory::getDBO();
  3.         $order = $db->setQuery( 'SELECT * FROM #__hikashop_order WHERE order_id=' . (int) $order_id )->loadObject();
  4.         if ( empty( $order->order_id ) ) {
  5.             return false;
  6.         }
  7.  
  8. $statuses = [ 'confirmed' ];
  9.         if ( ! in_array( $order->order_status, $statuses ) ) {
  10.             return;
  11.         }
  12.  
  13.         $products = $db->setQuery( '
  14.             SELECT i.*
  15.               FROM #__hikashop_order o
  16.               JOIN #__hikashop_order_product i ON i.order_id=o.order_id
  17.              WHERE o.order_id=' . (int) $order->order_id . '
  18.              GROUP BY i.order_product_id
  19.         ' )->loadObjectList();
  20.         if ( empty( $products ) ) {
  21.             return;
  22.         }
  23.  
  24.         $joomla_user_id = (int) $db->setQuery( 'SELECT user_cms_id FROM #__hikashop_user WHERE user_id=' . (int) $order->order_user_id )->loadResult();
  25.         if ( $joomla_user_id < 1 ) {
  26.             return false;
  27.         }
  28.         $joomla_user = JFactory::getUser( $joomla_user_id );
  29.         if ( $joomla_user->id != $joomla_user_id ) {
  30.             return false;
  31.         }
  32.  
  33.         $tax_total = 0;
  34.         $sub_total = 0;
  35.         $order_products = [];
  36.         foreach ( $products as $row ) {
  37.             $tax_total += $row->order_product_tax * $row->order_product_quantity;
  38.             $sub_total += $row->order_product_price * $row->order_product_quantity;
  39.             $unit_price = ! empty( $row->order_product_price_before_discount ) ? $row->order_product_price_before_discount : $row->order_product_price;
  40.             $order_products[] = [
  41.                 'product_id' => $row->product_id,
  42.                 'product_name' => $row->order_product_name,
  43.                 'qty' => $row->order_product_quantity,
  44.                 'unit_price' => number_format( $unit_price, 2, '.', '' ),
  45.                 'subtotal' => number_format( $unit_price * $row->order_product_quantity, 2, '.', '' ),
  46.                 'discount' => number_format( ( $unit_price * $row->order_product_quantity ) - ( $row->order_product_price * $row->order_product_quantity ), 2, '.', '' ),
  47.                 'subtotal_after_discount' => number_format( $row->order_product_price * $row->order_product_quantity, 2, '.', '' ),
  48.                 'tax' => number_format( $row->order_product_tax * $row->order_product_quantity, 2, '.', '' ),
  49.                 'total' => number_format( ( $row->order_product_price + $row->order_product_tax ) * $row->order_product_quantity, 2, '.', '' ),
  50.             ];
  51.         }
  52.  
  53.         $billing_address = $db->setQuery( '
  54.             SELECT a.*, c.zone_code_3 as country_code, s.zone_name AS state_name
  55.               FROM #__hikashop_address a
  56.               LEFT JOIN #__hikashop_zone c ON c.zone_type="country" AND c.zone_namekey=a.address_country
  57.               LEFT JOIN #__hikashop_zone s ON s.zone_type="state" AND s.zone_namekey=a.address_state
  58.              WHERE a.address_id=' . (int) $order->order_billing_address_id . '
  59.         ' )->loadObject();
  60.         $shipping_address = $db->setQuery( '
  61.             SELECT a.*, c.zone_code_3 as country_code, s.zone_name AS state_name
  62.               FROM #__hikashop_address a
  63.               LEFT JOIN #__hikashop_zone c ON c.zone_type="country" AND c.zone_namekey=a.address_country
  64.               LEFT JOIN #__hikashop_zone s ON s.zone_type="state" AND s.zone_namekey=a.address_state
  65.              WHERE a.address_id=' . (int) $order->order_shipping_address_id . '
  66.         ' )->loadObject();
  67.  
  68.         $shipping_method = [];
  69.         if ( ! empty( $order->order_shipping_id ) ) {
  70.             $sids = array_map( 'intval', explode( ';', $order->order_shipping_id ) );
  71.             $shippings = $db->setQuery( 'SELECT * FROM #__hikashop_shipping WHERE shipping_id IN(' . implode(',', $sids) . ')' )->loadObjectList( 'shipping_id' );
  72.             foreach ( explode( ';', $order->order_shipping_id ) as $sid_full ) {
  73.                 list( $sid_full ) = explode( '@', $sid_full ); // remove warehouse part
  74.                 $sid = (int) $sid_full;
  75.                 if ( ! isset( $shippings[ $sid ] ) ) {
  76.                     continue;
  77.                 }
  78.                 if ( $shippings[ $sid ]->shipping_type == 'wsg_lulu' ) {
  79.                     list( $sid, $part ) = explode( '-', $sid_full );
  80.                     $sname = 'Lulu shipping - ' . $part;
  81.                 }
  82.                 else {
  83.                     $sname = $shippings[ $sid ]->shipping_name;
  84.                 }
  85.                 $shipping_method[] = [
  86.                     'id' => $sid,
  87.                     'name' => $sname,
  88.                 ];
  89.             }
  90.         }
  91.  
  92.         $coupon_codes = $db->setQuery( 'SELECT coupon_code FROM #__awocoupon_history WHERE estore="hikashop" AND order_id=' . (int) $order->order_id )->loadColumn();
  93.  
  94.         $data = [
  95.             'order_id' => $order->order_id,
  96.             'order_status' => $order->order_status,
  97.             'updated_at' => gmdate( 'Y-m-d H:i:s', $order->order_modified ),
  98.             'currency' => $db->setQuery( 'SELECT currency_code FROM #__hikashop_currency WHERE currency_id=' . (int) $order->order_currency_id )->loadResult(),
  99.             'coupon_codes' => ! empty( $coupon_codes ) ? implode( ',', $coupon_codes ) : '',
  100.             'billing_address' => empty( $billing_address->address_id ) ? null : [
  101.                 'first_name' => $billing_address->address_firstname,
  102.                 'last_name' => $billing_address->address_lastname,
  103.                 'company' => $billing_address->address_company,
  104.                 'address_1' => $billing_address->address_street,
  105.                 'address_2' => $billing_address->address_street2,
  106.                 'post_code' => $billing_address->address_post_code,
  107.                 'city' => $billing_address->address_city,
  108.                 'country' => $billing_address->country_code,
  109.                 'state' => $billing_address->state_name,
  110.             ],
  111.             'shipping_address' => empty( $shipping_address->address_id ) ? null : [
  112.                 'first_name' => $shipping_address->address_firstname,
  113.                 'last_name' => $shipping_address->address_lastname,
  114.                 'company' => $shipping_address->address_company,
  115.                 'address_1' => $shipping_address->address_street,
  116.                 'address_2' => $shipping_address->address_street2,
  117.                 'post_code' => $shipping_address->address_post_code,
  118.                 'city' => $shipping_address->address_city,
  119.                 'country' => $shipping_address->country_code,
  120.                 'state' => $shipping_address->state_name,
  121.             ],
  122.             'order_products' => $order_products,
  123.             'shipping_method' => $shipping_method,
  124.             'payment_method' => [
  125.                 'id' => $order->order_payment_id,
  126.                 'name' => $db->setQuery( 'SELECT payment_name FROM #__hikashop_payment WHERE payment_id=' . (int) $order->order_payment_id )->loadResult(),
  127.             ],
  128.             'order_totals' => [
  129.                 'subtotal' => number_format( $sub_total, 2, '.', '' ),
  130.                 'coupon' => number_format( $order->order_discount_price - $order->order_discount_tax, 2, '.', '' ),
  131.                 'shipping' => number_format( $order->order_shipping_price - $order->order_shipping_tax, 2, '.', '' ),
  132.                 'tax' => number_format( $tax_total + $order->order_shipping_tax, 2, '.', '' ),
  133.                 'total' => number_format( $order->order_full_price, 2, '.', '' ),
  134.             ],
  135.         ];
  136.  
  137.         $instance = Helper::instance()->init( \Brevo\Client\Api\EventsApi::class );
  138.         if ( empty( $instance ) ) {
  139.             return false;
  140.         }
  141.         try {
  142.             $result = $instance->createEvent( new \Brevo\Client\Model\Event( [
  143.                 'eventName' => 'order_updated',
  144.                 'identifiers' => [
  145.                     'email_id' => $joomla_user->email,
  146.                 ],
  147.                 'eventProperties' => $data,
  148.             ] ) );
  149.         }
  150.         catch ( \Exception $e ) { return false; }
  151.  
  152.         //Helper::instance()->mail_it( 'order_updated', [ 'email' => $joomla_user->email, 'data' => $data ] );
  153.  
  154.     }
Advertisement
Add Comment
Please, Sign In to add comment