I want to add a comment to individual products in the cart page. I am new to woocommerce wordpress plugin so I have no idea how to do.
I have done some research. In that tutorial, I found that I can use woocommerce_add_cart_item_data hook this way:
add_filter( 'woocommerce_add_cart_item_data', 'add_comment', 10, 3 );
function add_comment( $cart_item_data, $product_id, $variation_id ) {
$cart_item_data['comment'] = 'This is comment';
return $cart_item_data;
}
but this does not work in my case.
I attach the Cart page image so you can understand.
I hope you guys understand what I want?
Thank you.
Below code will add a custom text to an item in cart:
You need to create a custom field "comment" for the product to use it here.
add_filter( 'woocommerce_add_cart_item_data', 'add_comment', 10, 3 );
function add_comment( $cart_item_data, $product_id, $variation_id ) {
$cart_item_data['comment'] = 'This is comment';
return $cart_item_data;
}
Add a Custom text/comment before Cart table in Cart Page:
add_action( 'woocommerce_before_cart_table', 'add_comment' );
function add_comment()
{
echo '<div class="woocommerce-info">This is comment</div>';
}
Add a Custom text/comment after Cart table in Cart Page:
add_action( 'woocommerce_after_cart_table', 'add_comment' );
function add_comment() {
echo '<div class="woocommerce-info">This is comment</div>';
}
HOW TO ADD AN INPUT FIELD TO WOOCOMMERCE CART ITEMS & Let users update input fields in the cart
https://pluginrepublic.com/how-to-add-an-input-field-to-woocommerce-cart-items/
Refer this link for a working solution to similar request:
https://stackoverflow.com/a/21818028/12291365
If you are okay with using the plugin, then this plugin will do the trick:
https://wordpress.org/plugins/wc-fields-factory/
Use code snippet below for adding custom values for each cart item.
Please add your logic for attaching values for each item
// Add custom value into cart item
add_filter('woocommerce_add_cart_item_data','sub_add_item_data',1,2);
if(!function_exists('sub_add_item_data'))
{
function sub_add_item_data($cart_item_data,$product_id)
{
// use condition for adding custom value here
$new_value = array(
'sub_custom_value' => 'custom value',
);
return array_merge($cart_item_data,$new_value);
}
}
// Extract custom values
add_filter('woocommerce_get_cart_item_from_session', 'sub_get_cart_items_from_session', 1, 3 );
if(!function_exists('sub_get_cart_items_from_session'))
{
function sub_get_cart_items_from_session($item,$values,$key)
{
if (array_key_exists( 'sub_custom_value', $values ) )
{
$item['sub_custom_value'] = $values['sub_custom_value'];
}
return $item;
}
}
// display in cart and checkout
add_filter('woocommerce_checkout_cart_item_quantity','sub_add_user_custom_option_from_session_into_cart',1,3);
add_filter('woocommerce_cart_item_price','sub_add_user_custom_option_from_session_into_cart',1,3);
if(!function_exists('sub_add_user_custom_option_from_session_into_cart'))
{
function sub_add_user_custom_option_from_session_into_cart($product_name, $values, $cart_item_key )
{
if(isset( $values['sub_custom_value'] ) && '' != $values['sub_custom_value'] )
{
$return_string = $product_name . "</a><dl class='variation'>";
$return_string .= "<table class='sub_options_table' id='" . $values['product_id'] . "'>";
$return_string .= "<tr><td>" . $values['sub_custom_value'] . "</td></tr>";
$return_string .= "</table></dl>";
return $return_string;
}
else
{
return $product_name;
}
}
}
Related
By default WooCommerce shows the attribute of a variable product in the title and I'm using this code to show the attribute below the title in the cart and checkout pages:
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
But that doesn't work in My Account page, users see the full product name with no attribute.
To fix it I'm using the code below to show the attribute in the product title:
function show_attributes_outside_title_1( $enabled ) {
if ( !is_account_page() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_product_variation_title_include_attributes', 'show_attributes_outside_title_1' );
function show_attributes_outside_title_2( $enabled ) {
if ( !is_account_page() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_is_attribute_in_product_name', 'show_attributes_outside_title_2' );
But I'd like to show the attribute below the title (or a new column), it's easier to read and goes with the same desing you see in the cart and checkout pages.
There is some confusion in the initial part of the question.
You say you want to show the attribute under the product title on the cart and checkout page but then return __return_false, do you intend to do the opposite?
SOLUTION #1
You may want to reverse the check to make sure that your chosen product variation attribute is shown under the product name on your account page under Downloads (as evidenced by your comment above):
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
add_filter( 'woocommerce_product_variation_title_include_attributes', 'show_attributes_outside_title_1' );
function show_attributes_outside_title_1( $enabled ) {
if ( is_account_page() ) {
$enabled = true;
}
return $enabled;
}
add_filter( 'woocommerce_is_attribute_in_product_name', 'show_attributes_outside_title_2' );
function show_attributes_outside_title_2( $enabled ) {
if ( ! is_account_page() ) {
$enabled = false;
}
return $enabled;
}
SOLUTION #2
If you want to leave the code in your question unchanged you can use the woocommerce_account_downloads_column_download-product hook where download-product is the id of the product name column (in the /my-account/downloads/ page). Here the documentation.
Finally, with the wc_get_formatted_variation function you can get the name of the chosen variation. For more information on parameters read the documentation.
// shows the variation chosen in the product name in the download table of the my-account page
add_action( 'woocommerce_account_downloads_column_download-product', 'change_product_download_name', 10, 1 );
function change_product_download_name( $download ) {
// gets the product object
$product = wc_get_product( $download['product_id'] );
// gets the name of the produc
$product_name = $download['product_name'];
// if the product is a variation
if ( $product->is_type( 'variation' ) ) {
// gets the name of the product with the chosen variation
$product_name = $product->get_name() . " - " . wc_get_formatted_variation( $product, true, false, false );
}
// print the product name (with or without product url)
if ( $download['product_url'] ) {
echo '' . esc_html( $product_name ) . '';
} else {
echo esc_html( $product_name );
}
}
The code has been tested and works. Add it to your active theme's functions.php.
I changed Vincenzo's answer a bit to make it look the same way I see the attributes in my Cart and Checkout pages. Here's the code in case anybody else needs it:
// Shows the variation chosen in the product name in the download table of the my-account page
add_action( 'woocommerce_account_downloads_column_download-product', 'change_product_download_name', 10, 1 );
function change_product_download_name( $download ) {
// gets the product object
$product = wc_get_product( $download['product_id'] );
// gets the name of the product
$product_name = $download['product_name'];
// define variable
$product_attributes = '';
// if the product is a variation
if ( $product->is_type( 'variation' ) ) {
// gets the name of the product with the chosen variation
$product_name = $product->get_name();
$product_attributes = wc_get_formatted_variation( $product, true, true, false );
}
// print the product name (with or without product url)
if ( $download['product_url'] ) {
echo '' . esc_html( $product_name ) . '<p>' . esc_html( $product_attributes ) . '</p>';
} else {
echo esc_html( $product_name ) . '<p>' . esc_html( $product_attributes ) . '</p>';
}
}
// Shows variation outside title in cart and checkout pages
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
The last two filters replace my code and the first part solves the issue in the Downloads page.
I am using WooCommerce and I'm trying to display product variation SKUs on the product page below the product title. I managed to find this code, which works but displays the SKU in the wrong place:
// Display product variations SKU and GTIN info
add_filter( 'woocommerce_available_variation', 'display_variation_sku_and_gtin', 20, 3 );
function display_variation_sku_and_gtin( $variation_data, $product, $variation ) {
$html = ''; // Initializing
// Inserting SKU
if( ! empty( $variation_data['sku'] ) ){
$html .= '</div><div class="woocommerce-variation-sku">' . __('SKU:') . ' ' . $variation_data['sku'];
}
// Using the variation description to add dynamically the SKU and the GTIN
$variation_data['variation_description'] .= $html;
return $variation_data;
}
Can anyone help with changing the order of this code so the SKU shows below the product title, or help me with some new code?
Many thanks!
WooCommerce does not provide a specific action hook that will let you add anything right after the product title, that also makes use of the variation data like the hook in your current code. You can work around this by adding an element after the product title via JavaScript/jQuery.
You want this element to dynamically change based on the selected variation. Since you do not have access to the variation data directly in the action hook you will have to check the hidden input variation_id that WooCommerce uses to store the selected variation id. Then use AJAX every time that input changes to retrieve the variation SKU belonging to this variation id.
add_action( 'woocommerce_before_single_product', 'show_variation_sku_underneath_product_title' );
function show_variation_sku_underneath_product_title() {
global $product;
if ( $product->is_type('variable') ) {
?>
<script>
jQuery(document).ready(function($) {
$('input.variation_id').change( function(){
if( '' != $('input.variation_id').val() ) {
jQuery.ajax( {
url: '<?php echo admin_url( 'admin-ajax.php'); ?>',
type: 'post',
data: {
action: 'get_variation_sku',
variation_id: $('input.variation_id').val()
},
success: function(data) {
$('h1.product_title').siblings('.variation-sku').remove();
if(data.length > 0) {
$('h1.product_title').after('<p class="variation-sku">' + data + '</p>');
}
}
});
}
});
});
</script>
<?php
}
}
add_action('wp_ajax_get_variation_sku' , 'get_variation_sku');
add_action('wp_ajax_nopriv_get_variation_sku','get_variation_sku');
function get_variation_sku() {
$variation_id = intval( $_POST['variation_id'] );
$sku = '';
if ( $product = wc_get_product( $variation_id ) ) $sku = $product->get_sku();
echo $sku;
wp_die(); // this is required to terminate immediately and return a proper response
}
These code snippets should be added to the functions.php of your child theme or via a plugin like Code Snippets.
on my wordpress page I have a gallery which displays featured images.
I would like to add HTML to a specific item, using the wordpress functions file.
If I run the following code:
add_filter( 'post_thumbnail_html', 'add_featured_image_html', 10, 1 );
function add_featured_image_html( $html ) {
return $html .= '<p>'.get_the_ID().' is the ID</p>';
}
My frontend does display the post ID's underneath my thumbnails. In this case, I would like to target Post_ID 19 (btw this is a custom post type).
If I extend my code to:
$id = get_the_ID();
if ($id == 19) {
add_filter( 'post_thumbnail_html', 'add_featured_image_html', 10, 1 );
function add_featured_image_html( $html ) {
return $html .= '<p>'.get_the_ID().' is the ID</p>';
}
}
Nothing is displayed.
Is there someone that could help me out? thanks a million!
The second parameter of this filter is the post id. https://developer.wordpress.org/reference/hooks/post_thumbnail_html/
This isn't tested yet, because I'm on my phone... But this should work.
add_filter( 'post_thumbnail_html', 'add_featured_image_html', 10, 2 );
function add_featured_image_html( $html , $id) {
if ($id == 19){
return $html .= '<p>'.get_the_ID().' is the ID</p>';}
else {return $html}
}
Trying to add a custom button in the cart page. If the button is clicked particular product should add and cart page should refresh with ajax load. I used below code but it is refreshing the page without updating cart prices.
add_filter( 'woocommerce_cart_item_price', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data']; // Get the WC_Product Object
if ( $value = $product->get_meta('pro_price_extra_info') ) {
$product_name .= '<a class="gt_price" href="example.com/cart/?add-to-cart=250">Get this price</a>';
}
return $product_name;
}
Please help me with the code. Thanks in advance
I've just tested your code and it worked as you mentioned. All you need is to add a line to recalculate cart totals: "WC()->cart->calculate_totals();"
Based on your code:
add_filter( 'woocommerce_cart_item_price', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data']; // Get the WC_Product Object
if ( $value = $product->get_meta('pro_price_extra_info') ) {
$product_name .= '<a class="gt_price" href="example.com/cart/?add-to-cart=250">Get this price</a>';
}
WC()->cart->calculate_totals();
return $product_name;
}
For a better position for your button you may try this ():
add_action( 'woocommerce_cart_collaterals', 'custom_add_to_cart_redirect' );
function custom_add_to_cart_redirect() {
echo '<button class="de-button-admin de-button-anim-4">Get Your Discounted Product!</button>';
}
Cart Page Hooks: https://www.tychesoftwares.com/woocommerce-cart-page-hooks-visual-guide-with-code-examples/
I hope it works. Have a good day.
Is it possible to add custom text notice below the email field in the checkout page?
Yes, below code should do it.
add_filter( 'woocommerce_form_field' , 'so_27270883_woocommerce_form_field', 10, 2 );
function so_27270883_woocommerce_form_field( $field, $key) {
if ( is_checkout() ) {
if ($key == 'billing_email') {
$field .= '<p class="form-row">CUSTOM TEXT HERE</p>';
}
}
return $field;
}