Making the coupon field mandatory on WooCommerce - wordpress

I was wondering if it was possible to make the coupon field mandatory on WooCommerce.
I know that this is possible using functions, however this is slightly above my current skill level so I was wondering if you could give me a step-by-step version of how to accomplish this. Any answer would be much appreciated.

I don't know about the function but you can modify the plugin to achieve this in following manner :
Make one folder in your theme folder woocommerce and in new created woocommerce folder, create another folder with checkout name.
So now it will look something like wp-content > themes > your-theme > woocommerce > checkout.
Now go to your plugin directory and follow below path :
wp-content > plugins > woocommerce > templates > checkout
When you go in above path, you will find one file named as form-coupon.php. Copy that file and paste it to the directory which we created at top of that answer.
wp-content > themes > your-theme > woocommerce > checkout > form-coupon.php.
Now its time to modify the code in wp-content > themes > your-theme > woocommerce > checkout > form-coupon.php :
Find following code line in above mentioned file :
<input type="text" name="coupon_code" class="input-text" placeholder="<?php _e( 'Coupon code', 'woocommerce' ); ?>" id="coupon_code" value="" />
And replace above line with
<input type="text" name="coupon_code" class="input-text" placeholder="<?php _e( 'Coupon code', 'woocommerce' ); ?>" id="coupon_code" value="" required/>
Note: Here I have added required attribute of html.
Tell me if you have any doubt.
UPDATED:
add_action('woocommerce_check_cart_items', 'make_coupon_code');
function make_coupon_code()
{
global $woocommerce;
if(is_cart() || is_checkout()){
$my_coupon = $woocommerce->cart->applied_coupons;
echo $woocommerce->cart->get_applied_coupons;
if(empty($my_coupon))
{
$woocommerce->add_error("Please enter coupon code to checkout.");
}
}
}
Please give it a try and let me know feedback.
NOTE: UNTESTED

to solve the problem try this code:
<?php
add_action('woocommerce_check_cart_items', 'make_coupon_code');
function make_coupon_code()
{
global $woocommerce;
if(is_cart() || is_checkout()){
$my_coupon = $woocommerce->cart->applied_coupons;
echo $woocommerce->cart->get_applied_coupons;
if(empty($my_coupon))
{
wc_add_notice( '<strong>' . $btn['label'] . '</strong> ' . __( 'insert coupon code', 'woocommerce' ), 'error' );
}
}
}
?>
in functions.php instead of the one above...
for me it works

Add the following code in functions.php
Require Coupon for Single Product
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_for_specific_items' );
function mandatory_coupon_for_specific_items() {
$targeted_ids = array(37); // The targeted product ids (in this array)
$coupon_code = 'summer2'; // The required coupon code
$coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );
// Loop through cart items
foreach(WC()->cart->get_cart() as $cart_item ) {
// Check cart item for defined product Ids and applied coupon
if( in_array( $cart_item['product_id'], $targeted_ids ) && ! $coupon_applied ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.',
$cart_item['data']->get_name() ), 'error' );
break; // stop the loop
}
}
}
Replace 37 by the your product ID in $targeted_ids = array(37); you can have multiple product IDs like $targeted_ids = array(37,48,12);
Replace "summer2" by any other coupon code in $coupon_code = 'summer2';
Don't forget to add this coupon code in WooCommerce before using it.
Require coupon for all products
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
$product_categories = array( 'clothing' ); // Category ID or slug of targeted category
$coupon_code = 'summer2'; // The required coupon code
$coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ){
if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) && !$coupon_applied ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.',
$cart_item['data']->get_name() ), 'error' );
break; // stop the loop
}
}
}
Replace $product_categories = array( 'clothing' ); by any other category name or category ID.

Related

How to fix 'Woocommerce Cart PDF' functions file to move pdf-link to checkout page instead of cart page

