在 WooCommerce 单一产品页面的正文中获取一些产品数据
Get some product data in the body for WooCommerce Single product pages
我正在尝试提取正在查看的产品的产品 ID,以便将该 ID 传递到 TrustPilot 的小部件中以显示对该产品的评论。
我试过使用以下代码创建代码段:
global $product;
$id = $product->get_id()
但是我遇到了一个错误,而且 - 老实说 - 我真的不明白我在做什么!我一天的大部分时间都在浏览解决方案,所以我希望有人能提供帮助!
如前所述,目标是能够填充 TrustPilot“TrustBox”中的 data-sku="xxxxxxxx"
字段 - 一个 javascript 工具,可以从 trustpilot 中提取和呈现评论。我认为需要在元素的 HTML 中更新 ID。
TrustPilot 的全部代码如下所示:
将此添加到页眉
<!-- TrustBox script -->
<script type="text/javascript" src="//widget.trustpilot.com/bootstrap/v5/tp.widget.bootstrap.min.js" async></script>
<!-- End TrustBox script -->
将此添加到正文中,您希望小部件出现的位置
<!-- TrustBox widget - Product Reviews SEO -->
<div class="trustpilot-widget" data-locale="en-GB" data-template-id="5717796816f630043868e2e8" data-businessunit-id="5d472f87ff988700011c73b8" data-style-height="700px" data-style-width="100%" data-theme="light" data-sku="487" data-name="10 Plantshake Pack" data-review-languages="en">
<a href="https://uk.trustpilot.com/review/foga.co" target="_blank" rel="noopener">Trustpilot</a>
</div>
<!-- End TrustBox widget -->
已更新
要获取单个产品页面中的产品 ID,您可以使用:$product_id = get_the_ID();
为了确保在单个产品页面中获取产品对象,您可以使用:
$product = wc_get_product( get_the_ID() );
甚至更好:
global $product;
if ( ! is_a( $product, 'WC_Product' ) ) {
$product = wc_get_product( get_the_ID() );
}
现在您可以使用挂钩函数,将所有相关代码插入到单个产品页面中。您的 javscript 将使用产品名称和 sku 动态填充:
add_action('woocommerce_before_single_product', 'action_before_single_product', 5 );
function action_before_single_product(){
global $product;
$id = $product->get_id(); // Product Id
$sku = $product->get_sku(); // Product sku
$name = $product->get_name(); // Product name
?>
<!-- TrustBox widget - Product Reviews SEO -->
<div class="trustpilot-widget" data-locale="en-GB" data-template-id="5717796816f630043868e2e8" data-businessunit-id="5d472f87ff988700011c73b8" data-style-height="700px" data-style-width="100%" data-theme="light" data-sku="<?php echo $sku; ?>" data-name="<?php echo $name; ?>" data-review-languages="en">
<a href="https://uk.trustpilot.com/review/foga.co" target="_blank" rel="noopener">Trustpilot</a>
</div>
<!-- End TrustBox widget -->
<?php
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该有效。
我正在尝试提取正在查看的产品的产品 ID,以便将该 ID 传递到 TrustPilot 的小部件中以显示对该产品的评论。
我试过使用以下代码创建代码段:
global $product;
$id = $product->get_id()
但是我遇到了一个错误,而且 - 老实说 - 我真的不明白我在做什么!我一天的大部分时间都在浏览解决方案,所以我希望有人能提供帮助!
如前所述,目标是能够填充 TrustPilot“TrustBox”中的 data-sku="xxxxxxxx"
字段 - 一个 javascript 工具,可以从 trustpilot 中提取和呈现评论。我认为需要在元素的 HTML 中更新 ID。
TrustPilot 的全部代码如下所示:
将此添加到页眉
<!-- TrustBox script -->
<script type="text/javascript" src="//widget.trustpilot.com/bootstrap/v5/tp.widget.bootstrap.min.js" async></script>
<!-- End TrustBox script -->
将此添加到正文中,您希望小部件出现的位置
<!-- TrustBox widget - Product Reviews SEO -->
<div class="trustpilot-widget" data-locale="en-GB" data-template-id="5717796816f630043868e2e8" data-businessunit-id="5d472f87ff988700011c73b8" data-style-height="700px" data-style-width="100%" data-theme="light" data-sku="487" data-name="10 Plantshake Pack" data-review-languages="en">
<a href="https://uk.trustpilot.com/review/foga.co" target="_blank" rel="noopener">Trustpilot</a>
</div>
<!-- End TrustBox widget -->
已更新
要获取单个产品页面中的产品 ID,您可以使用:$product_id = get_the_ID();
为了确保在单个产品页面中获取产品对象,您可以使用:
$product = wc_get_product( get_the_ID() );
甚至更好:
global $product;
if ( ! is_a( $product, 'WC_Product' ) ) {
$product = wc_get_product( get_the_ID() );
}
现在您可以使用挂钩函数,将所有相关代码插入到单个产品页面中。您的 javscript 将使用产品名称和 sku 动态填充:
add_action('woocommerce_before_single_product', 'action_before_single_product', 5 );
function action_before_single_product(){
global $product;
$id = $product->get_id(); // Product Id
$sku = $product->get_sku(); // Product sku
$name = $product->get_name(); // Product name
?>
<!-- TrustBox widget - Product Reviews SEO -->
<div class="trustpilot-widget" data-locale="en-GB" data-template-id="5717796816f630043868e2e8" data-businessunit-id="5d472f87ff988700011c73b8" data-style-height="700px" data-style-width="100%" data-theme="light" data-sku="<?php echo $sku; ?>" data-name="<?php echo $name; ?>" data-review-languages="en">
<a href="https://uk.trustpilot.com/review/foga.co" target="_blank" rel="noopener">Trustpilot</a>
</div>
<!-- End TrustBox widget -->
<?php
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该有效。