Enable Wholesale prices in Woocommerce 3

后端 未结 1 2056
说谎
说谎 2020-12-19 21:53

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

1条回答
  •  -上瘾入骨i
    2020-12-19 22:26

    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 '

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

    0 讨论(0)
提交回复
热议问题