I'm using a plugin for wordpress-woocommerce called Woocommerce Cart PDF (https://wordpress.org/plugins/wc-cart-pdf/). It generates a pdf-link of the current cart, but is located on the cart page.
I have a combined cart and checkout page so the link does not appear on my website. I don't have the knowledge on how to edit the plugin files myself for it to appear on my cart page.
I've tried the wordpress plugin support forum for this specific plugin, but no answer.
/**
* Generates the PDF for download
*
* #return void
*/
function wc_cart_pdf_process_download() {
if( ! function_exists( 'WC' ) ) {
return;
}
if( ! isset( $_GET['cart-pdf'] ) ) {
return;
}
if( ! is_cart() || WC()->cart->is_empty() ) {
return;
}
if( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'cart-pdf' ) ) {
wc_add_notice( __( 'Invalid nonce. Unable to process PDF for download.', 'wc_cart_pdf' ), 'error' );
return;
}
$dompdf = new \Dompdf\Dompdf();
$content = $css = '';
$cart_table = wc_locate_template( 'cart-table.php', '/woocommerce/wc-cart-pdf/', __DIR__ . '/templates/' );
$css = wc_locate_template( 'pdf-styles.php', '/woocommerce/wc-cart-pdf/', __DIR__ . '/templates/' );
do_action( 'wc_cart_pdf_before_process' );
if( file_exists( $cart_table ) ) {
ob_start();
include $cart_table;
$content = ob_get_clean();
}
if( file_exists( $css ) ) {
ob_start();
include $css;
$css = apply_filters( 'woocommerce_email_styles', ob_get_clean() );
}
$dompdf->loadHtml( '<style>' . $css . '</style>' . $content );
$dompdf->setPaper( 'A4', 'portrait' );
$dompdf->render();
$dompdf->stream(
apply_filters( 'wc_cart_pdf_filename', 'WC_Cart-' . date( 'Ymd' ) . bin2hex( openssl_random_pseudo_bytes( 5 ) ) ) . '.pdf',
/**
* 'compress' => 1 or 0 - apply content stream compression, this is on (1) by default
* 'Attachment' => 1 or 0 - if 1, force the browser to open a download dialog, on (1) by default
*/
apply_filters( 'wc_cart_pdf_stream_options', array( 'compress' => 1, 'Attachment' => 1 ) )
);
exit;
}
add_action( 'template_redirect', 'wc_cart_pdf_process_download' );
if( ! function_exists( 'wc_cart_pdf_button' ) ) {
/**
* Renders the download cart as PDF button
*
* #return void
*/
function wc_cart_pdf_button() {
if( ! is_cart() || WC()->cart->is_empty() ) {
return;
}
?>
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'cart-pdf' => '1' ), wc_get_cart_url() ), 'cart-pdf' ) );?>" class="cart-pdf-button button" target="_blank">
<?php esc_html_e( 'Download Cart as PDF', 'wc-cart-pdf' ); ?>
</a>
<?php
}
}
add_action( 'woocommerce_proceed_to_checkout', 'wc_cart_pdf_button', 21 );
This might be the incorrect part of the functions in the plugin, but I hop I got it right.
The wc_cart_pdf_process_download() function isn't really relevant. The comment states that it "Generates the PDF for download". What it's doing is responding when the user visits the PDF link by generating the requested PDF file. The important function is the one beneath that, wc_cart_pdf_button().
Now that we know the function we're interested in, what's next? In your question, you suggested editing the plugin files however it's important to avoid doing that. Editing your plugin files is a sure-fire way to guarantee the changes you make get overwritten the next time you update.
You have a couple of options:
Create a mini feature plugin.
Add the code to the bottom of your (hopefully child) theme's functions.php file.
The first option would be the recommended approach but that's going to take us well beyond the scope of the question. Placing the code in a child theme's functions.php file will be adequate for getting you up and running.
Okay, so now we know what the code we want to modify is and where we're going to store those modifications. Let's break down the actual code:
if( ! is_cart() || WC()->cart->is_empty() ) {
return;
}
This checks two things, are we on the cart page and does the cart contain items? If either is false, we're going to bail out early. You're on the checkout page, not the cart page, so even if this function were to be called, it wouldn't make it past this conditional.
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'cart-pdf' => '1' ), wc_get_cart_url() ), 'cart-pdf' ) );?>" class="cart-pdf-button button" target="_blank">
<?php esc_html_e( 'Download Cart as PDF', 'wc-cart-pdf' ); ?>
</a>
If those two previous checks passed, generate the button output.
add_action( 'woocommerce_proceed_to_checkout', 'wc_cart_pdf_button', 21 );
This executes the code on the woocommerce_proceed_to_checkout hook which fires after the cart totals on the cart page. The same action is used by the checkout button itself.
We need to write our own function that displays that same output on the checkout page. Without knowing where you'd like the button to appear, I can't suggest which action to use. I'm using woocommerce_checkout_order_review with a priority that'll put it between the order table and the payment options. If you need to reposition it, you'll have to go through those hooks and find somewhere that feels appropriate.
You did mention in your question that this is necessary because you have your cart and checkout pages combined. You may require a completely different hook, again there's no way for me to know based on your question alone.
Here's the final code:
function stackoverflow_wc_checkout_pdf_button() {
// We're on the checkout page based on the action.
// Highly unlikely we need the is_empty() check but it can't hurt if you may find yourself reusing elsewhere.
if ( WC()->cart->is_empty() ) {
return;
} ?>
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'cart-pdf' => '1' ), wc_get_cart_url() ), 'cart-pdf' ) );?>" class="cart-pdf-button button" target="_blank">
<?php esc_html_e( 'Download Cart as PDF', 'wc-cart-pdf' ); ?>
</a>
<?php
}
add_action( 'woocommerce_checkout_order_review', 'stackoverflow_wc_checkout_pdf_button', 15 );

