Search code examples
wordpresswoocommercecustom-wordpress-pageswoocommerce-theming

Default SHORT description in woocommerce product pages


I would like to add a default line in all the woocommerce product short description forms, something like "free local shipping". It will be great if this line line in the short description can be edited or removed everytime a new product is being edited. Can someone please let me know how if you know the way to get this done. Thank you for your help in advance.

J


Solution

  • in order to achive you taarget and have the ability to change the Text area in case we need to display different massage other than the default in the product page we need to do three steps:

    Step 1: Add Text Area to Product General Tab in the admin panel

    // Display Text in Admin Panel 
    add_action('woocommerce_product_options_general_product_data', 'product_custom_text_area');
    
    function product_custom_text_area()
    {
    
        // Custom Product Text Area
    
        woocommerce_wp_textarea_input(
            array(
            'id'          => '_optional_textarea',
            'label'       => __('Optional Text Area', 'woocommerce'),
            'placeholder' => 'Product Text',
            'desc_tip' => 'true',
            'description' => __('This Text will be Displayed in Product Short Desc', 'woocommerce')
            )
        );
    }
    

    Step 2: Save The Text in our Database in case we added text

    // Save Fields
    add_action('woocommerce_process_product_meta', 'product_custom_text_area_save');
    
    function product_custom_text_area_save($post_id)
    {
        if (!empty($_POST['_optional_textarea'])) {
            update_post_meta($post_id, '_optional_textarea', esc_attr($_POST['_optional_textarea']));
        }
    }
    

    Step 3: Display our text in product page if exist if not display our Default message

    //Display The Text in Product Page
    
    add_action('woocommerce_before_add_to_cart_form', 'display_text_area');
    
    function display_text_area()
    {
        global $post;
        if (get_post_meta($post->ID, '_optional_textarea', true)) {
            echo get_post_meta($post->ID, '_optional_textarea', true);
            return;
        }
    
        echo __('FREE LOCAL SHIPPING', 'woocommerce');
    }
    

    Default Output

    Default Output

    BackEnd

    enter image description here

    if you want to display only Default hardcoded text in the short description you need to use only this function:

    //Display The Text in Product Page
    
    add_action('woocommerce_before_add_to_cart_form', 'display_text_area');
    
    function display_text_area()
    {
    
        echo __('FREE LOCAL SHIPPING', 'woocommerce');
    }
    

    Just Place the codes above in your functions.php and you are good to go.