woocommerce更改价格位置 [英] woocommerce change position of price
问题描述
尝试在页面上的主要内容之前呈现产品价格时,出现此错误.
I get this error when trying to render the product price before the main content on the page.
Fatal error: Uncaught Error: Call to a member function get_price_html() on string in /wp-content/plugins/woocommerce/templates/single-product/price.php:27
这是我的代码:
//the remove action works fine
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
//this breaks it
add_action('woocommerce_before_main_content', 'woocommerce_template_single_price', 40);
我认为是因为我想在获取主要内容之前先获取价格,所以我需要确保已加载全局$product
.
I assume that because I'm trying to get the price before the main content I need to make sure the global $product
is loaded.
如何确保全局$product
已加载?
推荐答案
我认为您不愿做的事情是不可能的.您正在循环之前和添加购物车表格外部移动price.php的呈现.然后get_price_html()
需要变量$price
,此处不提供.
I don't think that what you wan't to do is possible. You are moving the rendering of price.php before the loop and outside the add-to-cart form. Then get_price_html()
need the variable $price
, not available in here.
为此,我想您需要在price.php
模板中进行以下更改:
To do that, I suppose you need to change in price.php
template with something like:
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product;
$price = esc_attr( $product->get_price() );
// $price = woocommerce_price( $product->regular_price ); // or this one
// $price = woocommerce_price( $product->sale_price ); // or this one too
?>
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<p class="price"><?php echo $product->get_price_html(); ?></p>
<meta itemprop="price" content="<?php echo esc_attr( $product->get_price() ); ?>" />
<meta itemprop="priceCurrency" content="<?php echo esc_attr( get_woocommerce_currency() ); ?>" />
<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
</div>
或将$product->get_price_html()
替换为esc_attr( $product->get_price() );
您可能还需要在其中插入循环……
Or replace $product->get_price_html()
by esc_attr( $product->get_price() );
You might need to insert the loop inside it too…
参考:通过主题覆盖模板
这篇关于woocommerce更改价格位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!