Add Woocommerce Temporary product

Is it possible to make a user-generated temporary product to be added to cart in WooCommerce?
I'm trying to transfer a non-wordpress website to WordPress, but the site already has a sophisticated e-commerce system that the client doesn't want to change. What basically happens is a visitor specifies the measurement of the product that the client is selling, add different variations to it, and then after submitting, the website generates the product's price based on the visitor's input.
Adding products will be very tedious because they have too many products with thousands of variations. So our solution is this.
I'm open to other plugin suggestions aside from WooCommerce. I already tried using Gravity Forms, but I got stuck in the end because our client's current website has to be an e-commerce site after it was added to the cart.
This is solved!
Here is what I did just in case someone needs help with this in the future.
First I made a page with a form. Action is whichever the page will live in and method is post.
<form method="post" action="/order-page">
<label for="tc_name" title="Full Name">Full Name</label>
<input type="text" name="tc_name"/>
<label for="tc_title" title="Product Name">Product Name</label>
<input type="text" name="tc_title">
<label for="tc_description" title="Product Description">Product Description</label>
<textarea name="tc_description"></textarea>
<label for="tc_price" title="Price">Price</label>
<input type="number" name="tc_price"/>
<input type="submit" value="Submit"/>
</form>
Then on the next page, I grabbed the values, created the product based on the values given, and then added a shortcode to display the cart button.
if('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['tc_price'])) {
//grab the values
$pName = $_POST["tc_name"];
$pDescription = $_POST["tc_description"];
$pPrice = $_POST["tc_price"];
$post_title = $_POST["tc_title"];
//add them in an array
$post = array(
'post_author' => $pName,
'post_content' => $pDescription,
'post_status' => "publish",
'post_title' => $post_title,
'post_type' => "product",
);
//create product
$product_id = wp_insert_post( $post, __('Cannot create product', 'bones') );
//type of product
wp_set_object_terms($product_id, 'simple', 'product_type');
//add price to the product, this is where you can add some descriptions such as sku's and measurements
update_post_meta( $product_id, '_regular_price', $pPrice );
update_post_meta( $product_id, '_sale_price', $pPrice );
update_post_meta( $product_id, '_price', $pPrice );
//get woocommerce shortcode for add to cart
$myButton = do_shortcode('[add_to_cart id="' . $product_id . '"]');
//display product
echo $myButton;
}
and then lastly, once an order is completed, delete the product by hooking an action to woocommerce_thankyou. I put it in the functions.
function mysite_completed($order_id) {
//get order ID
$order = new WC_Order( $order_id );
//grab items from the order id
$items = $order->get_items();
//loop thru all products in the order section and get product ID
foreach ( $items as $item ) {
$product_id = $item['product_id'];
//choose whatever suites you, trash the product is what I picked
//permanently deletes product
//wp_delete_post($product_id);
//trashes post
wp_trash_post($product_id);
}
}
To Extend this topic and trash or delete products from a category when the order is successfully created.
First you need to create a custom category where all the products you'd like to trash or delete upon successful order creation will live. Then add the following code;
function delete_product_on_woocommerce_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
// 1. Get order object
$order = wc_get_order( $order_id );
// 2. Initialize $cat_in_order variable
$cat_in_order = false;
foreach ( $items as $item ) {
$product_id = $item['product_id'];
if ( has_term( 'machine', 'product_cat', $product_id ) ) { //Where machine is the custom product category slug
// 3. choose whatever suites you, delete the product is what I picked
//To Delete Product
wp_delete_post($product_id);
//To Trash Product
//wp_trash_post($product_id);
}
}
}
add_action( 'woocommerce_thankyou', 'delete_product_on_woocommerce_complete_order', 5 );
Code goes in function.php file of your active child theme (or active theme) or cutsom plugin. Tested and worked.

