Add a custom product calculated price to Woocommerce cart

前端 未结 1 1053
再見小時候
再見小時候 2021-01-03 07:35

In woocommerce I have added an extra text field before add to cart button to add custom charge on product purchase.

This plugin will add extra text field to single p

相关标签:
1条回答
  • 2021-01-03 08:19

    You don't need any javascript or sessions to achieve that. Try the following revisited code instead:

    // Add a custom field before single add to cart
    add_action( 'woocommerce_before_add_to_cart_button', 'custom_product_price_field', 5 );
    function custom_product_price_field(){
        echo '<div class="custom-text text">
        <p>Extra Charge ('.get_woocommerce_currency_symbol().'):</p>
        <input type="text" name="custom_price" value="" placeholder="e.g. 10" title="Custom Text" class="custom_price text_custom text">
        </div>';
    }
    
    // Get custom field value, calculate new item price, save it as custom cart item data
    add_filter('woocommerce_add_cart_item_data', 'add_custom_field_data', 20, 2 );
    function add_custom_field_data( $cart_item_data, $product_id ){
        if (! isset($_POST['custom_price']))
            return $cart_item_data;
    
        $custom_price = (float) sanitize_text_field( $_POST['custom_price'] );
        if( empty($custom_price) )
            return $cart_item_data;
    
        $product    = wc_get_product($product_id); // The WC_Product Object
        $base_price = (float) $product->get_regular_price(); // Product reg price
    
        // New price calculation
        $new_price = $base_price + $custom_price;
    
        // Set the custom amount in cart object
        $cart_item_data['custom_data']['extra_charge'] = (float) $custom_price;
        $cart_item_data['custom_data']['new_price'] = (float) $new_price;
        $cart_item_data['custom_data']['unique_key'] = md5( microtime() . rand() ); // Make each item unique
    
        return $cart_item_data;
    }
    
    // Set the new calculated cart item price
    add_action( 'woocommerce_before_calculate_totals', 'extra_price_add_custom_price', 20, 1 );
    function extra_price_add_custom_price( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        foreach ( $cart->get_cart() as $cart_item ) {
            if( isset($cart_item['custom_data']['new_price']) )
                $cart_item['data']->set_price( (float) $cart_item['custom_data']['new_price'] );
        }
    }
    
    // Display cart item custom price details
    add_filter('woocommerce_cart_item_price', 'display_cart_items_custom_price_details', 20, 3 );
    function display_cart_items_custom_price_details( $product_price, $cart_item, $cart_item_key ){
        if( isset($cart_item['custom_data']['extra_charge']) ) {
            $product = $cart_item['data'];
            $product_price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) );
            $product_price .= '<br>' . wc_price( $cart_item['custom_data']['extra_charge'] ).'&nbsp;';
            $product_price .= __("Extra Charge", "woocommerce" );
        }
        return $product_price;
    }
    

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

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