简体   繁体   English

在 WooCommerce 存档页面上获取并显示产品自定义字段

[英]Get and display product custom field on WooCommerce archive pages

I have successfully added a custom Metabox to admin product pages.我已成功将自定义 Metabox 添加到管理产品页面。 The above code generate an Input type text in the Admin-Area where I can insert the city name:上面的代码在管理区域中生成一个输入类型的文本,我可以在其中插入城市名称:

add_action( 'save_post_product', 'set_post_metas_city', 10, 3 );
function set_post_metas_city( $post_id, $post, $update ) {
    if(isset($_POST['city'])) 
        update_post_meta($post_id, 'city', esc_attr($_POST['city']));
}

add_action( 'add_meta_boxes', 'metas_boxes_city', 50 );
function metas_boxes_city(){
    add_meta_box( "options-city", "City Name", 'edit_form_after_title_cidade', 'product', 'normal' );
}

function edit_form_after_title_city($post) { ?>
    <div id="mdiv">
        <input type="text" style="width: 100%" name="city" value="<?php echo($post ? get_post_meta($post->ID,'city',true) : ''); ?>" id="city" spellcheck="true" autocomplete="off" placeholder="<?php _e('Insert the city'); ?>">
    </div>
    <?php 
}

Code is in the active theme's functions.php file.代码位于活动主题的functions.php 文件中。

Now I use the following the hooked function, trying to display on the Shop Page, the custom field value (the city name) as follows:现在我使用以下钩子函数,尝试在商店页面上显示自定义字段值(城市名称)如下:

// define the woocommerce_after_shop_loop_item callback 

function action_woocommerce_after_shop_loop_item(  ) { 

// make action magic happen here... 

echo($post ? get_post_meta($post->ID,'city',true) : '');    
    
}; 
             
// add the action 
add_action( 'woocommerce_after_shop_loop_item', 'action_woocommerce_after_shop_loop_item', 10, 0 ); 

So I am trying to get something like:所以我想得到类似的东西:

[Product Picture]
**City**
Category
Cake Ananas
$4,00
[BUY BUTTON]

But it still doesn't shows nothing up.但它仍然没有显示任何内容。

If I use a static content inside the echo, I get the desired display on the Shop Page:如果我在回声中使用静态内容,我会在商店页面上获得所需的显示:

// define the woocommerce_after_shop_loop_item callback 
function action_woocommerce_after_shop_loop_item(  ) { 
    // make action magic happen here... 
    echo "Statictest";  
}; 

But the correct variable is missing..但是缺少正确的变量..

Another point is, I don't know in this case in which page to run the action.另一点是,在这种情况下,我不知道在哪个页面上运行该操作。

// run the action
do_action( 'woocommerce_after_shop_loop_item' ); 

What am I doing wrong please?请问我做错了什么? Could someone give a Help please?有人可以帮忙吗?

The main problem was in your last function with $post->ID .主要问题出在您最后一个带有$post->ID函数中。 Since WooCommerce 3, there is also a much better hook than save_post_product .从 WooCommerce 3 开始,还有一个比save_post_product更好的钩子。 Try the following revisited code instead:请尝试使用以下重新访问的代码:

// Add custom product meta box
add_action( 'add_meta_boxes', 'add_product_metas_box_city', 50 );
function add_product_metas_box_city(){
    add_meta_box( "options-city", __("City Name"), 'product_metas_box_city_content_callback', 'product', 'normal', 'high' );
}

// custom product meta box
function product_metas_box_city_content_callback( $post ) {
    $value = get_post_meta( $post->ID, 'city', true );

    echo '<div id="mdiv">
        <input type="text" style="width: 100%" name="city" value="'. $value .'" id="city" spellcheck="true" autocomplete="off" placeholder="'. __('Insert the city') .'">
    </div>';
}

// Save city custom field value
add_action( 'woocommerce_admin_process_product_object', 'save_product_city_meta_value' );
function save_product_city_meta_value( $product ) {
    if( isset($_POST['city']) ) {
        $product->update_meta_data( 'city', sanitize_text_field( $_POST['city'] ) );
    }
}

// Display city value on frontend products loop
add_action( 'woocommerce_after_shop_loop_item', 'action_woocommerce_after_shop_loop_item', 10, 0 );
function action_woocommerce_after_shop_loop_item() {
    global $product;

    $city = $product->get_meta('city');

    if ( $city ) {
        echo '<p class="city">'. $city .'</p>';
    }
}

Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce 存档页面中的产品标题下方显示 ACF 字段/图像 - Display ACF field/image below the product title in WooCommerce archive pages 在 WooCommerce 产品存档页面上显示自定义分类术语 - Display custom taxonomy terms on WooCommerce product archive pages 在Woocommerce单个产品页面中显示高于产品价格的自定义字段 - Display a custom field above product price in Woocommerce single product pages 显示自定义字段吹产品标题Woocommerce产品单页 - Display custom field blow product title Woocommerce product single pages 在Woocommerce存档页面中显示特定产品属性 - Display specific product attribute in Woocommerce archive pages 通过钩子在 WooCommerce 单个产品页面中显示自定义字段值 - Display custom field value in WooCommerce single product pages via hook 在 Woocommerce 单个产品页面的简短描述下显示自定义字段 - Display a custom field under short description in Woocommerce single product pages 在 WooCommerce 管理订单页面中保留并显示产品自定义字段值 - Keep and display product custom field value in WooCommerce Admin Order pages 在 Woocommerce 存档页面中的产品标题下显示特定的产品属性 - Display specific product attributes under product title in Woocommerce archive pages 在 WooCommerce 存档页面上显示产品名称和星级之间的自定义字段 - Show custom field between product name and star ratings on WooCommerce archive pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM