Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Mini Cart block - fix translations handling #6153 #6158

Merged
merged 5 commits into from
Apr 1, 2022
Merged
Changes from 3 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
44 changes: 37 additions & 7 deletions src/BlockTypes/MiniCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ protected function enqueue_data( array $attributes = [] ) {
}

$this->scripts_to_lazy_load['wc-block-mini-cart-component-frontend'] = array(
'src' => $script_data['src'],
'version' => $script_data['version'],
'src' => $script_data['src'],
'version' => $script_data['version'],
'translations' => $this->get_dependencies_translations(),
);

$this->asset_data_registry->add(
Expand Down Expand Up @@ -260,11 +261,10 @@ protected function append_script_and_deps_src( $script ) {
return;
}
$this->scripts_to_lazy_load[ $script->handle ] = array(
'src' => $script->src,
'version' => $script->ver,
'before' => $wp_scripts->print_inline_script( $script->handle, 'before', false ),
'after' => $wp_scripts->print_inline_script( $script->handle, 'after', false ),
'translations' => $wp_scripts->print_translations( $script->handle, false ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking closer, I notice that the $wp_scripts->print_translations() of enqueued scripts cause the JS errors; even we don't echo them, they are still printed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if related, but I remember being confused by something similar and it was because we hook into pre_load_script_translations to print the translations, which runs no matter if you echo them or not:
https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/2d0270cc23121f79a5d3f1759a7e7df43f1323a4/woocommerce-gutenberg-products-block.php#L239

Copy link
Member

@dinhtungdu dinhtungdu Mar 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're the lifesaver @Aljullu! Thanks so much for the suggestion!

Aljullu marked this conversation as resolved.
Show resolved Hide resolved
'src' => $script->src,
'version' => $script->ver,
'before' => $wp_scripts->print_inline_script( $script->handle, 'before', false ),
'after' => $wp_scripts->print_inline_script( $script->handle, 'after', false ),
);
}

Expand Down Expand Up @@ -442,4 +442,34 @@ protected function get_tax_label() {
protected function get_cart_payload() {
return WC()->api->get_endpoint_data( '/wc/store/cart' );
}

/**
* Prepare translations for inner blocks and dependencies.
*/
protected function get_dependencies_translations() {
$wp_scripts = wp_scripts();
$translations = array();

$blocks = [
'mini-cart-contents-block/filled-cart',
'mini-cart-contents-block/empty-cart',
'mini-cart-contents-block/title',
'mini-cart-contents-block/items',
'mini-cart-contents-block/products-table',
'mini-cart-contents-block/footer',
'mini-cart-contents-block/shopping-button',
];
$chunks = preg_filter( '/$/', '-frontend', $blocks );

foreach ( $chunks as $chunk ) {
$handle = 'wc-blocks-' . $chunk . '-chunk';
$this->asset_api->register_script( $handle, $this->asset_api->get_block_asset_build_path( $chunk ), [], true );
$translations[] = $wp_scripts->print_translations( $handle, false );
wp_deregister_script( $handle );
}

$translations = array_filter( $translations );

return implode( '', $translations );
}
gigitux marked this conversation as resolved.
Show resolved Hide resolved
}