以下代码基于 基于 Woocommerce 3 中 2 个自定义字段的产品常规价格计算答案代码:
// Adding and displaying additional product pricing custom fields
add_action( 'woocommerce_product_options_pricing', 'additional_product_pricing_option_fields', 50 );
function additional_product_pricing_option_fields() {
$domain = "woocommerce";
global $post;
echo '</div><div class="options_group pricing show_if_simple show_if_external show_if_composite">';
woocommerce_wp_text_input( array(
'id' => '_purchase_price',
'label' => __("Prix d'achat ($) (si supérieur à 0, il calcule le prix régulier de vente)", $domain ) . ' ('. get_woocommerce_currency_symbol() . ')',
'placeholder' => '',
'description' => __("Prix d'achat ($) (si supérieur à 0, il calcule le prix régulier de vente)", $domain ),
'desc_tip' => true,
) );
echo '<input type="hidden" name="_custom_price_nonce" value="' . wp_create_nonce() . '">';
}
// Utility function that save "Rate margin" and "Purchase_price" custom fields values
function saving_rate_margin_and_purchase_price( $product ) {
// Security check
if ( isset($_POST['_custom_price_nonce']) && ! wp_verify_nonce($_POST['_custom_price_nonce']) ) {
return;
}
// Save "Rate margin" and "Purchase_price" custom fields values
if(isset($_POST['_purchase_price']) ) {
$product->update_meta_data('_purchase_price', sanitize_text_field( (float) $_POST['_purchase_price'] ) );
}
}
// Utility function: Calculate and save product price from the "Rate margin" and the "Purchase price" custom fields
function calculate_and_save_new_product_price( $product ) {
// Disable when product is on sale
if( isset($_POST['_sale_price']) && $_POST['_sale_price'] > 0 ){
return;
}
// Calculate and save the new price
if(isset($_POST['_purchase_price'])
&& $_POST['_purchase_price'] > 0 ) {
// Catch the pricing data
$rate_margin = (float) get_option( 'dbi_example_plugin_options' )['pctrate'];
$purchase_price = (float) $_POST['_purchase_price'];
$active_price = (float) $product->get_price();
// Calculating new price
$new_price = (100-$rate_margin)/100 * $purchase_price;
// If the active price is different from the calculated new price
if( $new_price !== $active_price ) {
// Update regular price and active price with new calculated price
$product->set_price( $new_price );
update_post_meta($product->get_id(),'_subscription_price',$new_price);
$product->set_regular_price( $new_price );
}
}
}
// Saving and calculating prices
add_action( 'woocommerce_admin_process_product_object', 'update_product_meta_data' );
function update_product_meta_data( $product ) {
// Saving "Rate margin" and "Purchase_price" custom fields values
saving_rate_margin_and_purchase_price( $product ); // <== To be removed if not used with the first function
// Calculate and save product price from the "Rate margin" and the "Purchase price" custom fields
calculate_and_save_new_product_price( $product );
}
我希望该代码可以在 php 中的 WordPress 中编辑 woocommerce 中的订阅金额,但它仅适用于简单的产品,它不会影响订阅的经常性价格,为什么?
这是我正在开发的插件的代码,但它没有按预期工作,因为即使有更新后元功能,它也不会影响订阅的价格
尝试使用此代码:
// Adding and displaying additional product pricing custom fields
add_action('woocommerce_product_options_pricing', 'additional_product_pricing_option_fields', 50);
function additional_product_pricing_option_fields()
{
$domain = "woocommerce";
global $post;
echo '</div><div class="options_group pricing show_if_simple show_if_external show_if_composite">';
woocommerce_wp_text_input(array(
'id' => '_purchase_price',
'label' => __("Prix d'achat ($) (si supérieur à 0, il calcule le prix régulier de vente)", $domain) . ' (' . get_woocommerce_currency_symbol() . ')',
'placeholder' => '',
'value' => get_post_meta(get_the_ID(), '_purchase_price', true),
'description' => __("Prix d'achat ($) (si supérieur à 0, il calcule le prix régulier de vente)", $domain),
'desc_tip' => true,
));
echo '<input type="hidden" name="_custom_price_nonce" value="' . wp_create_nonce() . '">';
}
// Saving and calculating prices
add_action( 'save_post', 'update_product_meta_data', 12 );
function update_product_meta_data($post_id)
{
if ( empty( $_POST['_wcsnonce'] ) || ! wp_verify_nonce( $_POST['_wcsnonce'], 'wcs_subscription_meta' ) ) {
return;
}
$product = wc_get_product( $post_id );
// Saving "Rate margin" and "Purchase_price" custom fields values
saving_rate_margin_and_purchase_price($product); // <== To be removed if not used with the first function
// Calculate and save product price from the "Rate margin" and the "Purchase price" custom fields
calculate_and_save_new_product_price($product);
}
// Utility function that save "Rate margin" and "Purchase_price" custom fields values
function saving_rate_margin_and_purchase_price($product)
{
// Security check
if (isset($_POST['_custom_price_nonce']) && !wp_verify_nonce($_POST['_custom_price_nonce'])) {
return;
}
// Save "Rate margin" and "Purchase_price" custom fields values
if (isset($_POST['_purchase_price'])) {
$product->update_meta_data('_purchase_price', (float) $_POST['_purchase_price']);
}
}
// Utility function: Calculate and save product price from the "Rate margin" and the "Purchase price" custom fields
function calculate_and_save_new_product_price($product)
{
// Disable when product is on sale
if (isset($_POST['_sale_price']) && $_POST['_sale_price'] > 0) {
return;
}
// Calculate and save the new price
if (
isset($_POST['_purchase_price'])
&& $_POST['_purchase_price'] > 0
) {
// Catch the pricing data
$rate_margin = (float) isset(get_option( 'dbi_example_plugin_options' )['pctrate'])?get_option( 'dbi_example_plugin_options' )['pctrate']:10;
$purchase_price = (float) $_POST['_purchase_price'];
$active_price = (float) $product->get_price();
// Calculating new price
$new_price = (100-$rate_margin)/100 * $purchase_price;
// If the active price is different from the calculated new price
if( $new_price !== $active_price ) {
// Update regular price and active price with new calculated price
$product->set_regular_price( $new_price );
$product->set_price( $new_price );
$product->update_meta_data('_subscription_price', wc_format_decimal($new_price));
$product->save();
}
}
}