How to change add-to-cart form?

Is there a way to change the WooCommerce add-to-cart form through functions.php?
The goal is to add a checkbox for an additional product. When the checkbox is checked this product will also be added to the cart after a click on the add to cart button.
I am looking for a solution which doesn't rely on javascript.
A better title would be "WooCommerce up-sells as checkboxes".
A lot of research and several strategies to tackle this problem lead me to a solution which I thought was not even possible in the beginning.
The solution is now exactly what I wanted. A non-JavaScript, no-template-override, but a simple and pure addition to functions.php. It works for simple and variable products (and probably with grouped and external products too).
It misses some nice features still. It won't work yet if an up-sell is a variable product. Quantity selection and limiting up-sells per item or order would be nice additions too. Based on the code below adding those features should not be a big deal anymore.
// create the checkbox form fields and add them before the cart button
add_action( 'woocommerce_before_add_to_cart_button', 'action_woocommerce_before_add_to_cart_form', 10, 0 );
function action_woocommerce_before_add_to_cart_form(){
global $woocommerce, $product;
// get the product up-sells
$upsells = $product->get_upsells();
// store the number of up-sells and pass it on to the add-to-cart hook
?>
<input type="hidden" name="upsells_size" value="<?php echo(sizeof($upsells)); ?>">
<div id="wb-upsell-div">
<?php
// iterate through all upsells and add an input field for each
$i = 1;
foreach( $upsells as $value ){
$product_id = $value;
?>
<input id="wb-upsell-checkboxes" type="checkbox" name="upsell_<?php echo($i) ?>" value="<?php echo($product_id); ?>"><?php echo( '' . get_the_title( $product_id ) . "". " ($" . get_post_meta( $product_id, '_regular_price', true) . ")"); ?><br>
<?php
$i++;
}
?>
</div>
<?php
}
// function to add all up-sells, where the checkbox have been checked, to the cart
add_action('woocommerce_add_to_cart', 'custom_add_to_cart', 10, 3);
function custom_add_to_cart() {
global $woocommerce;
// get the number of up-sells to iterate through
$upsell_size = $_POST['upsells_size'];
// iterate through up-sell fields
for ($i=1; $i<=$upsell_size; $i++){
// get the product id of the up-sell
$product_id = $_POST['upsell_' . $i];
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}
And here is the CSS for formatting the <div>and the checkboxes. It goes into the style.css file:
#wb-upsell-div {
margin-top: 10px;
margin-bottom: 20px;
}
#wb-upsell-checkboxes{
}
So there's an actual answer to this question, you can add whatever you want inside the add to cart <form> using hooks. For example:
add_action( 'woocommerce_before_add_to_cart_button', 'so_34115452_add_input' );
function so_34115452_add_input(){
echo '<input type="checkbox" name="something"/>' . __( 'Some Checkbox', 'text-domain' );
}

WooCommerce - Making a checkout field required

