Base.php
6 years ago
EasyDigitalDownloads.php
6 years ago
MemberPress.php
6 years ago
Woocommerce.php
6 years ago
MemberPress.php
130 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * @package matomo |
| 8 | */ |
| 9 | |
| 10 | namespace WpMatomo\Ecommerce; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; // if accessed directly |
| 14 | } |
| 15 | |
| 16 | class MemberPress extends Base { |
| 17 | |
| 18 | public function register_hooks() { |
| 19 | if ( ! is_admin() ) { |
| 20 | parent::register_hooks(); |
| 21 | |
| 22 | add_action( 'template_redirect', array( $this, 'on_product_view' ), 99999, 0 ); |
| 23 | add_action( 'wp_footer', array( $this, 'on_order' ), 99999, 2 ); |
| 24 | add_action( 'mepr-signup', array( $this, 'on_cart_update' ), 99999, 1 ); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @param \MeprTransaction $transaction |
| 30 | */ |
| 31 | public function on_cart_update( $transaction ) { |
| 32 | $tracking_code = ''; |
| 33 | $sku = $transaction->id; |
| 34 | $product = $transaction->product(); |
| 35 | $params = array( |
| 36 | 'addEcommerceItem', |
| 37 | $sku, |
| 38 | $product->post_title, |
| 39 | $categories = array(), |
| 40 | $transaction->amount, |
| 41 | 1, |
| 42 | ); |
| 43 | $tracking_code .= $this->make_matomo_js_tracker_call( $params ); |
| 44 | |
| 45 | $total = $transaction->total; |
| 46 | $tracking_code .= $this->make_matomo_js_tracker_call( array( 'trackEcommerceCartUpdate', $total ) ); |
| 47 | |
| 48 | // we can't echo directly as we wouldn't know where in the template rendering stage we are and whether |
| 49 | // we're supposed to print or not etc |
| 50 | $this->cart_update_queue = $this->wrap_script( $tracking_code ); |
| 51 | $this->logger->log( 'Tracked ecommerce cart update: ' ); |
| 52 | } |
| 53 | |
| 54 | public function on_product_view() { |
| 55 | if ( ! is_singular( 'memberpressproduct' ) ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | $product_id = get_the_ID(); |
| 60 | |
| 61 | if ( empty( $product_id ) ) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | if ( ! class_exists( '\MeprProduct' ) ) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | $product = new \MeprProduct( $product_id ); |
| 70 | |
| 71 | $sku = $product_id; |
| 72 | |
| 73 | $params = array( |
| 74 | 'setEcommerceView', |
| 75 | '' . $sku, |
| 76 | $product->post_title, |
| 77 | $categories = array(), |
| 78 | $product->price, |
| 79 | ); |
| 80 | |
| 81 | echo $this->wrap_script( $this->make_matomo_js_tracker_call( $params ) ); |
| 82 | } |
| 83 | |
| 84 | public function on_order() { |
| 85 | if ( isset( $_GET['membership'] ) |
| 86 | && isset( $_GET['trans_num'] ) |
| 87 | && class_exists( '\MeprTransaction' ) ) { |
| 88 | $txn = \MeprTransaction::get_one_by_trans_num( (int) $_GET['trans_num'] ); |
| 89 | if ( isset( $txn->id ) && $txn->id > 0 ) { |
| 90 | if ( $this->has_order_been_tracked_already( $txn->id ) ) { |
| 91 | return; |
| 92 | } |
| 93 | $this->set_order_been_tracked( $txn->id ); |
| 94 | $transaction = new \MeprTransaction( $txn->id ); |
| 95 | $order_id_to_track = $txn->trans_num; |
| 96 | $product = $transaction->product(); |
| 97 | |
| 98 | $discount = 0; |
| 99 | |
| 100 | if ( $product && $transaction->coupon() ) { |
| 101 | $discount = $product->price - $txn->amount; |
| 102 | } |
| 103 | $tracking_code = ''; |
| 104 | $params = array( |
| 105 | 'addEcommerceItem', |
| 106 | '' . $product->ID, |
| 107 | $product->post_title, |
| 108 | array(), |
| 109 | $txn->amount, |
| 110 | 1, |
| 111 | ); |
| 112 | $tracking_code .= $this->make_matomo_js_tracker_call( $params ); |
| 113 | $params = array( |
| 114 | 'trackEcommerceOrder', |
| 115 | '' . $order_id_to_track, |
| 116 | $txn->total, |
| 117 | $txn->amount, |
| 118 | $txn->tax_amount, |
| 119 | $shipping = 0, |
| 120 | $discount, |
| 121 | ); |
| 122 | $tracking_code .= $this->make_matomo_js_tracker_call( $params ); |
| 123 | |
| 124 | echo $this->wrap_script( $tracking_code ); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | } |
| 130 |