【问题标题】:How do I change the price string in WooCommerce subscriptions如何更改 WooCommerce 订阅中的价格字符串
【发布时间】:2014-02-02 19:41:39
【问题描述】:

我需要一个更好的方法来做到这一点。

目前,我已将代码直接添加到 class-wc-subscriptions-product.php 文件中的 get_price_string 函数中,因此当设置免费试用时,我可以更改添加到价格字符串中的文本。

当然,这远非完美。

那么,有谁知道我需要在主题文件夹中的 functions.php 文件中添加什么钩子才能执行相同的操作?

【问题讨论】:

    标签: wordpress woocommerce woocommerce-subscriptions


    【解决方案1】:

    这是我网站的一个非常简单的版本。我需要让它说“每季”而不是“每 3 个月”。最简单的方法是创建一个插件:

    <?php
    /**
     * Plugin Name: My Subscription Price Text
     * Description: Make it say "Every Season" instead of "Every 3 Months"
     * Author: green coder
     * Author URI: 
     * Version: 1.0
     * License: GPL v2
     */
    function my_subs_price_string( $pricestring ) {
    
        $newprice = str_replace( 'every 3 months', 'per season', $pricestring );
    return $newprice;
    
    }
    add_filter( 'woocommerce_subscriptions_product_price_string', 'my_subs_price_string' );
    add_filter( 'woocommerce_subscription_price_string', 'my_subs_price_string' );
    

    【讨论】:

    • 其他 WooCommerce 订阅插件也可以实现相同的功能,例如Subscriptio?
    【解决方案2】:

    好的老问题,但我最近需要这样做。我不想依赖字符串替换,所以我就是这样做的(可以根据您的需要进行调整 - 他们的关键是查看您可以使用的 $product 中的属性):

    add_filter( 'woocommerce_subscriptions_product_price_string', 'my_subs_price_string', 10, 3 );
    
    function my_subs_price_string( $subscription_string, $product, $include ) {
    
        /****
        var_dump($product); Various variables are available to us
        ****/
    
        return 'An initial easy payment of ' . wc_price( $product->subscription_sign_up_fee ) . 
            ', a ' . $product->subscription_trial_length . ' ' . $product->subscription_trial_period . 
            ' trial of the product, then an outright purchase of ' . wc_price( $product->subscription_price );
    }
    

    【讨论】:

      【解决方案3】:

      /public_html/wp-content/plugins/woocommerce-subscriptions/includes/class-wc-subscriptions-product.php

      查找get_price_string(...)

      在此处调整布尔值:

      $include = wp_parse_args( $include, array(
              'tax_calculation'     => get_option( 'woocommerce_tax_display_shop' ),
              'subscription_price'  => true,
              'subscription_period' => true,
              'subscription_length' => true,
              'sign_up_fee'         => true,
              'trial_length'        => true,
          )
      );
      

      【讨论】:

      • 直接修改插件文件是个坏主意。使用过滤器和挂钩。
      猜你喜欢
      • 2018-08-31
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 2016-04-27
      • 2023-03-31
      相关资源
      最近更新 更多