Search code examples
phpwordpresswoocommercenestedwpml

PHP Nested functions logic error - translating functions.php with WPML


I am translating a piece of text that shows up on the checkout page with custom code that I am using. How do I correctly use nested functions in PHP?

I changed echo to a WPML recognizable function, but to no avail on the ​frontend.

add_action( 'woocommerce_review_order_before_submit', 'bbloomer_checkout_add_on', 9999 );

function bbloomer_checkout_add_on() {
   $product_ids = array( 14877, 14879, 15493 );
   $in_cart = false;
   foreach( WC()->cart->get_cart() as $cart_item ) {
      $product_in_cart = $cart_item['product_id'];
      if ( in_array( $product_in_cart, $product_ids ) ) {
         $in_cart = true;
         break;
      }
   }
   if ( ! $in_cart ) {
      echo '<h4><b>● Would you like to add 10/20/30 small sample vials?</b></h4>';

      function change_sm_location_search_title( $original_value ) {
    return '<h4><b>' . __('● Would you like to add 10/20/30 small sample vials?','text-domain') . '</b></h4>';
}
add_filter( 'sm-location-search-title', 'change_sm_location_search_title' );

      echo '<p><a class="button" style="width: 140px" href="?add-to-cart=1183"> €1.2 (10) </a><a class="button" style="width: 140px" href="?add-to-cart=9945"> €2.1 (20)</a><a class="button" style="width: 140px" href="?add-to-cart=9948"> €3 (30)</a></p>';
   }
} 

The echo stills shows up on frontend, but new text-domain function only shows up on backend.


Solution

  • Filters are used to replace values. You should bring the filter function declaration outside of the main function and use apply_filters call to use the filter.

    You can also use action hook instead. I suggest reading up on using hooks and filters: https://docs.presscustomizr.com/article/26-wordpress-actions-filters-and-hooks-a-guide-for-non-developers

    And this answer here for a better understanding how the filter is working: https://wordpress.stackexchange.com/questions/97356/trouble-understanding-apply-filters

    This should work (untested).

    add_action( 'woocommerce_review_order_before_submit', 'bbloomer_checkout_add_on', 9999 );
    
        function bbloomer_checkout_add_on() {
           $product_ids = array( 14877, 14879, 15493 );
           $in_cart = false;
           foreach( WC()->cart->get_cart() as $cart_item ) {
              $product_in_cart = $cart_item['product_id'];
              if ( in_array( $product_in_cart, $product_ids ) ) {
                 $in_cart = true;
                 break;
              }
           }
           if ( ! $in_cart ) {
              echo apply_filters('sm-location-search-title', 'Would you like to add 10/20/30 small sample vials?');
              echo '<p><a class="button" style="width: 140px" href="?add-to-cart=1183"> €1.2 (10) </a><a class="button" style="width: 140px" href="?add-to-cart=9945"> €2.1 (20)</a><a class="button" style="width: 140px" href="?add-to-cart=9948"> €3 (30)</a></p>';
           }
        }   
        function change_sm_location_search_title( $original_value ) {
          return '<h4><b>' . __($original_value,'text-domain') . '</b></h4>';
        }  
        add_filter( 'sm-location-search-title', 'change_sm_location_search_title');