我需要根据某些自定义字段的值更新产品价格
这是我如何构建和保存自定义字段,最后我尝试根据海关字段的值更新_regular_price,当我保存,更新,发布产品但没有发生什么,是什么我失踪了?
所有代码现在都进入function.php
//BUILD AND SAVE FIELDS
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Product URL
woocommerce_wp_checkbox(
array(
'label' => __('Product URL', 'woocommerce' ),
$post_id = $_GET['post'],
$produrl = get_post_meta( $post_id, 'productOriginalUrl', true),
'description' => '<a href="' . $produrl . '" class="button button-primary button-large" target="_blank">' . __('View Product', 'woocommerce') . '</a>',
)
);
// Number Field
woocommerce_wp_text_input(
array(
'id' => '_price_usd',
'label' => __( 'Price in USD', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
woocommerce_wp_text_input(
array(
'id' => '_price_tax',
'label' => __( 'TAX', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
woocommerce_wp_text_input(
array(
'id' => '_price_shipping',
'label' => __( 'Shipping', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
// Select
woocommerce_wp_select(
array(
'id' => '_shipping_weight',
'label' => __( 'Shipping Weight', 'woocommerce' ),
'options' => array(
'3.75' => __( 'Less Than 0.5 KG', 'woocommerce' ),
'7.5' => __( '0.5 KG', 'woocommerce' ),
'15' => __( '1 KG', 'woocommerce' )
)
)
);
// Hidden field
woocommerce_wp_hidden_input(
array(
'id' => '_usd_rate',
'value' => '18'
)
);
echo '</div>';
};
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
// Number Field
$woo_price_usd = $_POST['_price_usd'];
if( !empty( $woo_price_usd ) )
update_post_meta( $post_id, '_price_usd', esc_attr( $woo_price_usd ) );
$woo_price_tax = $_POST['_price_tax'];
if( !empty( $woo_price_tax ) )
update_post_meta( $post_id, '_price_tax', esc_attr( $woo_price_tax ) );
$woo_price_shipping = $_POST['_price_shipping'];
if( !empty( $woo_price_shipping ) )
update_post_meta( $post_id, '_price_shipping', esc_attr( $woo_price_shipping ) );
// Select
$woo_shipping_weight = $_POST['_shipping_weight'];
if( !empty( $woo_shipping_weight ) )
update_post_meta( $post_id, '_shipping_weight', esc_attr( $woo_shipping_weight ) );
// Hidden Field
$woo_usd_rate = $_POST['_usd_rate'];
if( !empty( $woo_usd_rate ) )
update_post_meta( $post_id, '_usd_rate', esc_attr( $woo_usd_rate ) );
}
//UPDATE PRICE REGULAR
function wpa104760_default_price( $post_id, $post ) {
if ( isset( $_POST['_regular_price'] )) {
$post_id = $_GET['post'];
$woo_price_usd = get_post_meta($post_id, '_price_usd', true);
$woo_price_tax = get_post_meta($post_id, '_price_tax', true);
$woo_price_shipping = get_post_meta($post_id, '_price_shipping', true);
$woo_shipping_weight = get_post_meta($post_id, '_shipping_weight', true);
$woo_usd_rate = get_post_meta($post_id, '_usd_rate', true);
$woo_new_product_price = (($woo_price_usd + (($woo_price_usd*$woo_price_tax)/100) + $woo_price_shipping + $woo_shipping_weight) * $woo_usd_rate );
update_post_meta( $post_id, '_regular_price', $woo_new_product_price );
}
}
add_action( 'woocommerce_process_product_meta', 'wpa104760_default_price' );
答案 0 :(得分:0)
答案很简单,
它需要一个过滤器来更新_regular_price和_sale_price
//price new
function return_custom_price($price, $product) {
global $post;
$new_total = get_post_meta( $post->ID, '_total_egp', true);
$price = get_post_meta($post->ID, '_regular_price', true);
$sale_price = get_post_meta($post->ID, '_sale_price', true);
$now_price = get_post_meta($post->ID, '_price', true);
if($price != 0 ){
update_post_meta($post->ID, '_regular_price', $new_total);
update_post_meta($post->ID, '_sale_price', $new_total);
update_post_meta($post->ID, '_price', $new_total);
return $price;
}
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);