在 Woocommerce 3 中启用批发价格 [英] Enable Wholesale prices in Woocommerce 3

查看:78
本文介绍了在 Woocommerce 3 中启用批发价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 wooCommerce 中,我在编辑产品页面设置中添加了一个带有自定义价格(批发价格)的自定义元字段.一切正常.

当我设置批发价格时,它在任何地方都可以正常工作.但是,如果我尝试更改此批发价格,则行不通.旧价格无处不在.

我做错了什么?如何解决此问题以允许更改批发价格?

我使用的代码:

function w4dev_get_wholesale_price( $product ){如果($product->is_type( array('simple', 'variable') )&&get_post_meta( $product->id, '_wholesale_price', true ) >0){返回 get_post_meta( $product->id, '_wholesale_price', true );}否则($product->is_type('变体')&&get_post_meta( $product->variation_id, '_wholesale_price', true ) >0){返回 get_post_meta( $product->variation_id, '_wholesale_price', true );}返回0;}add_action('woocommerce_product_options_pricing', 'w4dev_woocommerce_product_options_pricing');函数 w4dev_woocommerce_product_options_pricing(){woocommerce_wp_text_input(数组('id' =>'_批发价','类' =>'wc_input_wholesale_price 空头','标签' =>__( '批发价', 'woocommerce' ) .' ('.get_woocommerce_currency_symbol().')','类型' =>'文本'));}add_action('woocommerce_process_product_meta_simple', 'w4dev_woocommerce_process_product_meta_simple', 10, 1);函数 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);函数 w4dev_woocommerce_get_price( $price, $product ){if(w4dev_get_wholesale_price($product) > 0 ) {$price = w4dev_get_wholesale_price($product);返回 $price;}}

解决方案

您的代码中存在一些错误,还有很多缺失的部分:

  • 钩子 woocommerce_get_price 已弃用且已过时
  • 要处理产品变体,您需要更多代码(对于可变产品价格范围也是如此).
  • 其他错误,例如保存批发价格时......

您重新访问的代码:

//添加批发价";产品选项定价的自定义字段add_action('woocommerce_product_options_pricing', 'w4dev_add_product_options_pricing');函数 w4dev_add_product_options_pricing(){woocommerce_wp_text_input(数组('id' =>'_批发价','类' =>'wc_input_wholesale_price 空头','标签' =>__( '批发价', 'woocommerce' ) .' ('.get_woocommerce_currency_symbol().')','类型' =>'文本'));}//将自定义字段添加到 VARIATIONS 选项定价add_action('woocommerce_variation_options_pricing', 'w4dev_add_variation_options_pricing', 20, 3);函数 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[' .$循环.']';echo '<div class="variable_wholesale-price"><p class="form-row form-row-first"><标签>'.__(批发价格",woocommerce").$符号.'</标签><输入类型=文本"大小=5"名称="'.$key.'"值="'.esc_attr( $value ) .'"/></p></div>';}//保存批发价"产品的自定义字段add_action('woocommerce_process_product_meta_simple', 'w4dev_save_product_wholesale_price', 20, 1);函数 w4dev_save_product_wholesale_price( $product_id ) {if( isset($_POST['_wholesale_price']) )update_post_meta( $product_id, '_wholesale_price', $_POST['_wholesale_price'] );}//保存批发价"自定义字段到 VARIATIONSadd_action('woocommerce_save_product_variation', 'w4dev_save_product_variation_wholesale_price', 20, 2);函数 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]));}}//简单、分组和外部产品add_filter('woocommerce_product_get_price', 'w4dev_custom_price', 90, 2);add_filter('woocommerce_product_get_regular_price', 'w4dev_custom_price', 90, 2 );//产品变体(可变产品的)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 );;函数 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 );返回 $price;}//可变产品价格区间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);函数 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 );返回 $price;}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经过测试并有效.


现在,您将能够更改批发价格"的价值并在产品变体中进行处理.

产品变化批发价格设置:


<块引用>

要处理有关 Woocommerce 中销售价格的销售价格,有一些相关的钩子:

<块引用>

  • woocommerce_product_get_sale_price

  • woocommerce_product_variation_get_sale_price
  • woocommerce_variation_prices_get_sale_price

In wooCommerce, I have added a custom meta field with a custom price (wholesale price) in edit product pages settings. Everything works well.

When I set a wholesale price it works fine everywhere. But if I try to change this wholesale price it doesn't work. the old price remains everywhere.

What I am doing wrong? How can I solve this problem to allow wholesale price changes?

The code I am using:

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;
    }
}

解决方案

There are some errors in your code and a lot of missing parts too:

  • The hook woocommerce_get_price is deprecated and outdated
  • To handle Product variations you need some more code (same thing for the variable products price range).
  • Other errors, like when saving your Wholesale price…

Your revisited code:

// 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;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


Now you will be able to change the value of your 'Wholesale Price' and to handle it in product variations.

Product variations Wholesale price setting:


To handle sale prices regarding Sale prices in Woocommerce, there is those related hooks:

  • woocommerce_product_get_sale_price

  • woocommerce_product_variation_get_sale_price
  • woocommerce_variation_prices_get_sale_price

这篇关于在 Woocommerce 3 中启用批发价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