【问题标题】:Price next to product title [woocommerce]产品标题旁边的价格 [woocommerce]
【发布时间】:2020-09-10 13:09:35
【问题描述】:

我有这个功能,我想在产品价格旁边添加。 我用get_price_html()$price = $product->get_price_html

function woocommerce_template_loop_product_title() {
echo '<h4 class="woocommerce-loop-product__title">' . get_the_title() . '</h4>'; }

电流输出

期望的输出

【问题讨论】:

  • 太好了。你能准确解释什么不起作用吗?
  • 我无法得到这个:产品名称:价格,我只有产品名称。当我添加到代码变量代码有错误和网站不工作例如连帽衫 $36
  • 您的代码只是检索标题 (get_the_title()) 您是否尝试过查找返回价格并将其添加到您的字符串的函数?

标签: php css function woocommerce product


【解决方案1】:
function woocommerce_template_loop_product_title() {
global $product;
$price = $product->get_price_html();
echo '<h4 class="woocommerce-loop-product__title h-price">' . get_the_title() .'&nbsp'. $price .'</h4>';}

我的回答。

【讨论】:

    【解决方案2】:

    您需要添加一个操作来更改标题。所以我为你写了这段代码,只需将它添加到你的functions.php中

    add_action( 'the_title', 'add_price_title' );
    
    function add_price_title($title) {
     $post_ID = get_the_ID();
     $the_post = get_post($post_ID);
     $date = $the_post->post_date;
     $maintitle = $the_post->post_title;
     $count='';
     $product = wc_get_product( $post_ID );
     $price = $product->get_price();
    
    
    if ($the_post->post_status == 'publish' AND $the_post->post_type == 'product' AND in_the_loop()) { 
     return "<span type='number' class='notbold'>".$title." $".$price.""."</span>";
    }
    
    else{
     return $title;   
    }
    }
    

    【讨论】:

      【解决方案3】:
      // define the woocommerce_shop_loop_item_title callback 
      function action_woocommerce_shop_loop_item_title() { 
          // make action magic happen here... 
          global $product;
          echo '<p>'.get_post_meta($product->get_id(), '_price', true).'</p>';
      }; 
      
      // add the action 
      add_action( 'woocommerce_shop_loop_item_title', 'action_woocommerce_shop_loop_item_title', 10 ); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-22
        • 1970-01-01
        • 2018-08-30
        • 1970-01-01
        • 2017-05-12
        • 2017-10-26
        • 2019-07-09
        • 2018-10-13
        相关资源
        最近更新 更多