Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for the WooCommerce block 'all-products'. #304

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/admin/settings.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-components', 'wp-element', 'wp-i18n'), 'version' => '492875e2cff4b97d6fff');
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-components', 'wp-element', 'wp-i18n'), 'version' => '783bc0098ae10ff2b858');
2 changes: 1 addition & 1 deletion assets/admin/settings.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/admin/wizard.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'wp-api-fetch', 'wp-components', 'wp-element', 'wp-i18n'), 'version' => '41bbfcedb3247f67e9a1');
<?php return array('dependencies' => array('lodash', 'react', 'wp-api-fetch', 'wp-components', 'wp-element', 'wp-i18n'), 'version' => '19e8a41f77e996c8a8f6');
2 changes: 1 addition & 1 deletion assets/admin/wizard.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/frontend/woocommerce-blocks.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-hooks'), 'version' => '8e97ab5407a1a7355719');
<?php return array('dependencies' => array('wp-hooks', 'wp-i18n'), 'version' => '8644d72d73d05623cda7');
2 changes: 1 addition & 1 deletion assets/frontend/woocommerce-blocks.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Yes! Pagespeed is one of our main focus points, and we strive to make the plugin
= 1.X =

Enhancements:
* Added support for the WooCommerce block 'all-products'.
* Inline scripts are now registered with the wp_add_inline_script insted of wp_head. This allows easy extension GTM Kit and implementation of a CSP (Content Security Policy).

Bugfixes:
Expand Down
15 changes: 15 additions & 0 deletions src/Installation/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected function get_upgrades(): array {
$available_upgrades = [
'1.11' => 'v111_upgrade',
'1.14' => 'v114_upgrade',
'1.15' => 'v115_upgrade',
];

$current_version = \get_option( 'gtmkit_version' );
Expand Down Expand Up @@ -108,4 +109,18 @@ protected function v114_upgrade(): void {

Options::init()->set( $values, false, false );
}

/**
* Upgrade routine for v1.15
*/
protected function v115_upgrade(): void {

$values = [
'integrations' => [
'woocommerce_view_item_list_limit' => 0,
],
];

Options::init()->set( $values, false, false );
}
}
16 changes: 15 additions & 1 deletion src/Integration/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,19 @@ public function enqueue_scripts(): void {
} else {
$this->util->enqueue_script( 'gtmkit-woocommerce-checkout', 'integration/woocommerce-checkout.js', false, [ 'gtmkit-woocommerce' ] );
}
} elseif ( has_block( 'woocommerce/all-products' ) ) {

$this->util->enqueue_script( 'gtmkit-woocommerce-blocks', 'frontend/woocommerce-blocks.js', true );

wp_localize_script(
'gtmkit-woocommerce-blocks',
'gtmkitWooCommerceBlocksBuild',
[
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' ),
]
);

}
}

