woocommerce 添加来自元的自定义费用,并通过来自重力形式的元值乘以倍数
woocommerce add custom fee from meta and multiple by meta value coming from gravity forms
好的,到目前为止,这是我的代码,我可以通过自定义字段在每个产品中设置自定义费用,购物车中的所有内容都正确显示。现在我需要能够将它乘以来自重力表单插件的下拉字段的值。我有 woocommerce 重力形式插件。
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge');
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
//Loop through the cart to find out the extra costs
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
//Get the product info
$_product = $values['data'];
//Get the custom field value
$custom_fee = get_post_meta($_product->id, 'taxes_fees', true);
//Adding together the extra costs
$extra_custom_fee = $extra_custom_fee + $custom_fee;
}
//Lets check if we actually have a fee, then add it
if ($extra_custom_fee) {$woocommerce->cart->add_fee( 'Taxes & Fees', $extra_custom_fee, true, 'standard' )* $field["choices"][0]["value"];
}}
简单示例
$fee="20";
adult=2
child=2
$var1=adult+child=4
$fee*$var1="80"
好的,我从朋友那里得到了答案,我会 post 把它放在这里,以防有一天有人需要它。我的网站是一个 activity 网站,我有自定义字段,我在其中输入费用,我有带有 "inputname" 的重力表单字段,这是 css 和动态名称。我得到这些名称(来自重力的api)并得到价值,分解所有东西并取数字并将其乘以每个产品的费用。
add_action('woocommerce_cart_calculate_fees','woocommerce_custom_surcharge');
function woocommerce_custom_surcharge()
{
global $woocommerce;
if (is_admin() && !defined('DOING_AJAX')) return;
$extra_custom_fee = 0;
//Loop through the cart to find out the extra costs
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
//Get the product info
$_product = $values['data'];
// Find ID's for Adults and Kids charge in Gravity Form
$form = GFAPI::get_form($values['_gravity_form_data']['id']);
$total_adults = 0;
$total_kids = 0;
foreach ($form['fields'] as $field) {
if ($field['inputName'] == 'adultsprice' &&
!empty($values['_gravity_form_lead'][$field['id']])) {
$parts = explode('|', $values['_gravity_form_lead'][$field['id']]);
if (count($parts) == 2) {
$total_adults = intval($parts[0]);
$total_adult_charge = doubleval($parts[1]);
}
}
else if ($field['inputName'] == 'kidsprice' &&
!empty($values['_gravity_form_lead'][$field['id']])) {
$parts = explode('|', $values['_gravity_form_lead'][$field['id']]);
if (count($parts) == 2) {
$total_kids = intval($parts[0]);
$total_kids_charge = doubleval($parts[1]);
}
}
}
//Get the custom field value
$custom_fee = intval(get_post_meta($_product->id, 'taxes_fees', true));
//Adding together the extra costs
$extra_custom_fee += $custom_fee * ($total_adults + $total_kids);
}
//Lets check if we actually have a fee, then add it
if ($extra_custom_fee > 0) {
$woocommerce->cart->add_fee('Taxes & Fees', $extra_custom_fee, true, 'standard');
}
}
好的,到目前为止,这是我的代码,我可以通过自定义字段在每个产品中设置自定义费用,购物车中的所有内容都正确显示。现在我需要能够将它乘以来自重力表单插件的下拉字段的值。我有 woocommerce 重力形式插件。
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge');
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
//Loop through the cart to find out the extra costs
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
//Get the product info
$_product = $values['data'];
//Get the custom field value
$custom_fee = get_post_meta($_product->id, 'taxes_fees', true);
//Adding together the extra costs
$extra_custom_fee = $extra_custom_fee + $custom_fee;
}
//Lets check if we actually have a fee, then add it
if ($extra_custom_fee) {$woocommerce->cart->add_fee( 'Taxes & Fees', $extra_custom_fee, true, 'standard' )* $field["choices"][0]["value"];
}}
简单示例
$fee="20";
adult=2
child=2
$var1=adult+child=4
$fee*$var1="80"
好的,我从朋友那里得到了答案,我会 post 把它放在这里,以防有一天有人需要它。我的网站是一个 activity 网站,我有自定义字段,我在其中输入费用,我有带有 "inputname" 的重力表单字段,这是 css 和动态名称。我得到这些名称(来自重力的api)并得到价值,分解所有东西并取数字并将其乘以每个产品的费用。
add_action('woocommerce_cart_calculate_fees','woocommerce_custom_surcharge');
function woocommerce_custom_surcharge()
{
global $woocommerce;
if (is_admin() && !defined('DOING_AJAX')) return;
$extra_custom_fee = 0;
//Loop through the cart to find out the extra costs
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
//Get the product info
$_product = $values['data'];
// Find ID's for Adults and Kids charge in Gravity Form
$form = GFAPI::get_form($values['_gravity_form_data']['id']);
$total_adults = 0;
$total_kids = 0;
foreach ($form['fields'] as $field) {
if ($field['inputName'] == 'adultsprice' &&
!empty($values['_gravity_form_lead'][$field['id']])) {
$parts = explode('|', $values['_gravity_form_lead'][$field['id']]);
if (count($parts) == 2) {
$total_adults = intval($parts[0]);
$total_adult_charge = doubleval($parts[1]);
}
}
else if ($field['inputName'] == 'kidsprice' &&
!empty($values['_gravity_form_lead'][$field['id']])) {
$parts = explode('|', $values['_gravity_form_lead'][$field['id']]);
if (count($parts) == 2) {
$total_kids = intval($parts[0]);
$total_kids_charge = doubleval($parts[1]);
}
}
}
//Get the custom field value
$custom_fee = intval(get_post_meta($_product->id, 'taxes_fees', true));
//Adding together the extra costs
$extra_custom_fee += $custom_fee * ($total_adults + $total_kids);
}
//Lets check if we actually have a fee, then add it
if ($extra_custom_fee > 0) {
$woocommerce->cart->add_fee('Taxes & Fees', $extra_custom_fee, true, 'standard');
}
}