Customs code
The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday. If you have problems with registration ask help on contact us page pleaseIf you not got email within 24~36 business hours, firstly check your spam box, and if no any email from the support there - back to the forum and read answer here. DO NOT ANSWER ON EMAILS [noreply@pluginus.net] FROM THE FORUM!! Emails are just for your info, all answers should be published only here.
The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday.
Quote from DevPt on February 15, 2024, 17:42Hello,
I have created a custom code to have different choice of quantity and implant a shortcode on the product page to set different add to cart. The calculation of the amount in the cart page is : product_price*quantity*"a meta field".
My code work perfectly for the USD (base currency), but for EUR, secondary currency, it does not take the meta field in consideration.
Could you help to understand why the secondary currency can not work with our code :
// Add Shortcode
function custom_product_purchase_shortcode() {
global $product;
if (!$product->is_type('simple')) {
return; // Only simple products are supported
}
ob_start();
?>
<form class="custom-product-purchase-form" method="post">
<input type="hidden" name="add_to_cart" value="<?php echo esc_attr($product->get_id()); ?>">
<input type="hidden" name="product_id" value="<?php echo esc_attr($product->get_id()); ?>">
<p>
<label for="pallet_quantity">Pallet Quantity:</label>
<input type="number" id="pallet_quantity" name="pallet_quantity" value="1" min="1">
</p>
<p>
<button type="submit" class="button alt" name="pallet_submit">Add to Cart (Pallet)</button>
</p>
</form>
<form class="custom-product-purchase-form" method="post">
<input type="hidden" name="add_to_cart" value="<?php echo esc_attr($product->get_id()); ?>">
<input type="hidden" name="product_id" value="<?php echo esc_attr($product->get_id()); ?>">
<p>
<label for="container_20_quantity">Container 20' Quantity:</label>
<input type="number" id="container_20_quantity" name="container_20_quantity" value="1" min="1">
</p>
<p>
<button type="submit" class="button alt" name="container_20_submit">Add to Cart (Container 20')</button>
</p>
</form>
<form class="custom-product-purchase-form" method="post">
<input type="hidden" name="add_to_cart" value="<?php echo esc_attr($product->get_id()); ?>">
<input type="hidden" name="product_id" value="<?php echo esc_attr($product->get_id()); ?>">
<p>
<label for="container_40_quantity">Container 40' Quantity:</label>
<input type="number" id="container_40_quantity" name="container_40_quantity" value="1" min="1">
</p>
<p>
<button type="submit" class="button alt" name="container_40_submit">Add to Cart (Container 40')</button>
</p>
</form>
<?php
return ob_get_clean();
}
add_shortcode('custom_product_purchase', 'custom_product_purchase_shortcode');
// Function to handle form submission
function handle_custom_product_purchase_form() {
if (isset($_POST['add_to_cart'])) {
$product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['add_to_cart']));
$product = wc_get_product($product_id);
if ($product && $product->is_purchasable()) {
$quantity = 1;
$variation_id = 0;
$variation = array();
if (isset($_POST['pallet_quantity']) && isset($_POST['pallet_submit'])) {
$quantity = intval($_POST['pallet_quantity']);
$price_multiplier = get_post_meta($product_id, 'quantity_pallet', true);
} elseif (isset($_POST['container_20_quantity']) && isset($_POST['container_20_submit'])) {
$quantity = intval($_POST['container_20_quantity']);
$price_multiplier = get_post_meta($product_id, 'quantity_per_container', true);
} elseif (isset($_POST['container_40_quantity']) && isset($_POST['container_40_submit'])) {
$quantity = intval($_POST['container_40_quantity']);
$price_multiplier = get_post_meta($product_id, 'quantity_per_containers', true);
}
// Add to cart
WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variation, array('quantity_pallet' => $price_multiplier));
// Redirect to cart page
wp_redirect(wc_get_cart_url());
exit;
}
}
}
add_action('init', 'handle_custom_product_purchase_form');
// Adjust cart totals based on WCFM field value
function modify_cart_totals($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
$product_id = $product->get_id();
$quantity = $cart_item['quantity'];
$price_multiplier = 1; // Default value
if (isset($cart_item['quantity_pallet'])) {
$price_multiplier = $cart_item['quantity_pallet'];
} elseif (isset($cart_item['quantity_per_container'])) {
$price_multiplier = $cart_item['quantity_per_container'];
} elseif (isset($cart_item['quantity_per_containers'])) {
$price_multiplier = $cart_item['quantity_per_containers'];
}
// Get product price
$product_price = $product->get_price();
// Calculate total price for the item including the price multiplier
$total_price = $product_price * $quantity * $price_multiplier;
// Set total price to the cart item
$cart_item['data']->set_price($total_price);
}
}
add_action('woocommerce_before_calculate_totals', 'modify_cart_totals', 10, 1);
Hello,
I have created a custom code to have different choice of quantity and implant a shortcode on the product page to set different add to cart. The calculation of the amount in the cart page is : product_price*quantity*"a meta field".
My code work perfectly for the USD (base currency), but for EUR, secondary currency, it does not take the meta field in consideration.
Could you help to understand why the secondary currency can not work with our code :
// Add Shortcode
function custom_product_purchase_shortcode() {
global $product;
if (!$product->is_type('simple')) {
return; // Only simple products are supported
}
ob_start();
?>
<form class="custom-product-purchase-form" method="post">
<input type="hidden" name="add_to_cart" value="<?php echo esc_attr($product->get_id()); ?>">
<input type="hidden" name="product_id" value="<?php echo esc_attr($product->get_id()); ?>">
<p>
<label for="pallet_quantity">Pallet Quantity:</label>
<input type="number" id="pallet_quantity" name="pallet_quantity" value="1" min="1">
</p>
<p>
<button type="submit" class="button alt" name="pallet_submit">Add to Cart (Pallet)</button>
</p>
</form>
<form class="custom-product-purchase-form" method="post">
<input type="hidden" name="add_to_cart" value="<?php echo esc_attr($product->get_id()); ?>">
<input type="hidden" name="product_id" value="<?php echo esc_attr($product->get_id()); ?>">
<p>
<label for="container_20_quantity">Container 20' Quantity:</label>
<input type="number" id="container_20_quantity" name="container_20_quantity" value="1" min="1">
</p>
<p>
<button type="submit" class="button alt" name="container_20_submit">Add to Cart (Container 20')</button>
</p>
</form>
<form class="custom-product-purchase-form" method="post">
<input type="hidden" name="add_to_cart" value="<?php echo esc_attr($product->get_id()); ?>">
<input type="hidden" name="product_id" value="<?php echo esc_attr($product->get_id()); ?>">
<p>
<label for="container_40_quantity">Container 40' Quantity:</label>
<input type="number" id="container_40_quantity" name="container_40_quantity" value="1" min="1">
</p>
<p>
<button type="submit" class="button alt" name="container_40_submit">Add to Cart (Container 40')</button>
</p>
</form>
<?php
return ob_get_clean();
}
add_shortcode('custom_product_purchase', 'custom_product_purchase_shortcode');
// Function to handle form submission
function handle_custom_product_purchase_form() {
if (isset($_POST['add_to_cart'])) {
$product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['add_to_cart']));
$product = wc_get_product($product_id);
if ($product && $product->is_purchasable()) {
$quantity = 1;
$variation_id = 0;
$variation = array();
if (isset($_POST['pallet_quantity']) && isset($_POST['pallet_submit'])) {
$quantity = intval($_POST['pallet_quantity']);
$price_multiplier = get_post_meta($product_id, 'quantity_pallet', true);
} elseif (isset($_POST['container_20_quantity']) && isset($_POST['container_20_submit'])) {
$quantity = intval($_POST['container_20_quantity']);
$price_multiplier = get_post_meta($product_id, 'quantity_per_container', true);
} elseif (isset($_POST['container_40_quantity']) && isset($_POST['container_40_submit'])) {
$quantity = intval($_POST['container_40_quantity']);
$price_multiplier = get_post_meta($product_id, 'quantity_per_containers', true);
}
// Add to cart
WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variation, array('quantity_pallet' => $price_multiplier));
// Redirect to cart page
wp_redirect(wc_get_cart_url());
exit;
}
}
}
add_action('init', 'handle_custom_product_purchase_form');
// Adjust cart totals based on WCFM field value
function modify_cart_totals($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
$product_id = $product->get_id();
$quantity = $cart_item['quantity'];
$price_multiplier = 1; // Default value
if (isset($cart_item['quantity_pallet'])) {
$price_multiplier = $cart_item['quantity_pallet'];
} elseif (isset($cart_item['quantity_per_container'])) {
$price_multiplier = $cart_item['quantity_per_container'];
} elseif (isset($cart_item['quantity_per_containers'])) {
$price_multiplier = $cart_item['quantity_per_containers'];
}
// Get product price
$product_price = $product->get_price();
// Calculate total price for the item including the price multiplier
$total_price = $product_price * $quantity * $price_multiplier;
// Set total price to the cart item
$cart_item['data']->set_price($total_price);
}
}
add_action('woocommerce_before_calculate_totals', 'modify_cart_totals', 10, 1);
Quote from Pablo Borysenco on February 16, 2024, 10:52Hello
To convert a custom number, please use - https://currency-switcher.com/function/woocs-woocs_exchange_value
https://share.pluginus.net/image/i20240216104923.png - In this part of the code you should take into account that the price should be set only in the base currency. So if you counted amounts in another currency to the set_price function, you must pass the amount in the base currency. Example of conversion to base currency:
$currrent = $WOOCS->current_currency;
if ($currrent != $WOOCS->default_currency) {
$currencies = $WOOCS->get_currencies();
$rate = $currencies[$currrent]['rate'];
$your_any_price = $your_any_price / $rate;
}
Hello
To convert a custom number, please use - https://currency-switcher.com/function/woocs-woocs_exchange_value
https://share.pluginus.net/image/i20240216104923.png - In this part of the code you should take into account that the price should be set only in the base currency. So if you counted amounts in another currency to the set_price function, you must pass the amount in the base currency. Example of conversion to base currency:
$currrent = $WOOCS->current_currency;
if ($currrent != $WOOCS->default_currency) {
$currencies = $WOOCS->get_currencies();
$rate = $currencies[$currrent]['rate'];
$your_any_price = $your_any_price / $rate;
}
Quote from DevPt on February 16, 2024, 10:59Thank you very much Pablo.
The second currency (EUR) is already set on the product by the vendor, so just wondering if we need to try to convert the price or just pick up the price already set in EUR in backend by the vendor ?
Thank you very much Pablo.
The second currency (EUR) is already set on the product by the vendor, so just wondering if we need to try to convert the price or just pick up the price already set in EUR in backend by the vendor ?
Quote from Pablo Borysenco on February 16, 2024, 13:06All prices must be stored in the base currency
All prices must be stored in the base currency
Quote from DevPt on February 16, 2024, 16:29I tried like this, but it doesnt work :
// Adjust cart totals based on WCFM field value
function modify_cart_totals($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;global $WOOCS;
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
$product_id = $product->get_id();
$quantity = $cart_item['quantity'];// Check if the product has the custom field and it's not empty
if (isset($cart_item['quantity_pallet']) && !empty($cart_item['quantity_pallet'])) {
$price_multiplier = $cart_item['quantity_pallet'];
} else {
// If the custom field is empty, remove the item from the cart
$cart->remove_cart_item($cart_item_key);
continue; // Skip to the next cart item
}// Get product price
$product_price = $product->get_price();// Calculate total price for the item including the price multiplier
$total_price = $product_price * $quantity * $price_multiplier;// Convert total price to base currency if necessary
$current_currency = $WOOCS->current_currency;
if ($current_currency != $WOOCS->default_currency) {
$currencies = $WOOCS->get_currencies();
$rate = $currencies[$current_currency]['rate'];
$total_price = $total_price / $rate;
}// Set total price to the cart item
$cart_item['data']->set_price($total_price);
}
}add_action('woocommerce_before_calculate_totals', 'modify_cart_totals', 10, 1);
I tried like this, but it doesnt work :
// Adjust cart totals based on WCFM field value
function modify_cart_totals($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
global $WOOCS;
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
$product_id = $product->get_id();
$quantity = $cart_item['quantity'];
// Check if the product has the custom field and it's not empty
if (isset($cart_item['quantity_pallet']) && !empty($cart_item['quantity_pallet'])) {
$price_multiplier = $cart_item['quantity_pallet'];
} else {
// If the custom field is empty, remove the item from the cart
$cart->remove_cart_item($cart_item_key);
continue; // Skip to the next cart item
}
// Get product price
$product_price = $product->get_price();
// Calculate total price for the item including the price multiplier
$total_price = $product_price * $quantity * $price_multiplier;
// Convert total price to base currency if necessary
$current_currency = $WOOCS->current_currency;
if ($current_currency != $WOOCS->default_currency) {
$currencies = $WOOCS->get_currencies();
$rate = $currencies[$current_currency]['rate'];
$total_price = $total_price / $rate;
}
// Set total price to the cart item
$cart_item['data']->set_price($total_price);
}
}
add_action('woocommerce_before_calculate_totals', 'modify_cart_totals', 10, 1);
Quote from Pablo Borysenco on February 19, 2024, 10:59Hello
describe in more detail what does not work
Hello
describe in more detail what does not work
Quote from DevPt on February 19, 2024, 11:30Hello Pablo,
I see that the field value is not multiply with the unit price * quantity.
The rules of this code works like this (and its work for the base currency USD but not for secondary EUR) :
- The client add the predefined quantity (for example pallet), the number inside the pallet quantity is set in advance in a field named 'pallet_quantity'. So the calculation for the cart and checkout is : Unit Price * quantity * field (pallet_quantity). This is working perfectly for the USD
- For the EUR, we can see that the field is not taken in consideration in the cart and checkout page. Its only taken the Unit price*quantity and the the field that is the base value of the product quantity in 1 pallet. The unit price of the product in EUR is working, its well the one already set. But it seems woocs plugin, doesnt want to take the field that is necessary for our calculation
Hello Pablo,
I see that the field value is not multiply with the unit price * quantity.
The rules of this code works like this (and its work for the base currency USD but not for secondary EUR) :
- The client add the predefined quantity (for example pallet), the number inside the pallet quantity is set in advance in a field named 'pallet_quantity'. So the calculation for the cart and checkout is : Unit Price * quantity * field (pallet_quantity). This is working perfectly for the USD
- For the EUR, we can see that the field is not taken in consideration in the cart and checkout page. Its only taken the Unit price*quantity and the the field that is the base value of the product quantity in 1 pallet. The unit price of the product in EUR is working, its well the one already set. But it seems woocs plugin, doesnt want to take the field that is necessary for our calculation
Quote from Pablo Borysenco on February 19, 2024, 12:54do you use fixed prices?
do you use fixed prices?
Quote from Pablo Borysenco on February 20, 2024, 10:30Hello
Ok! Turn it off and do a test
The point is that the meaning of a fixed price is a price that does not change
Hello
Ok! Turn it off and do a test
The point is that the meaning of a fixed price is a price that does not change
Quote from DevPt on February 20, 2024, 11:31Hello Pablo,
I just tried to turn off the fix priced, effectively it's working. But the issue we need to define the EUR price in advance with the fixed price in our case.
"The point is that the meaning of a fixed price is a price that does not change" : when we multiply the unit price in our * quantity (of pallet) * field (number of unit on a pallet), in that case we do not change the fixed unit price, only the quantity ordered. The field is only giving a number that is not price changing, this is only a quantity that we will multiply to the unit price
Hello Pablo,
I just tried to turn off the fix priced, effectively it's working. But the issue we need to define the EUR price in advance with the fixed price in our case.
"The point is that the meaning of a fixed price is a price that does not change" : when we multiply the unit price in our * quantity (of pallet) * field (number of unit on a pallet), in that case we do not change the fixed unit price, only the quantity ordered. The field is only giving a number that is not price changing, this is only a quantity that we will multiply to the unit price
Quote from Pablo Borysenco on February 20, 2024, 13:12The only option is to use this hook - https://currency-switcher.com/hook/woocs_fixed_raw_woocommerce_price
The only option is to use this hook - https://currency-switcher.com/hook/woocs_fixed_raw_woocommerce_price
Quote from DevPt on February 21, 2024, 17:14Thanks, i'm trying since yesterday, but don't find the right way at the moment with this hook
Thanks, i'm trying since yesterday, but don't find the right way at the moment with this hook
Quote from Pablo Borysenco on February 22, 2024, 11:04Hello
This hook passes the fixed price and product object. If you determine the current position in the cart or checkout, you can change the fixed price
Hello
This hook passes the fixed price and product object. If you determine the current position in the cart or checkout, you can change the fixed price