How can I change “Your order” text on checkout page in WooCommerce under certain conditions

前端 未结 1 677
野趣味
野趣味 2020-12-21 14:55

With this code I can change "Your Order" text in checkout page. But I need to change if specific product in my cart or virtual product is in my cart.



        
相关标签:
1条回答
  • 2020-12-21 15:29

    The text you want to change is located in checkout/form-checkout.php line 54

    <h3 id="order_review_heading"><?php esc_html_e( 'Your order', 'woocommerce' ); ?></h3>
    

    As you will see there are just before and after the

    • woocommerce_checkout_before_order_review_heading and
    • woocommerce_checkout_before_order_review hooks, only these do not apply to the H3 tag

    So gettext is recommended if you don't want to overwrite the template file.

    To debug this and other text you can use

    function filter_gettext( $translated, $text, $domain  ) {
        echo '<pre>', print_r( $text , 1 ), '</pre>';
        return $translated;
    }
    add_filter( 'gettext',  'filter_gettext', 10, 3 );
    

    So to answer your question, this should suffice

    • Check for a specific product ID
    function filter_gettext( $translated, $text, $domain  ) {
        if( $text == 'Your order' && is_checkout() && ! is_wc_endpoint_url() ) {        
            // HERE set the desired specific product ID
            $targeted_product_id = 1122;
    
            // Loop through cart items
            foreach( WC()->cart->get_cart() as $cart_item ) {
                if( $targeted_product_id == $cart_item['data']->get_id() ) {
                    $translated = __( 'İletişim Bilgileri', $domain );
                }
            }
        }
        return $translated;
    }
    add_filter( 'gettext',  'filter_gettext', 10, 3 );
    

    UPDATE 10/2020

    • You can use the following code to check for multiple product IDs
    function filter_gettext( $translated, $text, $domain  ) {
        if( $text == 'Your order' && is_checkout() && ! is_wc_endpoint_url() ) {        
            // HERE set the desired specific product IDs
            $targeted_product_ids = array( 1122, 30, 815 );
    
            // Loop through cart items
            foreach( WC()->cart->get_cart() as $cart_item ) {
                // In array
                if ( in_array( $cart_item['data']->get_id(), $targeted_product_ids ) ) {
                    $translated = __( 'İletişim Bilgileri', $domain );
                }
            }
        }
    
        return $translated;
    }
    add_filter( 'gettext',  'filter_gettext', 10, 3 );
    

    • To check for virtual products you could use
    function filter_gettext( $translated, $text, $domain  ) {
        if( $text == 'Your order' && is_checkout() && ! is_wc_endpoint_url() ) {
            // Loop through cart items
            foreach( WC()->cart->get_cart() as $cart_item ) {
                // Is virtual
                if ( $cart_item['data']->is_virtual() ) {
                    $translated = __( 'İletişim Bilgileri', $domain );
                }
            }
        }
        return $translated;
    }
    add_filter( 'gettext',  'filter_gettext', 10, 3 );
    
    0 讨论(0)
提交回复
热议问题