Sami

If you prefer to implement Pay What You Want pricing in WooCommerce using custom PHP code instead of using a plugin, you can follow these steps:

  1. Create a child theme for your WooCommerce site, if you haven’t already done so. This will allow you to make customizations to your site without losing them when you update your theme.
  2. Open your child theme’s functions.php file.
  3. Add the following code to the functions.php file:
// Add a custom price input field to the product page
add_action( 'woocommerce_before_add_to_cart_button', 'pwyc_add_custom_price_field', 10 );
function pwyc_add_custom_price_field() {
    echo '<div class="pwyc-price-field">';
    woocommerce_form_field( 'custom_price', array(
        'type'          => 'number',
        'class'         => array( 'input-text', 'price', 'form-row-wide' ),
        'label'         => __( 'Enter your price', 'woocommerce' ),
        'placeholder'   => '',
        'min'           => '0',
        'required'      => true,
    ), '');
    echo '</div>';
}

// Add the custom price to the cart item
add_filter( 'woocommerce_add_cart_item_data', 'pwyc_add_custom_price_to_cart_item', 10, 2 );
function pwyc_add_custom_price_to_cart_item( $cart_item_data, $product_id ) {
    if( isset( $_POST['custom_price'] ) && $_POST['custom_price'] != '' ) {
        $cart_item_data['custom_price'] = wc_clean( $_POST['custom_price'] );
    }
    return $cart_item_data;
}

// Render the custom price in the cart and checkout
add_filter( 'woocommerce_cart_item_price', 'pwyc_render_custom_price_in_cart', 10, 3 );
function pwyc_render_custom_price_in_cart( $price, $cart_item, $cart_item_key ) {
    if( isset( $cart_item['custom_price'] ) ) {
        $price = wc_price( $cart_item['custom_price'] );
    }
    return $price;
}

// Set the custom price as the product price
add_action( 'woocommerce_before_calculate_totals', 'pwyc_set_custom_price_as_product_price' );
function pwyc_set_custom_price_as_product_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }
    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // Set custom price as product price
        if( isset( $cart_item['custom_price'] ) ) {
            $cart_item['data']->set_price( $cart_item['custom_price'] );
        }
    }
}
  1. Save the functions.php file.
  2. Go to the product you want to offer Pay What You Want pricing and refresh the page. You should now see a new input field where customers can enter their desired price.
  3. When a customer adds the product to their cart, their custom price will be saved as part of the cart item data.
  4. The custom price will be displayed in the cart and checkout, and the product price will be updated to reflect the custom price.

Note: This code is provided as an example only and may need to be modified to suit your specific needs. It is also important to thoroughly test any custom code before deploying it on a live site.

Leave a Reply

Your email address will not be published. Required fields are marked *