Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Created April 10, 2023 21:32
Show Gist options
  • Select an option

  • Save Acephalia/00b84ae9e70e8cc3cf3ef096b8ea748e to your computer and use it in GitHub Desktop.

Select an option

Save Acephalia/00b84ae9e70e8cc3cf3ef096b8ea748e to your computer and use it in GitHub Desktop.
WC Name Your Price: Add Price Change To Cart & Checkout
// Add Name Your Price Field on Cart page
add_filter( 'woocommerce_cart_item_price', 'add_name_your_price_field_to_cart', 10, 3 );
function add_name_your_price_field_to_cart( $product_price, $cart_item, $cart_item_key ) {
global $woocommerce;
// Check if the Name Your Price plugin is active and if the current product supports it
if ( class_exists( 'WC_Name_Your_Price' ) && WC_Name_Your_Price_Helpers::product_supports_name_your_price( $cart_item['data'] ) ) {
// Add Name Your Price Field
$name_your_price_input = woocommerce_name_your_price_input( $cart_item['product_id'], '', '', false );
$product_price .= $name_your_price_input;
}
return $product_price;
}
// Add Name Your Price Field on Checkout page
add_filter( 'woocommerce_cart_item_price', 'add_name_your_price_field_to_checkout', 10, 3 );
function add_name_your_price_field_to_checkout( $product_price, $cart_item, $cart_item_key ) {
global $woocommerce;
// Check if the Name Your Price plugin is active and if the current product supports it
if ( class_exists( 'WC_Name_Your_Price' ) && WC_Name_Your_Price_Helpers::product_supports_name_your_price( $cart_item['data'] ) ) {
// Add Name Your Price Field
$name_your_price_input = woocommerce_name_your_price_input( $cart_item['product_id'], '', '', false );
$product_price .= $name_your_price_input;
}
return $product_price;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment