如何在单个产品页面 woocommerce 上显示高级自定义字段 Google 地图

How to display Advanced Custom Field Google Map on single product page woocommerce

我想使用 ACF Google 地图在我的单个产品页面的简短描述部分下方显示 google 地图。

我在 ACF 中遵循了这个教程:https://www.advancedcustomfields.com/resources/google-map/

我已经通过将 php 代码添加到 single-product.php 来测试解决方案:

<?php 

$location = get_field('location');

if( !empty($location) ):
?>
<div class="acf-map">
 <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
</div>
<?php endif; ?>

它显示在单个产品页面上,但显示位置不正确。

我想把地图放在 'woocommerce_single_product_summary' 位置的某处,但我知道怎么做。

是否可以通过挂钩添加它?

希望大家能帮帮我。

您需要使用 add_action(). Also, note that you need to define the priority as WooCommerce adds many functions to that same hook, (see source) 将您的代码添加到 woocommerce_single_product_summary 操作挂钩,优先级决定它们的顺序。我猜 30 是合适的,因为它应该在摘录之后。

add_action( 'woocommerce_single_product_summary', 'so_41625126_add_map', 30 );
function so_41625126_add_map() {

    if( function_exists( 'get_field' ) && $location = get_field( 'location' ) ) { ?>
        <div class="acf-map">
            <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
        </div>
    <?php }
}