Php 添加基于国家/地区的商业商店循环的价格后缀

Php 添加基于国家/地区的商业商店循环的价格后缀,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我需要在Woocommerce商店循环中显示价格后缀,但不是针对三个特定国家。因此,例如,如果来自瑞士或挪威的客户访问该站点,则不应显示后缀。但对所有其他国家来说,这应该是。我只尝试将css与pseudoelement after一起使用,但这会导致始终显示后缀。您可以尝试以下方法之一。方法是相同的,从shipping_country功能中找出国家,并相应地修改您的定价。get_shipping_country可能会返回一个字符串。然后需要修改if函数 1:修改子主题或主主题中的模板/loop/

我需要在Woocommerce商店循环中显示价格后缀,但不是针对三个特定国家。因此,例如,如果来自瑞士或挪威的客户访问该站点,则不应显示后缀。但对所有其他国家来说,这应该是。我只尝试将css与pseudoelement after一起使用,但这会导致始终显示后缀。

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

1:修改子主题或主主题中的模板/loop/price.php以及/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;

您如何确定用户来自哪个国家?如果您有php函数来做这件事,那么您可以在single product/price.php中覆盖price.php的模板,而不是使用$product->get_price_html();对于这些特定的国家,只需拿出$product->get_price();这没有任何html(后缀等),您可以随意修改。当用户使用购物车页面上的配送计算器时,将选择国家/地区。之前设置的默认值是shop base。我对PHP没有那么多知识。我的最佳方法是使用过滤器,但我只需要存档页面上的价格有后缀,而不是单一产品的价格。问题是,商店不包括相关产品、追加销售或使用产品快捷码的任何地方。我需要在产品在循环中显示的任何地方都使用后缀(因此在网格中),但不需要在单个poduct pice.AH ok。那也没问题。将调整条件逻辑。也许下次你应该在描述中提到这些细节。
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;

}