skip to Main Content

I have added a Metabox with a custom input field to admin products using the following code (added in the functions.php file):

// ****************************** COUNTRY ******************************** 
// /*
// |--------------------------------------------------------------------------
// | ADD CUSTOM PRODUCT META BOX - PAIS made in 10:37 07.10.2020
// | WOOCOMMERCE - PRODUTOS - EDITAR PRODUTO 
// |--------------------------------------------------------------------------
// */
    add_action( 'add_meta_boxes', 'add_product_metas_box_country', 50 );
    function add_product_metas_box_country(){
    add_meta_box( "options-country", __("Country Name"),'product_metas_box_country_content_callback', 'product', 'normal', 'high' );
   }
// /*
// |--------------------------------------------------------------------------
// | ADD CUSTOM PRODUCT META BOX HTML INPUT - PAIS made in 10:37 07.10.2020
// | WOOCOMMERCE - PRODUTOS - EDITAR PRODUTO 
// |--------------------------------------------------------------------------
// */
    function product_metas_box_country_content_callback( $post ) {
    $value = get_post_meta( $post->ID, 'country', true );

    echo '<div id="mdiv">
    <input type="text" style="width: 100%" name="country" value="'. $value .'" id="country" spellcheck="true" autocomplete="off" placeholder="'. __('Insira País') .'">
    </div>';
    }
///*
// |--------------------------------------------------------------------------
// | SAVE COUNTRY CUSTOM FIELD VALUE - PAIS made in 10:37 07.10.2020
// | WOOCOMMERCE - PRODUTOS - EDITAR PRODUTO 
// |--------------------------------------------------------------------------
// */
   add_action( 'woocommerce_admin_process_product_object', 'save_product_country_meta_value' );
   function save_product_country_meta_value( $product ) {
   if( isset($_POST['country']) ) {
   $product->update_meta_data( 'country', sanitize_text_field( $_POST['country'] ) );
   }
}

And it works:

Now The text that I insert in that custom input field must be shown BETWEEN the Product Title and the Price.

The Goal is to insert 5 or more different Texts BETWEEN the Product Title and the Price, in List Format, AND for individual products.

Accordingly with WooCommerce Archive Page [Visual Hook Guide] tutorial, i’m trying to use those Hooks but the Magic still not happened:

Used hook to show on the Shop Page in the functions.php file:

add_action( 'woocommerce_after_shop_loop_item', 'action_woocommerce_after_shop_loop_item', 20 ); // 1
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 250, 0 ); // 2
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 15 ); // 3
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10); // 4
function woocommerce_template_loop_rating() {
    global $product;

    $country = $product->get_meta('country');
  
    if ( $country) {
        echo '<p class="stateandcity">'. $country .'</p>';
    }
}

some of those Hooks hide completely the Price and this is not the goal.

Which hook or filter should i use to make the Magic happens please?

The only one Hook that i have a near result is with the number 2. But as the Picture shows, the Hook brings me a double result, the word two times.

I need to present 5 or more Words in List Format between the product title and the price.

Attached the photo with the result (The Word Brasil shown 2 times):

enter image description here

Anticipated Gratitude for Any Help

4

Answers


  1. Chosen as BEST ANSWER

    I didn't find a way to make the code above to work as I aspekted, so the alternative that i used was to use the same steps from this answer from three years ago. Thank you guys for the huge Help you all ofered me.

    Add a custom field value below product title in WooCommerce archives pages


  2. WooCommerce has an existing callback named woocommerce_template_loop_rating() so by writing a custom callback and giving it the same name, you’re overriding the original.

    The reason you’re seeing the value inserted twice is because you’re replacing the existing callback that’s already hooked by WooCommerce and then using add_action to insert it again.

    The first thing you’re going to want to do is change your callback’s name.

    Example:

    function wpse_wc_template_loop_country() {
    

    Then insert it using the right action. If you look inside woocommerce/templates/content-product.php, you’ll see the actions that fire and the callbacks that are hooked by default.

    Since your goal is to show the country between the title and the price, you probably want the woocommerce_after_shop_loop_item_title hook. Price is given a priority of 10 which means to get your callback to appear before price, you need to use a lower priority.

    Example:

    add_action( 'woocommerce_after_shop_loop_item_title', 'wpse_wc_template_loop_country', 5 );
    
    Login or Signup to reply.
  3. The price and rating are added (by default) to

    add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
    add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
    

    Therefore if you want extra data to appear after the title, but before the price/rating you would need to use that same hook with a lower priority.

    function kia_template_custom_meta() {
        global $product;
    
        $country = $product->get_meta('country');
      
        if ( $country) {
            echo '<p class="stateandcity">'. $country .'</p>';
        }
    }
    add_action( 'woocommerce_after_shop_loop_item_title', 'kia_template_custom_meta', 2 );
    

    PS- Worth noting that this is only valid for the regular shop loop and will not have an impact on Block content.

    Login or Signup to reply.
  4. For Astra theme, instead of using 'woocommerce_after_shop_loop_item_title' use this 'astra_woo_shop_title_after' and your text will appear after the product title on shop page.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search