【问题标题】:Add price suffix ro Woocommerce shop loop based on country根据国家/地区添加价格后缀 ro Woocommerce 商店循环
【发布时间】:2021-03-05 13:46:58
【问题描述】:

我需要在 Woocommerce 商店循环中显示价格后缀,但不是针对三个特定国家/地区。因此,例如,如果来自瑞士或挪威的客户正在访问该网站,则不应显示后缀。但对于所有其他国家来说,它应该是。之后我只尝试使用带有伪元素的css,但这会导致后缀总是被显示。

【问题讨论】:

  • 您如何识别用户来自哪个国家/地区?如果您有 php 函数来执行此操作,则可以覆盖 single-product/price.php 中 price.php 的模板,而不是退出 $product->get_price_html();让这些特定国家/地区退出 $product->get_price();这没有任何 html(后缀等),您可以随意修改它。
  • 当用户使用购物车页面上的运费计算器时选择国家。之前设置的默认值是商店基地。

标签: php wordpress woocommerce


【解决方案1】:

您可以尝试以下方法之一。方法是一样的,从 shipping_country 函数中找出国家并相应地修改您的定价。 get_shipping_country 可能会返回一个字符串。然后你需要修改 if 函数。

1:修改模板/loop/price.php以及你的childtheme或main中的/single-product/price.php

global $woocommerce;
$countries = array('NR', 'CH');
   
if ( in_array( $woocommerce->customer->get_shipping_country(), $countries ) ) :
   
// not sure about the country codes. You should double check that.
// just call your basic price function. Which you can customize yourself or use of the other pricing functions.
echo $product->get_price();
: else ?>
<p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>"><?php echo $product->get_price_html(); ?></p>
<?php endif;

2:在插件或子主题的functions.php中为price_html添加过滤器

add_filter( 'woocommerce_get_price_html', 'adjust_price_html', 100, 2 );




// callback function to adjust price based on country
function adjust_price_html( $price, $product ){
    global $woocommerce_loop;

    $wLoop = true;

// for related loop
if ( is_product() && $woocommerce_loop['name'] == 'related' ) :
    display_price_html_based_on_country($price);
elseif (is_shop() || is_product_category()) :

    display_price_html_based_on_country($price);
else :

    // this is single_product
    if(is_product()){
        return $price;
    } else {

        // here you can modify shortcode output

        display_price_html_based_on_country($price);
    }

    endif;
    

}

function display_price_html_based_on_country($price){
    global $woocommerce;
    $countries = array('NR', 'CH');

    if ( in_array( $woocommerce->customer->get_shipping_country(), $countries ) ) :


    // here you can play with the price you get
    // or call other data from the $product object. For example 
    $product->get_price();
    return $price;
    endif;

}

【讨论】:

  • 我对 PHP 的了解不多。我推荐的方法是使用过滤器,但我只需要存档页面上的价格有后缀,而不是单个产品的价格。
  • 问题是 is_shop 不包括相关产品、追加销售或使用产品短代码的任何地方。我需要产品在循环中显示的任何地方(所以在网格中)的后缀,但不是在单个产品上。
  • 好吧。这也没有问题。将调整条件逻辑。也许你下次应该在描述中提及这些细节。
猜你喜欢
  • 2016-09-24
  • 1970-01-01
  • 2019-05-04
  • 2014-09-22
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多