Answers to commonly asked questions about the WooCommerce Name Your Price extension.
Note: Customizations on this page are considered Developer level. If you are unfamiliar with code/templates and resolving potential conflicts, select a WooExpert or Developer for assistance. We are unable to provide support for customizations under our Support Policy.
How do I change the markup?
↑ Back to topminimum-product.php
inside the /woocommerce/single-product
folder of your theme.
You can technically do the same with the price-input.php
template, however, much of what makes the Name Your Price component function is wrapped up in helper functions in that template, so only edit this if you really know what you are doing.
How can I move the markup?
↑ Back to topwoocommerce_single_product_summary
action hook, while the text input is attached to the woocommerce_before_add_to_cart_button
hook. The minimum price is attached to the woocommerce_nyp_after_price_input
hook.
Following typical WordPress behavior for hooks, to change the location of any of these templates you must remove them from their default hook and add them to a new hook. For example, to relocate the price input place the following in your theme’s functions.php and be sure to adjust the_hook_you_want_to_add_to
with your desired hook’s name.
To move the price input:
function nyp_move_price_input(){ if( class_exists( 'WC_Name_Your_Price_Display' ) ) { remove_action( 'woocommerce_before_add_to_cart_button', array( WC_Name_Your_Price()->display, 'display_price_input' ), 9 ); add_action( 'the_hook_you_want_to_add_to', array( WC_Name_Your_Price()->display, 'display_price_input' ) ); } } add_action( 'woocommerce_before_single_product' , 'nyp_move_price_input' );To not display a suggested price, you can simply leave the suggested field blank when setting up the product’s meta information. (See the Usage instructions). Similarly, to not enforce a minimum price, leave that field blank when setting up the product information.
How do I remove the stylesheet?
↑ Back to topHow can I hide the minimum price?
↑ Back to topwoocommerce_single_product_summary
action hook. Following typical WordPress behavior for hooks, to change the location of any of these templates you must remove them from their default hook. For example, to remove the minimum price (and to obfuscate the error messages), you’d place the following in your theme’s functions.php:
function nyp_remove_minimum_price(){ remove_action( 'woocommerce_nyp_after_price_input', array( WC_Name_Your_Price()->display, 'display_minimum_price' ) ); } add_action( 'woocommerce_nyp_before_price_input' , 'nyp_remove_minimum_price' ); function kia_change_minimum_error_templates( $templates ) { $templates['minimum'] = __( '"%%TITLE%%" could not be added to the cart: Please enter a higher price.', 'your-plugin-slug' ); $templates['minimum_js'] = __( 'Please enter a higher price', 'your-plugin-slug' ); return $templates; } add_action( 'woocommerce_nyp_error_message_templates', 'kia_change_minimum_error_templates' );
Force NYP products to be “sold individually”?
↑ Back to topwp-plugins
and activate it.
Requires WC 2.6.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WooCommerce Name Your Price Sold Individually | |
* Plugin URI: https://gist.github.com/helgatheviking/a8802255167751a5dd746f83cdfc8716 | |
* Description: Stricter enforcement of "Sold Individually" for NYP items | |
* Version: 1.2.0 | |
* WC requires at least: 2.6.3 | |
* Author: Kathy Darling | |
* Author URI: http://kathyisawesome.com/ | |
* | |
* Copyright: © 2016 Kathy Darling | |
* License: GNU General Public License v3.0 | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
*/ | |
/** | |
* Attach hooks only when NYP is active. | |
*/ | |
function wc_nyp_sold_individually_init() { | |
add_filter( 'woocommerce_cart_id', 'wc_nyp_force_sold_individually', 10, 5 ); | |
} | |
add_action( 'wc_name_your_price_loaded', 'wc_nyp_sold_individually_init' ); | |
/** | |
* Regenerate a unique ID for the cart item being added. | |
* | |
* @param string cart item key. | |
* @param int $product_id – id of the product the key is being generated for. | |
* @param int $variation_id of the product the key is being generated for. | |
* @param array $variation data for the cart item. | |
* @param array $cart_item_data other cart item data passed which affects this items uniqueness in the cart. | |
* @return string | |
*/ | |
function wc_nyp_force_sold_individually( $cart_id, $product_id, $variation_id, $variation, $cart_item_data ) { | |
// Get the product. | |
$product = wc_get_product( $variation_id ? $variation_id : $product_id ); | |
if ( $product->is_sold_individually() && WC_Name_Your_Price_Helpers::is_nyp( $product ) ){ | |
$id_parts = array( $product_id ); | |
if ( $variation_id && 0 != $variation_id ) { | |
$id_parts[] = $variation_id; | |
} | |
if ( is_array( $variation ) && ! empty( $variation ) ) { | |
$variation_key = ''; | |
foreach ( $variation as $key => $value ) { | |
$variation_key .= trim( $key ) . trim( $value ); | |
} | |
$id_parts[] = $variation_key; | |
} | |
$cart_id = md5( implode( '_', $id_parts ) ); | |
} | |
return $cart_id; | |
} |
Leave the price input blank
↑ Back to topadd_filter( 'woocommerce_nyp_get_initial_price', '__return_null' );
Add a placeholder to the price input
↑ Back to top<input>
element you need to filter woocommerce_get_price_input
by adding the following to your theme’s functions.php
.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Modify NYP price input's placeholder. | |
* | |
* @param array $attributes The array of attributes for the NYP div | |
* @param obj $product WC_Product The product object | |
* @param string $suffix – needed for grouped, composites, bundles, etc. | |
* @return string | |
* @return string – The modified input html. | |
*/ | |
function kia_custom_nyp_placeholder( $attributes, $product, $suffix ) { | |
$attributes['placeholder'] = __( 'Enter price here', 'your-text-domain' ); | |
return $attributes; | |
} | |
add_filter( 'wc_nyp_price_input_attributes', 'kia_custom_nyp_placeholder', 10, 3 ); |