I'm using a plugin "WooCommerce Product Fees" to add custom fees to products. I want to set these custom fields to be displayed only for one country.
* 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;
}
What I want to accomplish is that this field must show only if the user is from one country, so on product page must not display for a country either on cart and checkout. Is there a filter that can I add to exclude this field from one country? Thank you in advance!!!
** UPDATE**
I'm trying to dispaly the translation for the "$fee_name" in curent language that the user is using. Now my site is in greek and english and I'm using wpml but this field isn't translating. I was thinking if is a way to add the ICL_LANGUAGE_CODE and str_replace somehow so if the user is using the site in english to see the "$fee_name" translated.( but it brokes the site ). This is my code:
$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;
}
I really appreciate any help!!
Using the following in your first function:
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;
}
It should work.