php - 如何设置仅针对用户当前使用的语言的国家/地区显示的产品费用

标签 php wordpress woocommerce cart country

我正在使用插件“WooCommerce 产品费用”向产品添加自定义费用。我想将这些自定义字段设置为仅针对一个国家/地区显示。

     * Check if a product contains fee data.
     *
     * @param int $id Product ID.
     * @return bool True or false based on existance of custom meta.
     */
    public function product_contains_fee_data( $id ) {
        $fee_name   = get_post_meta( $id, 'product-fee-name', true );
        $fee_amount = get_post_meta( $id, 'product-fee-amount', true );

        if ( '' !== $fee_name && '' !== $fee_amount && $fee_amount > 0 ) {
            return true;
        }

        return false;
    }

/////////////////**
     * Get all the fees.
     *
     * @param object $cart WC Cart object.
     * @return array $fees An array of fees to be added.
     */
    public function get_fees( $cart ) {
        $fees = array();

        if ( $this->maybe_remove_fees_for_coupon( $cart ) ) {
            return $fees;
        }

        foreach( $cart->get_cart() as $cart_item => $item ) {

            // Get the data we need from each product in the cart.
            $item_data = array(
                'id'           => $item['data']->get_id(),
                'variation_id' => $item['variation_id'],
                'parent_id'    => $item['data']->get_parent_id(),
                'qty'          => $item['quantity'],
                'price'        => $item['data']->get_price()
            );

            $fee = $this->get_fee_data( $item_data );

            if ( $fee ) {
                $fee_id        = strtolower( $fee['name'] );
                $fee_tax_class = $this->get_fee_tax_class( $item['data'] );

                if ( array_key_exists( $fee_id, $fees ) && 'combine' === get_option( 'wcpf_name_conflicts', 'combine' ) ) {
                    $fees[$fee_id]['amount'] += $fee['amount'];
                } else {
                    $fees[$fee_id] = apply_filters( 'wcpf_filter_fee_data', array(
                        'name' => $fee['name'],
                        'amount' => $fee['amount'],
                        'taxable' => ( '_no_tax' === $fee_tax_class ) ? false : true,
                        'tax_class' => $fee_tax_class
                    ), $item_data );
                }
            }
        }

        return $fees;
    }

我想要完成的是,此字段必须仅在用户来自一个国家/地区时显示,因此产品页面上不得在购物车和结帐时显示某个国家/地区。是否可以添加过滤器以将该字段从一个国家/地区中排除? 预先感谢您!!!

** 更新**

我正在尝试以用户正在使用的当前语言显示“$fee_name”的翻译。现在我的网站有希腊语和英语版本,我使用的是 wpml,但该字段没有翻译。我在想是否可以以某种方式添加 ICL_LANGUAGE_CODE 和 str_replace ,这样如果用户使用英语网站来查看翻译后的“$fee_name”。(但它破坏了网站)。 这是我的代码:

        $fee_name   = get_post_meta( $id, 'product-fee-name', true );
        $fee_amount = get_post_meta( $id, 'product-fee-amount', true );
        $fee_name_en = str_replace('product-fee-name', 'Recycling   Tax', $fee_name); 
        $country_code = 'GR'; // <=== Set the country code
        $user_country = WC()->customer->get_shipping_country(); // or with get_billing_country(); method too.

        if( '' !== $fee_name_en && '' !== $fee_amount && $fee_amount > 0  && $user_country === $country_code && ICL_LANGUAGE_CODE=='en') {

            return true;}


        else ( '' !== $fee_name && '' !== $fee_amount && $fee_amount > 0  && $user_country === $country_code && ICL_LANGUAGE_CODE=='el') {

            return true;

            }



        return false;
    }

我真的很感谢任何帮助!

最佳答案

在您的第一个函数中使用以下内容:

public function product_contains_fee_data( $id ) {
    $fee_name   = get_post_meta( $id, 'product-fee-name', true );
    $fee_amount = get_post_meta( $id, 'product-fee-amount', true );

    $country_code = 'FR'; // <=== Set the country code
    $user_country = WC()->customer->get_shipping_country(); // or with get_billing_country(); method too.

    if ( '' !== $fee_name && '' !== $fee_amount && $fee_amount > 0  && $user_country === $country_code ) {
        return true;
    }

    return false;
}

应该可以。

关于php - 如何设置仅针对用户当前使用的语言的国家/地区显示的产品费用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55746041/

相关文章:

php - 如何对 MYSQL 进行多次插入,每次插入都基于子查询中一组值中的每个值?

php - Woocommerce create_category Hook 不适用于产品类别

php - 如何找出接收到的udp数据包的数据大小

php - file_get_contents() 返回文字 'Not Found' 而不是 bool 值

php - 无法在外部 dbgetaddrinfo 上连接 Wordpress 失败 : Name or service not known

javascript - 按类名获取元素并更改类名

css - 事件时在导航栏下划线

php - woocommerce 将自定义帖子类型添加到购物车

php - 在 PHP 中使用 URL 突出显示当前导航选项卡

php - 跨源请求只支持协议(protocol)方案,怎么办?