[英]Display ACF field/image below the product title in WooCommerce archive pages
[英]Get and display product custom field on WooCommerce archive pages
我已成功將自定義 Metabox 添加到管理產品頁面。 上面的代碼在管理區域中生成一個輸入類型的文本,我可以在其中插入城市名稱:
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
}
代碼位於活動主題的functions.php 文件中。
現在我使用以下鈎子函數,嘗試在商店頁面上顯示自定義字段值(城市名稱)如下:
// 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 );
所以我想得到類似的東西:
[Product Picture]
**City**
Category
Cake Ananas
$4,00
[BUY BUTTON]
但它仍然沒有顯示任何內容。
如果我在回聲中使用靜態內容,我會在商店頁面上獲得所需的顯示:
// define the woocommerce_after_shop_loop_item callback
function action_woocommerce_after_shop_loop_item( ) {
// make action magic happen here...
echo "Statictest";
};
但是缺少正確的變量..
另一點是,在這種情況下,我不知道在哪個頁面上運行該操作。
// run the action
do_action( 'woocommerce_after_shop_loop_item' );
請問我做錯了什么? 有人可以幫忙嗎?
主要問題出在您最后一個帶有$post->ID
函數中。 從 WooCommerce 3 開始,還有一個比save_post_product
更好的鈎子。 請嘗試使用以下重新訪問的代碼:
// 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>';
}
}
代碼位於活動子主題(或活動主題)的 functions.php 文件中。 測試和工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.