Настраиваемые поля (высота, ширина, множитель) сохраняются в базе данных. Я хочу рассчитать цену на основе настраиваемых полей и сохранить цену в базе данных как обычная цена.
Проблема в том, что цена не сохраняется.
Вот мой код из файла function.php в моей дочерней теме:
/>
/* CUSTOM FIELDS: Add custom fields to Product/General pane in Woocommerce
from http://www.remicorson.com/mastering-woocommerce-products-custom-fields/ */
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save');
function woo_add_custom_general_fields()
{
global $woocommerce, $post;
echo '';
// Product Height
woocommerce_wp_text_input(
array(
'id' => '_product_height',
'label' => __('Product Height (inches)', 'woocommerce'),
'placeholder' => '',
'description' => __('Enter the product height in inches here.', 'woocommerce'),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
// Product Width
woocommerce_wp_text_input(
array(
'id' => '_product_width',
'label' => __('Product Width (inches)', 'woocommerce'),
'placeholder' => '',
'description' => __('Enter the product width in inches here.', 'woocommerce'),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
// Product Area Multiplier
woocommerce_wp_text_input(
array(
'id' => '_product_area_mult',
'label' => __('Product Area Multiplier', 'woocommerce'),
'placeholder' => '',
'description' => __('Enter Product Area Multiplier. ', 'woocommerce'),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
// Product Area Price: added this field to check if this value is being calculated
woocommerce_wp_text_input(
array(
'id' => '_product_area_price',
'label' => __('Product Area Price', 'woocommerce'),
'placeholder' => '',
'description' => __('Product Area Price. Calculated automatically', 'woocommerce'),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
echo '';
}
function woo_add_custom_general_fields_save($post_id)
{
$woocommerce_product_height = $_POST['_product_height'];
$woocommerce_product_width = $_POST['_product_width'];
$woocommerce_product_area_mult = $_POST['_product_area_mult'];
$woocommerce_product_area_price = $_POST['_product_area_price'];
// save height, width, multiplier
if (!empty($woocommerce_product_height))
update_post_meta($post_id, '_product_height', esc_attr($woocommerce_product_height));
if (!empty($woocommerce_product_width))
update_post_meta($post_id, '_product_width', esc_attr($woocommerce_product_width));
if (!empty($woocommerce_product_area_mult))
update_post_meta($post_id, '_product_area_mult', esc_attr($woocommerce_product_area_mult));
// calculate and save _product_area_price, _regular_price, price as Height*Width*Mult
if (!empty($woocommerce_product_height) && !empty($woocommerce_product_width) && !empty($woocommerce_product_area_mult))
$woocommerce_product_area_price = $woocommerce_product_height * $woocommerce_product_width * $woocommerce_product_area_mult;
if (!empty($woocommerce_product_area_price))
update_post_meta($post_id, '_product_area_price', esc_attr($woocommerce_product_area_price));
if (!empty($woocommerce_product_area_price))
{
update_post_meta($post_id, '_regular_price', esc_attr($woocommerce_product_area_price));
update_post_meta($post_id, '_price', esc_attr($woocommerce_product_area_price));
}
}
Все работает, кроме обновления полей цен в базе данных. Мои настраиваемые поля обновляются с использованием того же синтаксиса. Я подтвердил, что переменная $woocommerce_product_area_price существует и обновляет настраиваемое поле, но эта же переменная не обновляет поля _regular_price или _price. Таким образом, эти строки не работают, хотя та же переменная будет обновлять поле _product_area_price:
Я добавил настраиваемые поля на вкладку Woocommerce/Добавить продукт/Общие, используя этот замечательный учебник http://www.remicorson.com/mastering-woocommerce-products-custom-fields/
Настраиваемые поля (высота, ширина, множитель) сохраняются в базе данных. [b]Я хочу рассчитать цену на основе настраиваемых полей и сохранить цену в базе данных как обычная цена[/b]. Проблема в том, что цена не сохраняется.
Вот мой код из файла function.php в моей дочерней теме: /> [code]/* CUSTOM FIELDS: Add custom fields to Product/General pane in Woocommerce from http://www.remicorson.com/mastering-woocommerce-products-custom-fields/ */
// Display Fields add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields'); // Save Fields add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save');
function woo_add_custom_general_fields() { global $woocommerce, $post; echo '';
// Product Area Price: added this field to check if this value is being calculated woocommerce_wp_text_input( array( 'id' => '_product_area_price', 'label' => __('Product Area Price', 'woocommerce'), 'placeholder' => '', 'description' => __('Product Area Price. Calculated automatically', 'woocommerce'), 'type' => 'number', 'custom_attributes' => array( 'step' => 'any', 'min' => '0' ) ) );
// save height, width, multiplier if (!empty($woocommerce_product_height)) update_post_meta($post_id, '_product_height', esc_attr($woocommerce_product_height));
if (!empty($woocommerce_product_width)) update_post_meta($post_id, '_product_width', esc_attr($woocommerce_product_width));
if (!empty($woocommerce_product_area_mult)) update_post_meta($post_id, '_product_area_mult', esc_attr($woocommerce_product_area_mult));
// calculate and save _product_area_price, _regular_price, price as Height*Width*Mult if (!empty($woocommerce_product_height) && !empty($woocommerce_product_width) && !empty($woocommerce_product_area_mult)) $woocommerce_product_area_price = $woocommerce_product_height * $woocommerce_product_width * $woocommerce_product_area_mult;
if (!empty($woocommerce_product_area_price)) update_post_meta($post_id, '_product_area_price', esc_attr($woocommerce_product_area_price));
Все работает, кроме обновления полей цен в базе данных. Мои настраиваемые поля обновляются с использованием того же синтаксиса. Я подтвердил, что переменная $woocommerce_product_area_price существует и обновляет настраиваемое поле, но эта же переменная не обновляет поля _regular_price или _price. Таким образом, эти строки не работают, хотя та же переменная будет обновлять поле _product_area_price: