1

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Paulo do Porto
  • 606
  • 1
  • 9
  • 24

4 Answers4

1

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 );
Nathan Dawson
  • 18,138
  • 3
  • 52
  • 58
  • Nathan,...Thank you so much for the Help. The Magic still Not happened. I got the Country Name in the first position "above the second phrase" and the Country Name between Product Title and the Price disappeared. I changed the Hook priority up to 55 But nothing happens, the word remains in the first position. – Paulo do Porto Mar 19 '21 at 17:42
  • From 5 up to 55: add_action( 'woocommerce_after_shop_loop_item_title', 'wpse_wc_template_loop_country', 55 ); – Paulo do Porto Mar 19 '21 at 17:42
  • Just now i found a similar question and answer from Mr.LoicTheAztec, but i got unfortunatly the same result. The Text is inserted in the first position. Not between the Product Title and the Price.: https://stackoverflow.com/questions/48444564/add-a-custom-field-value-below-product-title-in-woocommerce-archives-pages – Paulo do Porto Mar 19 '21 at 17:49
  • It sounds like a theme issue then. You'd have to look firstly at what loop actions the theme is modifying and also whether the theme is overriding content-product.php with anything custom. – Nathan Dawson Mar 19 '21 at 17:50
  • Well @nathan-dawson, i use Astra Version 3.2.0. Would you like further informations about it? – Paulo do Porto Mar 19 '21 at 18:10
  • If you comment out the code entirely, is the country then hidden? I'm talking about just the hook and callback mentioned in my answer and none of the other code you've written. – Nathan Dawson Mar 19 '21 at 19:49
  • For sure it's hidden! Till now i didn't find the answer – Paulo do Porto Mar 19 '21 at 20:00
  • The code that's used to display the title, price, etc isn't the default. That's the only reason I can think of why the hooks outlined wouldn't be working as expected. It could be that the theme or another plugin is interfering with them or it could be that there's a template override within your theme. There's not much more I can suggest beyond pointing you in that direction. – Nathan Dawson Mar 19 '21 at 20:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230146/discussion-between-paulo-do-porto-and-nathan-dawson). – Paulo do Porto Mar 19 '21 at 20:17
1

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.

helgatheviking
  • 25,596
  • 11
  • 95
  • 152
  • Hello @helgatheviking Thank you for the Big help, but The Magic still not happened. I did a lot of tries with this Script above, but not yet. – Paulo do Porto Mar 19 '21 at 18:08
  • All meta_boxes that i created has the priority 50. does anyone see an influence of this priority on the action of hooks? add_action( 'add_meta_boxes', 'add_product_metas_box_country', 50 ); – Paulo do Porto Mar 19 '21 at 18:30
  • Is there a way to debug this to know what Hook is assuming the priority execution? – Paulo do Porto Mar 19 '21 at 18:36
  • Metaboxes have no bearing on front end display. Please try the above with a default theme. If it works then you know your theme is conflicting. Here's what I have: https://share.getcloudapp.com/p9u8vJ0l Title, meta, price. But check my postscript above, if you are trying to use blocks... this will not work. – helgatheviking Mar 20 '21 at 20:04
0

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

Paulo do Porto
  • 606
  • 1
  • 9
  • 24
0

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.