on my baked goods site, I have WooCommerce installed and another plugin called Order Delivery Date for WooCommerce.
I installed the second plugin so my customers would be able to choose a delivery date for their items, however, I am trying to make the form field a required field. So far, I've just been able to make the field look like a required field, but have not figured out how to make sure that it is actually enforced. Any ideas?
Also, if anyone is familiar with WooCommerce, do you know how I would be able to make it so that customers receive this delivery date information in their order confirmation emails?
Thank you in advance!
My site: www.monpetitfour.com
You should try to had something like that :
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// You can make your own control here
if ( ! $_POST[ 'e_deliverydate' ] )
wc_add_notice( __( 'Please select a delivery date' ), 'error' );
}
For the email, the easiest is to save the meta value ( I think it's already done by your plugin). Then you need to copy the template email (customer-processing-order.php) on your theme and change in the template :
<?php $delivery_date = get_post_meta( $order->id, 'custom_field_date', true);
// If the plugin is well developed, you can't directly use magic getters :
// $delivery_date = $order->e_deliverydate;
// Can only by use if the post meta start with _
?>
Your delivery date is <?php echo $delivery_date ?>
You can also use
date_i18n( woocommerce_date_format(), strtotime( $delivery_date ) );
In order to format the date correctly.
On the code above, you just need to find the name of the custom field used by the plugin ( you can search easily on the table wp_postmeta searching by an existing order (should be _e_deliverydate).
Add the following code to your theme's functions.php file
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['e_deliverydate'] )
wc_add_notice( __( 'Please select a delivery date.' ), 'error' );
}
Now to get the email to show the custom field,
add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');
function my_woocommerce_email_order_meta_keys( $keys ) {
$keys['Delivery Date'] = '_e_deliverydate';
return $keys;
}
EDIT : Seems the field value isn't being saved to the database, try saving it explicitly
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['e_deliverydate'] ) ) {
update_post_meta( $order_id, '_e_deliverydate', sanitize_text_field( $_POST['e_deliverydate'] ) );
}
}

WooCommerce: Add input field to every item in cart

