在 wooCommerce 中,我在编辑产品页面设置中添加了一个带有自定义价格(批发价格)的自定义元字段。一切都运转良好。
当我设定批发价时,它在任何地方都运行良好。但如果我尝试改变这个批发价格,那就行不通了。到处都是旧价格。
我做错了什么?我该如何解决这个问题以允许批发价格变化?
我正在使用的代码:
function w4dev_get_wholesale_price( $product )
{
if(
$product->is_type( array('simple', 'variable') )
&& get_post_meta( $product->id, '_wholesale_price', true ) > 0
){
return get_post_meta( $product->id, '_wholesale_price', true );
}
elseif(
$product->is_type('variation')
&& get_post_meta( $product->variation_id, '_wholesale_price', true ) > 0
){
return get_post_meta( $product->variation_id, '_wholesale_price', true );
}
return 0;
}
add_action( 'woocommerce_product_options_pricing', 'w4dev_woocommerce_product_options_pricing' );
function w4dev_woocommerce_product_options_pricing()
{
woocommerce_wp_text_input( array(
'id' => '_wholesale_price',
'class' => 'wc_input_wholesale_price short',
'label' => __( 'Wholesale Price', 'woocommerce' ) . ' ('.get_woocommerce_currency_symbol().')',
'type' => 'text'
));
}
add_action( 'woocommerce_process_product_meta_simple', 'w4dev_woocommerce_process_product_meta_simple', 10, 1 );
function w4dev_woocommerce_process_product_meta_simple( $product_id )
{
if( isset($_POST['_wholesale_price']) && $_POST['_wholesale_price'] > 0 ){
update_post_meta( $product_id, '_wholesale_price', $_POST['_wholesale_price'] );
}
}
add_filter( 'woocommerce_get_price', 'w4dev_woocommerce_get_price', 10, 2);
function w4dev_woocommerce_get_price( $price, $product )
{
if( w4dev_get_wholesale_price($product) > 0 ) {
$price = w4dev_get_wholesale_price($product);
return $price;
}
}
您的代码中存在一些错误,并且还缺少很多部分:
woocommerce_get_price
已弃用且过时您重新访问的代码:
// Add "Wholesale Price" custom field to Products option pricing
add_action( 'woocommerce_product_options_pricing', 'w4dev_add_product_options_pricing' );
function w4dev_add_product_options_pricing()
{
woocommerce_wp_text_input( array(
'id' => '_wholesale_price',
'class' => 'wc_input_wholesale_price short',
'label' => __( 'Wholesale Price', 'woocommerce' ) . ' ('.get_woocommerce_currency_symbol().')',
'type' => 'text'
));
}
// Add custom field to VARIATIONS option pricing
add_action( 'woocommerce_variation_options_pricing', 'w4dev_add_variation_options_pricing', 20, 3 );
function w4dev_add_variation_options_pricing( $loop, $variation_data, $post_variation )
{
$value = get_post_meta( $post_variation->ID, '_wholesale_price', true );
$symbol = ' (' . get_woocommerce_currency_symbol() . ')';
$key = 'wholesale_price[' . $loop . ']';
echo '<div class="variable_wholesale-price"><p class="form-row form-row-first">
<label>' . __( "Wholesale Price", "woocommerce" ) . $symbol . '</label>
<input type="text" size="5" name="' . $key .'" value="' . esc_attr( $value ) . '" />
</p></div>';
}
// Save "Wholesale Price" custom field to Products
add_action( 'woocommerce_process_product_meta_simple', 'w4dev_save_product_wholesale_price', 20, 1 );
function w4dev_save_product_wholesale_price( $product_id ) {
if( isset($_POST['_wholesale_price']) )
update_post_meta( $product_id, '_wholesale_price', $_POST['_wholesale_price'] );
}
// Save "Wholesale Price" custom field to VARIATIONS
add_action( 'woocommerce_save_product_variation', 'w4dev_save_product_variation_wholesale_price', 20, 2 );
function w4dev_save_product_variation_wholesale_price( $variation_id, $i ){
if ( isset( $_POST['wholesale_price'][$i] ) ) {
update_post_meta( $variation_id, '_wholesale_price', floatval( $_POST['wholesale_price'][$i] ) );
}
}
// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'w4dev_custom_price', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'w4dev_custom_price', 90, 2 );
// Product variations (of a variable product)
add_filter('woocommerce_product_variation_get_regular_price', 'w4dev_custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'w4dev_custom_price', 90, 2 );;
function w4dev_custom_price( $price, $product ) {
if( get_post_meta( $product->get_id(), '_wholesale_price', true ) > 0 )
$price = get_post_meta( $product->get_id(), '_wholesale_price', true );
return $price;
}
// Variable product price ramge
add_filter('woocommerce_variation_prices_price', 'w4dev_custom_variation_price', 90, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'w4dev_custom_variation_price', 90, 3 );
function w4dev_custom_variation_price( $price, $variation, $product ) {
if( get_post_meta( $variation->get_id(), '_wholesale_price', true ) > 0 )
$price = get_post_meta( $variation->get_id(), '_wholesale_price', true );
return $price;
}
代码位于活动子主题(或活动主题)的functions.php文件中。已测试且有效。
现在您将能够更改“批发价格”的值并在产品变体中处理它。
产品变化批发价格设置:
要处理有关 Woocommerce 中销售价格的销售价格,有以下相关挂钩:
woocommerce_product_get_sale_price
woocommerce_product_variation_get_sale_price
woocommerce_variation_prices_get_sale_price