Expand All @@ -229,6 +242,7 @@ public function get_global_settings( array $global_settings ): array {
$global_settings['wc']['add_shipping_info']['config'] = (int) Options::init()->get( 'integrations', 'woocommerce_shipping_info' );
$global_settings['wc']['add_payment_info']['config'] = (int) Options::init()->get( 'integrations', 'woocommerce_payment_info' );
$global_settings['wc']['view_item']['config'] = (int) Options::init()->get( 'integrations', 'woocommerce_variable_product_tracking' );
$global_settings['wc']['view_item_list']['config'] = (int) Options::init()->get( 'integrations', 'woocommerce_view_item_list_limit' );
$global_settings['wc']['text'] = [
'wp-block-handpicked-products' => __( 'Handpicked Products', 'gtm-kit' ),
'wp-block-product-best-sellers' => __( 'Best Sellers', 'gtm-kit' ),
Expand Down Expand Up @@ -1035,7 +1049,7 @@ public function extend_store() {
*/
public function extend_product_data( $product ): array {
return array(
'item' => wp_json_encode( $this->get_item_data( $product ) ),
'item' => $this->get_item_data( $product ),
);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class Options {
'woocommerce_shipping_info',
'woocommerce_payment_info',
'woocommerce_variable_product_tracking',
'woocommerce_view_item_list_limit',
'cf7_load_js',
],

Expand Down Expand Up @@ -114,6 +115,7 @@ public static function get_defaults(): array {
'woocommerce_payment_info' => 1,
'cf7_load_js' => 1,
'woocommerce_variable_product_tracking' => 0,
'woocommerce_view_item_list_limit' => 0,
],
];
}
Expand Down Expand Up @@ -174,6 +176,7 @@ protected function postprocess_key_defaults( string $key ) {
break;

case 'woocommerce_variable_product_tracking':
case 'woocommerce_view_item_list_limit':
$value = 0;
break;
}
Expand Down
78 changes: 77 additions & 1 deletion src/js/frontend/woocommerce-blocks/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { addAction } from '@wordpress/hooks';

/**
* Internal dependencies
*/
import { actionPrefix, namespace } from './constants';
import { shippingInfo, paymentInfo, pushEvent } from './utils';
import {
getProductImpressionObject,
shippingInfo,
paymentInfo,
pushEvent,
} from './utils';

/**
* Track the shipping rate being set
Expand Down Expand Up @@ -103,6 +109,9 @@ addAction(
}
);

/**
* remove_from_cart.
*/
addAction(
`${actionPrefix}-cart-remove-item`,
namespace,
Expand All @@ -121,6 +130,9 @@ addAction(
}
);

/**
* add_to_cart.
*/
addAction(
`${actionPrefix}-cart-add-item`,
namespace,
Expand All @@ -138,3 +150,67 @@ addAction(
pushEvent('add_to_cart', eventParams);
}
);

const lists = [];

/**
* view_item_list.
*/
addAction(
`${actionPrefix}-product-list-render`,
namespace,
({ products, listName = __('Product List', 'gtm-kit') }) => {
if (products.length === 0 || window.gtmkit_data.wc.is_cart === true) {
return;
}

if (
window.gtmkit_settings.wc.view_item_list.config === 1 &&
Object.values(window.gtmkit_data.wc.blocks).includes(
'filter-wrapper'
)
) {
if (lists.includes(listName)) return;
lists.push(listName);
}

const eventParams = {
ecommerce: {
items: products.map((product, index) => ({
...getProductImpressionObject(product, listName),
index,
})),
},
};

pushEvent('view_item_list', eventParams);
}
);

/**
* select_item.
*/
addAction(
`${actionPrefix}-product-view-link`,
namespace,
({ product, listName = '' }) => {
const eventParams = {
ecommerce: {
item_list_name: listName,
items: [getProductImpressionObject(product, listName)],
},
};

pushEvent('select_item', eventParams);
}
);

/**
* Product Search
*/
addAction(`${actionPrefix}-product-search`, namespace, ({ searchTerm }) => {
const eventParams = {
search_term: searchTerm,
};
pushEvent('search', eventParams);
});
18 changes: 17 additions & 1 deletion src/js/frontend/woocommerce-blocks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const pushEvent = (eventName, eventParams) => {
window[window.gtmkit_settings.datalayer_name].push({ ecommerce: null });
window[window.gtmkit_settings.datalayer_name].push({
event: eventName,
eventParams,
...eventParams,
});

if (window.gtmkit_settings.console_log === 'on')
Expand Down Expand Up @@ -55,3 +55,19 @@ export const paymentInfo = () => {

window.gtmkit_data.wc.add_payment_info.fired = true;
};

/**
* Formats data into the impressionFieldObject shape.
*
* @param {Object} product
* @param {string} listName
*/
export const getProductImpressionObject = (product, listName = '') => {
const item = product.extensions.gtmkit.item;

if (listName) {
item.item_list_name = listName;
}

return item;
};
Loading