【问题标题】:Add Text After the Price in WooCommerce Product Per Product ID/WP在每个产品 ID/WP 的 WooCommerce 产品价格后添加文本
【发布时间】:2022-01-09 19:02:46
【问题描述】:

我的子主题的functions.php文件中有以下代码,效果很好。

function themeprefix_custom_price_message( $price ) { 

global $post;

$product_id = $post->ID;
$my_product_array = array( 270,373,378,506,1306,1311,1312,1444,1445,1447,1449,1930,1932,1933,1934,1935,1963,4146,4152,4153,4154 );//add in product IDs
if ( in_array( $product_id, $my_product_array )) {
    $textafter = ' Each/Min 12'; //add your text
    return $price . '<span class="price-description">' . $textafter . '</span>';
 } 
 else 
 { 
    return $price; 
 } 
}
add_filter( 'woocommerce_get_price_html', 'themeprefix_custom_price_message' );

但是,现在我需要添加另一组产品并在价格后启用不同的文本,即每个/分钟 8。我尝试了对上述内容的各种更改,并且每次都将网站关闭。如何调整上述内容以在价格后添加另一组(然后可能是另一组)具有不同文本的产品 ID?提前致谢!

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    此答案适用于此处提出的问题:Add custom text after the price in WooCommerce

    不过,也可以用在上面的问题中。

    如果您想在每个产品后添加动态文本,您可以尝试类似的方法。

    /* add custom field in single product */
    
    add_action('woocommerce_product_options_general_product_data', function() {
        woocommerce_wp_text_input([
                'id' => '_custom_text_after_price',
                'label' => __('Custom Text after Price', 'themedomain'),
        ]);
    });
    

    您可以在您的产品中看到上述变化。

    现在添加以下代码以将元数据保存在您的产品中

    /* save meta field */
    add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
    function woocommerce_product_custom_fields_save($post_id)
    {
        $woocommerce_custom_product_text_field = $_POST['_custom_text_after_price'];
        if (!empty($woocommerce_custom_product_text_field))
            update_post_meta($post_id, '_custom_text_after_price', esc_attr($woocommerce_custom_product_text_field));
    }
    

    现在要在前端显示自定义代码,您需要使用 2 个钩子。

    /* show custom text on front end */
    function cw_change_product_price_display( $price ) {
        global $post, $product;
        if(get_post_meta( $post->ID, '_custom_text_after_price', true )){
            $text = get_post_meta( $post->ID, '_custom_text_after_price', true );
        }
        $price .= ' '.$text;
        return $price;
    }
    add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
    add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
    

    在前端,它会是这样的。

    【讨论】:

      【解决方案2】:

      您可以保持现有数组不变。通过从if 中删除else 来简化代码,然后在返回默认的纯价格文本之前开始声明和检查新产品数组...

      function themeprefix_custom_price_message( $price ) { 
      
          global $post;
      
          $product_id = $post->ID;
      
          $my_product_array = array( 270,373,378,506,1306,1311,1312,1444,1445,1447,1449,1930,1932,1933,1934,1935,1963,4146,4152,4153,4154 );//add in product IDs
      
          if ( in_array( $product_id, $my_product_array )) {
              $textafter = ' Each/Min 12'; //add your text
              return $price . '<span class="price-description">' . $textafter . '</span>';
          } 
      
          $my_product_array2 = array( 9990,9991,9992 );
      
          if ( in_array( $product_id, $my_product_array2 )) {
              $textafter = ' Each/Min 8'; //add your text
              return $price . '<span class="price-description">' . $textafter . '</span>';
          }
      
          $my_product_array3 = array( 9993,9994,9995 );
      
          if ( in_array( $product_id, $my_product_array3 )) {
              $textafter = ' Each/Min 4'; //add your text
              return $price . '<span class="price-description">' . $textafter . '</span>';
          }
      
          return $price;  
      }
      add_filter( 'woocommerce_get_price_html', 'themeprefix_custom_price_message' );
      

      【讨论】:

      • 谢谢@davmos 这很棒!衷心感谢您的帮助!
      • 不客气,帕特里夏,很高兴听到它有效 :)
      猜你喜欢
      • 1970-01-01
      • 2021-07-20
      • 2021-05-19
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      • 2018-10-13
      • 2018-04-23
      相关资源
      最近更新 更多