Skip to content

Commit dd577f0

Browse files
Add mount point for attribution order meta box
Co-authored-by: asvinb <asvin.balloo@gmail.com> based on #3245 PoC
1 parent dd249e1 commit dd577f0

File tree

6 files changed

+107
-0
lines changed

6 files changed

+107
-0
lines changed

js/src/meta-boxes/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import './order-attribution';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import './google-ads-promo.scss';
5+
import useAdsCampaigns from '~/hooks/useAdsCampaigns';
6+
7+
/**
8+
* Google Ads Promo component.
9+
*
10+
* @return {JSX.Element|null} The Google Ads Promo component or null.
11+
*/
12+
const GoogleAdsPromo = () => {
13+
const { data: campaigns, loading } = useAdsCampaigns();
14+
15+
if ( loading || ! Array.isArray( campaigns ) ) {
16+
return null;
17+
}
18+
19+
return <div className="gla-google-ads-promo"></div>;
20+
};
21+
22+
export default GoogleAdsPromo;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.gla-google-ads-promo {
2+
/*
3+
* Styles go here.
4+
*/
5+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { createRoot, lazy, Suspense } from '@wordpress/element';
5+
6+
/**
7+
* Internal dependencies
8+
*/
9+
import { glaData } from '~/constants';
10+
11+
const GoogleAdsPromo = lazy( () =>
12+
import( /* webpackChunkName: "google-ads-promo" */ './google-ads-promo' )
13+
);
14+
15+
document.addEventListener( 'DOMContentLoaded', () => {
16+
// Only render the component if the order attribution source is Google
17+
if ( glaData?.orderAttributionSource !== 'google' ) {
18+
return;
19+
}
20+
21+
const orderAttributionBox = document.querySelector(
22+
'#woocommerce-order-source-data .inside'
23+
);
24+
25+
if ( ! orderAttributionBox ) {
26+
return;
27+
}
28+
29+
// Create empty div to serve as mount point for React components
30+
const glaElement = document.createElement( 'div' );
31+
32+
const root = createRoot( glaElement );
33+
34+
root.render(
35+
<Suspense>
36+
<GoogleAdsPromo />
37+
</Suspense>
38+
);
39+
40+
orderAttributionBox.prepend( glaElement );
41+
} );

src/Admin/Admin.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,40 @@ protected function get_assets(): array {
199199
$product_condition
200200
) );
201201

202+
$wc_edit_order_page_condition = function () {
203+
$screen = get_current_screen();
204+
205+
if ( ! $screen || 'woocommerce_page_wc-orders' !== $screen->id ) {
206+
return false;
207+
}
208+
209+
return isset( $_GET['action'] ) && 'edit' === $_GET['action']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
210+
};
211+
212+
$assets[] = ( new AdminScriptWithBuiltDependenciesAsset(
213+
'gla-wc-orders',
214+
'js/build/meta-boxes',
215+
"{$this->get_root_dir()}/js/build/meta-boxes.asset.php",
216+
new BuiltScriptDependencyArray(
217+
[
218+
'dependencies' => [],
219+
'version' => (string) filemtime( "{$this->get_root_dir()}/js/build/meta-boxes.js" ),
220+
]
221+
),
222+
$wc_edit_order_page_condition
223+
) )->add_inline_script(
224+
'glaData',
225+
[
226+
'slug' => $this->get_slug(),
227+
'adsSetupComplete' => $this->ads->is_setup_complete(),
228+
'initialWpData' => [
229+
'version' => $this->get_version(),
230+
'mcId' => $this->options->get_merchant_id() ?: null,
231+
'adsId' => $this->options->get_ads_id() ?: null,
232+
],
233+
]
234+
);
235+
202236
return $assets;
203237
}
204238

webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const webpackConfig = {
9696
'index.js'
9797
),
9898
blocks: path.join( __dirname, 'js/src/blocks/index.js' ),
99+
'meta-boxes': path.join( __dirname, 'js/src/meta-boxes/index.js' ),
99100
'gtag-events': path.resolve(
100101
process.cwd(),
101102
'js/src/gtag-events',

0 commit comments

Comments
 (0)