Nor*_*orv 6 php wordpress woocommerce price hook-woocommerce
我已经使用这个优秀的教程将自定义字段添加到Woocommerce/Add Product/General选项卡http://www.remicorson.com/mastering-woocommerce-products-custom-fields/
自定义字段(高度,宽度,乘数)正在保存到数据库中.
我想根据自定义字段计算价格,并将价格作为常规价格保存到数据库中.问题是价格没有得到保存.
这是我的代码,来自我的子主题中的functions.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 '<div class="options_group">';
// 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 '</div>';
}
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));
}
}
Run Code Online (Sandbox Code Playgroud)
除了更新数据库中的价格字段外,一切正常.我的自定义字段使用相同的语法更新.我确认该变量$woocommerce_product_area_price存在,并且它正在更新自定义字段,但相同的变量不是更新_regular_price或_price字段.所以这些行不起作用,即使相同的变量将更新该_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));
}
Run Code Online (Sandbox Code Playgroud)
也许有一些语法或检查我缺少更新Woocommerce的价格?
我认为您对前端显示的价格有疑问。_price如果你想_regular_price在前端反映,你需要更新密钥。
试试这个代码:
if (!empty($area_price)){
update_post_meta($post_id, '_regular_price', esc_attr($area_price));
update_post_meta($post_id, '_price', esc_attr($area_price)); //<-- Add this line.
}
Run Code Online (Sandbox Code Playgroud)
更新
只需为操作挂钩添加更高的优先级woocommerce_process_product_meta,就可以了。
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save', 99);
Run Code Online (Sandbox Code Playgroud)
我已经测试过它并且工作正常。
希望这可以帮助!