Home > Mobile >  Change Button Text via PHP | WooCoommerce
Change Button Text via PHP | WooCoommerce

Time:10-20

Hello I have a button in WooCoommerce. I would like to translate the button. But I can't do that via LocoTranslate or anything else. Is there any PHP function to replace button texts without ID?

Here you can see the Code

Here you can see the button in the frontend

Best regards!

CodePudding user response:

add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
function woocommerce_template_loop_add_to_cart( $args = array() ) {
  global $product;

  if ( $product ) {
    $defaults = array(
      'quantity'   => 1,
      'class'      => implode( ' ', array_filter( array(
        'button',
        'product_type_' . $product->get_type(),
        $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
        $product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
      ) ) ),
      'attributes' => array(
        'data-product_id'  => $product->get_id(),
        'data-product_sku' => $product->get_sku(),
        'aria-label'       => $product->add_to_cart_description(),
        'rel'              => 'nofollow',
      ),
    );

    $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );

    wc_get_template( 'loop/add-to-cart.php', $args );
  }
}

add_filter( 'woocommerce_product_add_to_cart_text', 'woo_custom_cart_button_text' );    // 2.1  
function woo_custom_cart_button_text() {
        return __( 'Add to cart', 'woocommerce' );
}

CodePudding user response:

add_filter('gettext_woocommerce', 'rename_button_text_wc', 10, 3);

function rename_button_text_wc($translated_text, $text, $text_domain) {
    // bail if admin page or not modifying frontend woocommerce text.
    if (is_admin() || 'woocommerce' !== $text_domain) {
        return $translated_text;
    }

    if ('Check Out All' === $text) {
        $translated_text = 'Checkout';
    }

    return $translated_text;
}
  • Related