WooCommerce 回显模式的美元价格

问题描述 投票:0回答:1

我所有的产品均以美元标价。但对于美国境外的访问者,价格会发生变化(我使用的是 WooCommerce 多币种)。当价格发生变化时,架构中的价格也会发生变化。

那么,我怎样才能回显美元价格呢?

示例代码:

        "offers": [{
        "@type": "Offer",
        "availability": "http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>",
        "price": "<?php echo $product->get_price(); ?>",
        "priceValidUntil": "<?php echo date('Y', strtotime('+1 year')); ?>-11-05",
        "priceCurrency": "<?php echo get_woocommerce_currency(); ?>",
        "url": "<?php echo get_permalink( $product->get_id() ); ?>"
    },{
        "@type": "Offer",
        "availability": "http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>",
        "price": "<?php $product->get_price_html(); ?>",
        "priceValidUntil": "<?php echo date('Y', strtotime('+1 year')); ?>-11-05",
        "priceCurrency": "USD",
        "url": "<?php echo get_permalink( $product->get_id() ); ?>"
    }],

“price”:“get_price_html(); ?>”是我想要的美元价格(第二次报价)。

php wordpress woocommerce hook-woocommerce
1个回答
0
投票
add_filter('woocommerce_get_price_html', 'change_currency_for_specific_product', 100, 2);

function change_currency_for_specific_product($price, $product) {
    if (1631818630 == $product->get_id()) { // Change the product ID
        add_filter('woocommerce_currency', function () {
            return 'INR';
        }, PHP_INT_MAX);
        if ($product->is_on_sale()) {
            $price = wc_format_sale_price(wc_get_price_to_display($product, array('price' => $product->get_regular_price())), wc_get_price_to_display($product)) . $product->get_price_suffix();
        } else {
            $price = wc_price(wc_get_price_to_display($product)) . $product->get_price_suffix();
        }
    }
    return $price;
}
© www.soinside.com 2019 - 2024. All rights reserved.