【问题标题】:woocommerce change position of pricewoocommerce 更改价格位置
【发布时间】:2016-06-03 09:41:20
【问题描述】:

尝试在页面上的主要内容之前呈现产品价格时出现此错误。

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 已加载。

如何确保加载全局 $product

【问题讨论】:

    标签: php wordpress woocommerce product price


    【解决方案1】:

    我认为你不想做的事情是不可能的。您将在循环之前和添加到购物车表单之外移动 price.php 的呈现。那么get_price_html()需要变量$price,这里没有。

    为此,我想您需要在 price.php 模板中更改如下内容:

    <?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-&gt;get_price_html() 替换为esc_attr( $product-&gt;get_price() ); 您可能还需要在其中插入循环......

    参考:Overriding Templates via a Theme

    【讨论】:

      【解决方案2】:

      只需复制单个产品价格模板中的代码并将其粘贴到您的挂钩函数中即可。 https://github.com/woothemes/woocommerce/blob/master/templates/single-product/price.php

      remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
      
      function price_in_title_row() {
      
      if ( ! defined( 'ABSPATH' ) ) {
            exit; // Exit if accessed directly
      }
      global $product;
      ?>
      
      <div class="price_in_title_row" itemprop="offers" itemscope itemtype="http://schema.org/Offer">
          <h3><?php echo $product->get_price_html(); ?></h3>
          <meta itemprop="price" content="<?php echo esc_attr( $product->get_display_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>
      
      }
      
      add_action('woocommerce_before_single_product', 'price_in_title_row', 50);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-24
        • 1970-01-01
        • 1970-01-01
        • 2020-06-09
        • 2018-05-17
        • 1970-01-01
        • 2021-11-23
        相关资源
        最近更新 更多