Last active
March 12, 2021 06:38
-
-
Save tamarazuk/bebcff2dd677097b4605bc14ace01592 to your computer and use it in GitHub Desktop.
WooCommerce Cart Notices: Translate Messages using WPML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // only copy this line if needed | |
/** | |
* Register Cart Notices texts for translation with WPML when the messages are first created/updated | |
*/ | |
add_filter( 'wc_cart_notices_update_fields', function( $fields ) { | |
if ( isset( $fields['name'] ) ) { | |
$slug = sanitize_title( $fields['name'] ); | |
if ( isset( $fields['message'] ) ) { | |
do_action( 'wpml_register_single_string', 'wc_cart_notices_fields', "message[{$slug}]", $fields['message'] ); | |
} | |
if ( isset( $fields['action'] ) ) { | |
do_action( 'wpml_register_single_string', 'wc_cart_notices_fields', "call_to_action[{$slug}]", $fields['action'] ); | |
} | |
if ( isset( $fields['action_url'] ) ) { | |
do_action( 'wpml_register_single_string', 'wc_cart_notices_fields', "call_to_action_url[{$slug}]", $fields['action_url'] ); | |
} | |
} | |
return $fields; | |
} ); | |
/** | |
* Translate Cart Notices notice message with WPML | |
* | |
* @param string $message the notice message | |
* @return string the translated notice message | |
*/ | |
add_filter( 'wc_cart_notices_notice_message', function ( $message, $notice_type, $notice ) { | |
if ( isset( $notice->name ) ) { | |
$slug = sanitize_title( $notice->name ); | |
$message = apply_filters( 'wpml_translate_single_string', $message, 'wc_cart_notices_fields', "message[$slug]" ); | |
} | |
return $message; | |
}, 10, 3 ); | |
/** | |
* Translate Cart Notices call to action with WPML | |
* | |
* @param object $notice the notice object | |
* @return object the notice object with a translated call to action | |
*/ | |
add_filter( 'wc_cart_notices_notice_object', function ( $notice ) { | |
if ( isset( $notice->name ) ) { | |
$slug = sanitize_title( $notice->name ); | |
// translate call to action | |
$notice->action = apply_filters( 'wpml_translate_single_string', $notice->action, 'wc_cart_notices_fields', "call_to_action[$slug]" ); | |
// translate call to action URL | |
$notice->action_url = apply_filters( 'wpml_translate_single_string', $notice->action_url, 'wc_cart_notices_fields', "call_to_action_url[$slug]" ); | |
} | |
return $notice; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment