Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function event_order_updated( $order_id ) {
- $db = JFactory::getDBO();
- $order = $db->setQuery( 'SELECT * FROM #__hikashop_order WHERE order_id=' . (int) $order_id )->loadObject();
- if ( empty( $order->order_id ) ) {
- return false;
- }
- $statuses = [ 'confirmed' ];
- if ( ! in_array( $order->order_status, $statuses ) ) {
- return;
- }
- $products = $db->setQuery( '
- SELECT i.*
- FROM #__hikashop_order o
- JOIN #__hikashop_order_product i ON i.order_id=o.order_id
- WHERE o.order_id=' . (int) $order->order_id . '
- GROUP BY i.order_product_id
- ' )->loadObjectList();
- if ( empty( $products ) ) {
- return;
- }
- $joomla_user_id = (int) $db->setQuery( 'SELECT user_cms_id FROM #__hikashop_user WHERE user_id=' . (int) $order->order_user_id )->loadResult();
- if ( $joomla_user_id < 1 ) {
- return false;
- }
- $joomla_user = JFactory::getUser( $joomla_user_id );
- if ( $joomla_user->id != $joomla_user_id ) {
- return false;
- }
- $tax_total = 0;
- $sub_total = 0;
- $order_products = [];
- foreach ( $products as $row ) {
- $tax_total += $row->order_product_tax * $row->order_product_quantity;
- $sub_total += $row->order_product_price * $row->order_product_quantity;
- $unit_price = ! empty( $row->order_product_price_before_discount ) ? $row->order_product_price_before_discount : $row->order_product_price;
- $order_products[] = [
- 'product_id' => $row->product_id,
- 'product_name' => $row->order_product_name,
- 'qty' => $row->order_product_quantity,
- 'unit_price' => number_format( $unit_price, 2, '.', '' ),
- 'subtotal' => number_format( $unit_price * $row->order_product_quantity, 2, '.', '' ),
- 'discount' => number_format( ( $unit_price * $row->order_product_quantity ) - ( $row->order_product_price * $row->order_product_quantity ), 2, '.', '' ),
- 'subtotal_after_discount' => number_format( $row->order_product_price * $row->order_product_quantity, 2, '.', '' ),
- 'tax' => number_format( $row->order_product_tax * $row->order_product_quantity, 2, '.', '' ),
- 'total' => number_format( ( $row->order_product_price + $row->order_product_tax ) * $row->order_product_quantity, 2, '.', '' ),
- ];
- }
- $billing_address = $db->setQuery( '
- SELECT a.*, c.zone_code_3 as country_code, s.zone_name AS state_name
- FROM #__hikashop_address a
- LEFT JOIN #__hikashop_zone c ON c.zone_type="country" AND c.zone_namekey=a.address_country
- LEFT JOIN #__hikashop_zone s ON s.zone_type="state" AND s.zone_namekey=a.address_state
- WHERE a.address_id=' . (int) $order->order_billing_address_id . '
- ' )->loadObject();
- $shipping_address = $db->setQuery( '
- SELECT a.*, c.zone_code_3 as country_code, s.zone_name AS state_name
- FROM #__hikashop_address a
- LEFT JOIN #__hikashop_zone c ON c.zone_type="country" AND c.zone_namekey=a.address_country
- LEFT JOIN #__hikashop_zone s ON s.zone_type="state" AND s.zone_namekey=a.address_state
- WHERE a.address_id=' . (int) $order->order_shipping_address_id . '
- ' )->loadObject();
- $shipping_method = [];
- if ( ! empty( $order->order_shipping_id ) ) {
- $sids = array_map( 'intval', explode( ';', $order->order_shipping_id ) );
- $shippings = $db->setQuery( 'SELECT * FROM #__hikashop_shipping WHERE shipping_id IN(' . implode(',', $sids) . ')' )->loadObjectList( 'shipping_id' );
- foreach ( explode( ';', $order->order_shipping_id ) as $sid_full ) {
- list( $sid_full ) = explode( '@', $sid_full ); // remove warehouse part
- $sid = (int) $sid_full;
- if ( ! isset( $shippings[ $sid ] ) ) {
- continue;
- }
- if ( $shippings[ $sid ]->shipping_type == 'wsg_lulu' ) {
- list( $sid, $part ) = explode( '-', $sid_full );
- $sname = 'Lulu shipping - ' . $part;
- }
- else {
- $sname = $shippings[ $sid ]->shipping_name;
- }
- $shipping_method[] = [
- 'id' => $sid,
- 'name' => $sname,
- ];
- }
- }
- $coupon_codes = $db->setQuery( 'SELECT coupon_code FROM #__awocoupon_history WHERE estore="hikashop" AND order_id=' . (int) $order->order_id )->loadColumn();
- $data = [
- 'order_id' => $order->order_id,
- 'order_status' => $order->order_status,
- 'updated_at' => gmdate( 'Y-m-d H:i:s', $order->order_modified ),
- 'currency' => $db->setQuery( 'SELECT currency_code FROM #__hikashop_currency WHERE currency_id=' . (int) $order->order_currency_id )->loadResult(),
- 'coupon_codes' => ! empty( $coupon_codes ) ? implode( ',', $coupon_codes ) : '',
- 'billing_address' => empty( $billing_address->address_id ) ? null : [
- 'first_name' => $billing_address->address_firstname,
- 'last_name' => $billing_address->address_lastname,
- 'company' => $billing_address->address_company,
- 'address_1' => $billing_address->address_street,
- 'address_2' => $billing_address->address_street2,
- 'post_code' => $billing_address->address_post_code,
- 'city' => $billing_address->address_city,
- 'country' => $billing_address->country_code,
- 'state' => $billing_address->state_name,
- ],
- 'shipping_address' => empty( $shipping_address->address_id ) ? null : [
- 'first_name' => $shipping_address->address_firstname,
- 'last_name' => $shipping_address->address_lastname,
- 'company' => $shipping_address->address_company,
- 'address_1' => $shipping_address->address_street,
- 'address_2' => $shipping_address->address_street2,
- 'post_code' => $shipping_address->address_post_code,
- 'city' => $shipping_address->address_city,
- 'country' => $shipping_address->country_code,
- 'state' => $shipping_address->state_name,
- ],
- 'order_products' => $order_products,
- 'shipping_method' => $shipping_method,
- 'payment_method' => [
- 'id' => $order->order_payment_id,
- 'name' => $db->setQuery( 'SELECT payment_name FROM #__hikashop_payment WHERE payment_id=' . (int) $order->order_payment_id )->loadResult(),
- ],
- 'order_totals' => [
- 'subtotal' => number_format( $sub_total, 2, '.', '' ),
- 'coupon' => number_format( $order->order_discount_price - $order->order_discount_tax, 2, '.', '' ),
- 'shipping' => number_format( $order->order_shipping_price - $order->order_shipping_tax, 2, '.', '' ),
- 'tax' => number_format( $tax_total + $order->order_shipping_tax, 2, '.', '' ),
- 'total' => number_format( $order->order_full_price, 2, '.', '' ),
- ],
- ];
- $instance = Helper::instance()->init( \Brevo\Client\Api\EventsApi::class );
- if ( empty( $instance ) ) {
- return false;
- }
- try {
- $result = $instance->createEvent( new \Brevo\Client\Model\Event( [
- 'eventName' => 'order_updated',
- 'identifiers' => [
- 'email_id' => $joomla_user->email,
- ],
- 'eventProperties' => $data,
- ] ) );
- }
- catch ( \Exception $e ) { return false; }
- //Helper::instance()->mail_it( 'order_updated', [ 'email' => $joomla_user->email, 'data' => $data ] );
- }
Advertisement
Add Comment
Please, Sign In to add comment