I've been trying to add a single text input field to every item in the cart and submit that user input to product's meta info. It's been 2 days and I haven't succeeded yet.
My objective is to:
Take input from user for every item added to the cart.
Display that input in the order's meta info.
Display that input in confirmation email sent to the customer.
So far, I have copied the template file to my theme and added an input field inside a cell. I'm having trouble with the hooks, learned about hooks I will need from WooCommerce Product Gift Wrap plugin as indicated in this woocommerce issue.
Code I added to the cart.php template copied in my theme directory :
$input_url_data = '<div class="input-url"><input type="text" name="cart-url" value="" title="" class="input-text cart-url text" /></div>';
echo apply_filters( 'woocommerce_add_cart_item_data', $input_url_data, $cart_item_key );
Code I added to my theme's functions.php :
add_filter( 'woocommerce_add_cart_item_data','add_cart_item_data', 10, 2 );
add_filter( 'woocommerce_get_cart_item_from_session','get_cart_item_from_session', 10, 2 );
add_filter( 'woocommerce_get_item_data','get_item_data', 10, 2 );
add_filter( 'woocommerce_add_cart_item','add_cart_item', 10, 1 );
add_action( 'woocommerce_add_order_item_meta','add_order_item_meta', 10, 2 );
function add_cart_item_data( $cart_item_meta, $product_id ) {
$input_url_key = "";
$input_url_data['inputurl'] = $input_url_key;
return $input_url_data;
}
function get_cart_item_from_session( $cart_item, $values ) {
if ( ! empty( $values['inputurl'] ) ) {
$cart_item['inputurl'] = true;
}
return $cart_item;
}
function get_item_data( $item_data, $cart_item ) {
if ( ! empty( $cart_item['inputurl'] ) )
$item_data[] = array(
);
return $item_data;
}
function add_cart_item( $cart_item ) {
if ( ! empty( $cart_item['inputurl'] ) ) {
}
return $cart_item;
}
function add_order_item_meta( $item_id, $cart_item ) {
if ( ! empty( $cart_item['inputurl'] ) )
woocommerce_add_order_item_meta( $item_id, __( 'URL by buyer', 'custom_input_url' ), __( 'Yes', 'custom_input_url' ) );
}
Documentation about hook woocommerce_add_cart_item_data isn't very helpful and I'm stuck at this. How do I proceed?
There is a wordpress plugin called WC Fields Factory for the exact purpose.
You can also achieve this by using the following woocommerce hooks woocommerce_before_add_to_cart_button, woocommerce_add_to_cart, woocommerce_cart_item_name,and 'woocommerce_add_order_item_meta'
like for adding text field to product page
function add_name_on_tshirt_field() {
echo '<table class="variations" cellspacing="0">
<tbody>
<tr>
<td class="label"><label for="color">Name On T-Shirt</label></td>
<td class="value">
<input type="text" name="name-on-tshirt" value="" />
</td>
</tr>
</tbody>
</table>';
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_name_on_tshirt_field' );
For displaying custom field on cart item table use the below
function render_meta_on_cart_item( $title = null, $cart_item = null, $cart_item_key = null ) {
if( $cart_item_key && is_cart() ) {
echo $title. '<dl class="">
<dt class="">Name On T-Shirt : </dt>
<dd class=""><p>'. WC()->session->get( $cart_item_key.'_name_on_tshirt') .'</p></dd>
</dl>';
}else {
echo $title;
}
}
add_filter( 'woocommerce_cart_item_name', 'render_meta_on_cart_item', 1, 3 );
to make your custom meta data on you order details, do some thing like this
function tshirt_order_meta_handler( $item_id, $values, $cart_item_key ) {
wc_add_order_item_meta( $item_id, "name_on_tshirt", WC()->session->get( $cart_item_key.'_name_on_tshirt') );
}
add_action( 'woocommerce_add_order_item_meta', 'tshirt_order_meta_handler', 1, 3 );
for detailed implementation, i have an article about how to do this without using any plugins. http://sarkware.com/how-to-pass-custom-data-to-cart-line-item-in-woocommerce-without-using-plugins/
It's easy. Try searching and reading code of Woocommerce.
This much of code got me to point where I can add Url # Cart. And I can see it in Order review as customer and as admin.
I can't test email because I'm lazy. I'm sorry.
Something like this goes in templates\cart\cart.php (there is need for some more code as it's seperate column)
<td class="product-url">
<?php
$html = sprintf( '<div class="url"><input type="text" name="cart[%s][url]" value="%s" size="4" title="Url" class="input-text url text" /></div>', $cart_item_key, esc_attr( $values['url'] ) );
echo $html;
?>
</td>
Functions.php
// get from session your URL variable and add it to item
add_filter('woocommerce_get_cart_item_from_session', 'cart_item_from_session', 99, 3);
function cart_item_from_session( $data, $values, $key ) {
$data['url'] = isset( $values['url'] ) ? $values['url'] : '';
return $data;
}
// this one does the same as woocommerce_update_cart_action() in plugins\woocommerce\woocommerce-functions.php
// but with your URL variable
// this might not be the best way but it works
add_action( 'init', 'update_cart_action', 9);
function update_cart_action() {
global $woocommerce;
if ( ( ! empty( $_POST['update_cart'] ) || ! empty( $_POST['proceed'] ) ) && $woocommerce->verify_nonce('cart')) {
$cart_totals = isset( $_POST['cart'] ) ? $_POST['cart'] : '';
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if ( isset( $cart_totals[ $cart_item_key ]['url'] ) ) {
$woocommerce->cart->cart_contents[ $cart_item_key ]['url'] = $cart_totals[ $cart_item_key ]['url'];
}
}
}
}
}
// this is in Order summary. It show Url variable under product name. Same place where Variations are shown.
add_filter( 'woocommerce_get_item_data', 'item_data', 10, 2 );
function item_data( $data, $cart_item ) {
if ( isset( $cart_item['url'] ) ) {
$data['url'] = array('name' => 'Url', 'value' => $cart_item['url']);
}
return $data;
}
// this adds Url as meta in Order for item
add_action ('woocommerce_add_order_item_meta', 'add_item_meta', 10, 2);
function add_item_meta( $item_id, $values ) {
woocommerce_add_order_item_meta( $item_id, 'Url', $values['url'] );
}
400$ is nice price.
You can do this fairly easily with the Woocommerce Product Add-ons plugin
From the WooThemes website:
Allow your customers to customise your products by adding new options
such as input boxes, dropdowns or checkboxes. With the Product Add-ons
extension, gift messages, donations, laser engraving and any other
product which may require user input in some way is now an option for
your customers!
Product add-ons supports required fields, textareas, checkboxes,
radios, select boxes, custom price inputs and file upload boxes.
I've used it before to add an additional donation field to a product purchase and display that on the thank you page/receipt email.
It's about $50 US and will get you up and running in no time to add the text input and display the field on thank you page/email like you want. $50 is definitely worth the amount of time you'd save drying to develop this feature yourself.
Here's the flow from the end-users perspective:
End user enters in field data and adds product to cart
When User views cart the data they entered into the custom field displays along with the product
After purchase, end user lands on thank you page and receives receipt email with field data included with the product item.
On the backend:
Create custom field for each product. The option is located on the Add-Ons menu tab.
Create a new Addon Group
Enter a group name (for easier organization)
Add a new option and enter the label (this is what the end user sees and is tied to the product)
Update/Publish product.
For completed orders, this is what you will see in the Order Details admin:
Hope this helps and saves you a ton of time in development!

Resources