I am trying to excute some inpute fields if items are in woocommerce cart, using the code below I am getting amn extra array which i don't need, I want to to add First name and Last name and put them in this loop so I can repeat these 2 fields (first/last name) for each item in cart.
function vicode_custom_checkout_fields($fields){
$counter=0;
$fields = array();
foreach(WC()->cart->get_cart() as $cart_item ){
$fields['vicodemedia_extra_fields'] = array(
'vicodemedia_add_field' => array(
'type' => 'text',
'label' => __( 'First Name' ),
'class' => 'form-row-first'
),
);
$counter++;
}
}
add_filter( 'woocommerce_checkout_fields', 'vicode_custom_checkout_fields' );
Related
I want to show a custom checkbox during checkout that will only show for certain SKUs or Product categories. I already have this code that shows the checkbox on all checkout pages
add_action( 'woocommerce_review_order_before_submit', 'bt_add_checkout_checkbox', 10 );
/**
* Add WooCommerce additional Checkbox checkout field
*/
function bt_add_checkout_checkbox() {
woocommerce_form_field( 'checkout_checkbox', array( // CSS ID
'type' => 'checkbox',
'class' => array('form-row mycheckbox'), // CSS Class
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => true, // Mandatory or Optional
'label' => 'Custom label', // Label and Link
));
}
add_action( 'woocommerce_checkout_process', 'bt_add_checkout_checkbox_warning' );
/**
* Alert if checkbox not checked
*/
function bt_add_checkout_checkbox_warning() {
if ( ! (int) isset( $_POST['checkout_checkbox'] ) ) {
wc_add_notice( __( 'Please acknowledge the Checkbox' ), 'error' );
}
}
I don't know much about PHP but I think I must use $cart_item and $product within a foreach loop and an if statement but I'm quite lost with this part, my logic says it would be something like this:
function bt_add_checkout_checkbox() {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ){
$product = $cart_item['data'];
$sku = WC()->cart->get_sku($product);
if ($sku == 'SA300ARS'){
woocommerce_form_field( 'checkout_checkbox', array( // CSS ID
'type' => 'checkbox',
'class' => array('form-row mycheckbox'), // CSS Class
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => true, // Mandatory or Optional
'label' => 'I acknowledge that my product can take up to 24 hours to be delivered. (Unless the description says otherwise)', // Label and Link
));
}
}
}
You're going in the right direction. You just need to modify the code like this:
add_action( 'woocommerce_review_order_before_submit', 'bt_add_checkout_checkbox', 10 );
/**
* Add WooCommerce additional Checkbox checkout field
*/
function bt_add_checkout_checkbox() {
//Check if wooCommerce is activated
if ( class_exists( 'WooCommerce' ) ) {
//Define SKUs you want to check for
$checkSKUs = ['sku1', 'sku2', 'sku3'];
//Grab all the SKUs in cart
$skus = array();
foreach( WC()->cart->get_cart() as $cart_item ) {
array_push($skus, $cart_item['data']->get_sku());
}
//Check if anything matches in both
$matchingResult = array_intersect($checkSKUs,$skus);
if (count($matchingResult) > 0) {
//If at least 1 SKU matches then generate checkout field
woocommerce_form_field( 'checkout_checkbox', array( // CSS ID
'type' => 'checkbox',
'class' => array('form-row mycheckbox'), // CSS Class
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => true, // Mandatory or Optional
'label' => 'I acknowledge that my product can take up to 24 hours to be delivered. (Unless the description says otherwise)', // Label and Link
));
}
}
}
add_action( 'woocommerce_checkout_process', 'bt_add_checkout_checkbox_warning' );
/**
* Alert if checkbox not checked
*/
function bt_add_checkout_checkbox_warning() {
if ( ! (int) isset( $_POST['checkout_checkbox'] ) ) {
wc_add_notice( __( 'Please acknowledge the Checkbox' ), 'error' );
}
}
I believe this should work. If not then let me know.
I've added comments for your understanding.
I have the following code working to create the Additional information tab in woocommerce with my own values however I want to pull the information from the built-in product custom field.
add_filter( 'woocommerce_display_product_attributes', 'custom_product_additional_information', 10, 2 );
function custom_product_additional_information( $product_attributes, $product ) {
// First row
$product_attributes[ 'attribute_' . 'custom-one' ] = array(
'label' => __('Label One'),
'value' => __('Value 1'),
);
// Second row
$product_attributes[ 'attribute_' . 'custom-two' ] = array(
'label' => __('Label Two'),
'value' => __('Value 2'),
);
return $product_attributes;
}
This is the current code I use to output the custom field, how do I output this in Value 1 in functions.php file in the working code above?
echo get_post_meta( get_the_ID(), 'Size', true );
Try the following replacing in your code:
'value' => __('Value 1'),
with:
'value' => $product->get_meta('Size'),
It should work.
I'm trying to add a custom field to the single product page for products in a specific category. I'm having problems with the conditional logic tho.
This is what I've got so far:
function cfwc_create_custom_field() {
global $product;
$terms = get_the_terms( $product->get_id(), 'product_cat' );
if (in_array("tau-ende", $terms)) {
$args = array(
'id' => 'custom_text_field_title',
'label' => __( 'Custom Text Field Title', 'cfwc' ),
'class' => 'cfwc-custom-field',
'desc_tip' => true,
'description' => __( 'Enter the title of your custom text field.', 'ctwc' ),);
woocommerce_wp_text_input( $args );
}}
The function works but not the if-statement. Does anyone have an idea what I'm doing wrong?
Try the following instead, using a foreach loop iterating through term objects:
function cfwc_create_custom_field() {
global $product;
$terms = get_the_terms( $product->get_id(), 'product_cat' );
// Loop through term objects
foreach( $terms as $term ) {
if ( "tau-ende" === $term->slug ) {
woocommerce_wp_text_input( array(
'id' => 'custom_text_field_title',
'label' => __( 'Custom Text Field Title', 'cfwc' ),
'class' => 'cfwc-custom-field',
'desc_tip' => true,
'description' => __( 'Enter the title of your custom text field.', 'ctwc' ),
) );
break; // The term match, we stop the loop.
}
}
}
When a term match, we stop the loop to have just one custom field… It should work now.
get_the_terms(id, taxonomy)
This function returns an array of WP_Term objects and not string names of the terms. Hence the if condition, which uses in_array function.
If you want to check whether the given name is in the terms, you can probably do it like this -
$cond = false;
foreach($terms as $term) {
if ($term->slug == "tau-ende"){ // You can also match using $term->name
$cond = true;
}
}
I am using the following code to check if a product ID is in the cart, and if so, add extra checkout fields:
add_action('woocommerce_after_order_notes', 'conditional_checkout_field');
function conditional_checkout_field( $checkout ) {
echo '<div id="conditional_checkout_field">';
$product_id = 326;
$product_cart_id = WC()->cart->generate_cart_id( $product_id );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
// Check if the product is in the cart and show the custom field if it is
if ($in_cart ) {
echo '<h3>'.__('Products in your cart require the following information').'</h3>';
woocommerce_form_field( 'custom_field_license', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('License Number'),
'placeholder' => __('Placeholder to help describe what you are looking for'),
), $checkout->get_value( 'custom_field_license' ));
}
}
This works just fine. However, how do I check for multiple product ID's in the cart? For instance, if product ID 326 or 245 are in the cart, show the conditional checkout fields? I feel like it is probably something simple, but I'm not sure how to go about doing it.
I have make some changes in your function to get it work for many product IDs. Also I have added the required option to the field. So your code sould be something like:
add_action('woocommerce_after_order_notes', 'conditional_checkout_field', 10, 1);
function conditional_checkout_field( $checkout ) {
// Set here your product IDS (in the array)
$product_ids = array( 37, 53, 70 );
$is_in_cart = false;
// Iterating through cart items and check
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item )
if( in_array( $cart_item['data']->get_id(), $product_ids ) ){
$is_in_cart = true; // We set it to "true"
break; // At east one product, we stop the loop
}
// If condition match we display the field
if( $is_in_cart ){
echo '<div id="conditional_checkout_field">
<h3 class="field-license-heading">'.__('Products in your cart require the following information').'</h3>';
woocommerce_form_field( 'custom_field_license', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'required' => true, // Added required
'label' => __('License Number'),
'placeholder' => __('Placeholder to help describe what you are looking for'),
), $checkout->get_value( 'custom_field_license' ));
echo '</div>';
}
}
Code goes in function.php file of your active child theme (active theme or in any plugin file).
This code is tested and works.
So I've got the following code which makes me add a Barcode field to the Inventory Options of a product.
Now I also want to add this to each variations so I can easily add Variation Products when I scan the Barcode of the product via the WooCommerce Point of Sale plugin.
Here is what I got currently:
// Add Barcode field in simple product inventory options
add_action('woocommerce_product_options_sku','add_barcode',10,0);
function add_barcode(){
global $woocommerce,$post;
woocommerce_wp_text_input(
array(
'id' => '_barcode',
'label' => __('Barcode','woocommerce'),
'placeholder' => 'Scan Barcode',
'desc_tip' => 'true',
'description' => __('Scan barcode.','woocommerce'),
'value' => get_post_meta($post->ID,'_barcode',true)
)
);
}
// Save Barcode field value for simple product inventory options
add_action('woocommerce_process_product_meta','save_barcode',10,1);
function save_barcode($post_id){
if(!empty($_POST['_barcode']))
update_post_meta($post_id,'_barcode',sanitize_text_field($_POST['_barcode']));
}
// Add a Barcode field in product variations options
add_action('woocommerce_product_after_variable_attributes','add_barcode_variations',10,3);
function add_barcode_variations($loop,$variation_data,$variation){
woocommerce_wp_text_input(
array(
'id' => '_barcode[' . $variation->ID . ']',
'label' => __('Variation Barcode','woocommerce'),
'placeholder' => 'Scan Barcode',
'desc_tip' => 'true',
'description' => __('Scan barcode.','woocommerce'),
'value' => get_post_meta($variation->ID,'_barcode',true)
)
);
}
// Save Barcode field for product variations options
add_action( 'woocommerce_save_product_variation','save_barcode_variations',10,2);
function save_barcode_variations($post_id){
$barcode = $_POST['_barcode'][$post_id];
if(!empty($barcode)) update_post_meta($post_id,'_barcode',sanitize_text_field($barcode));
}
// Set POS Custom Code
add_filter('woocommerce_pos_barcode_meta_key','pos_barcode_field');
function pos_barcode_field(){
return '_barcode';
}
But the problem here is, that with that I now added a part for the variation, that if I update the product the main barcode field in the Inventory settings shows "Array" instead of the provided barcode.
I assume that this has something to do with the ID being the same for the variations as the original field other than the variationID at the end. The reason the ID requires to be the same as the WooCommerce POS plugin I'm using, is being filtered on that ID when I scan a product.
But currently can't figure out, to what I have to change to make both the Inventory Barcode Field and the Variation Barcode field to be saved properly.
As well as I'd like to add the variation field below the variation SKU field, but can't directly find the proper hook to do this.
Thanks in advance for further information.
In your last hooked function you have a missing argument, which is a similar to $loop argument in your 3rd function. So I have made little changes in your code:
// Add product Barcode custom field
add_action('woocommerce_product_options_sku','add_barcode_custom_field' );
function add_barcode_custom_field(){
woocommerce_wp_text_input( array(
'id' => '_barcode',
'label' => __('Barcode','woocommerce'),
'placeholder' => 'Scan Barcode',
'desc_tip' => 'true',
'description' => __('This is the Scan barcode field for this product.','woocommerce')
) );
}
// Save product Barcode custom field
add_action( 'woocommerce_process_product_meta', 'save_barcode_custom_field', 10, 1 );
function save_barcode_custom_field( $post_id ){
if( isset($_POST['_barcode']) )
update_post_meta( $post_id, '_barcode', esc_attr( $_POST['_barcode'] ) );
}
// Add Variation Barcode custom field
add_action( 'woocommerce_variation_options_pricing', 'add_barcode_variation_custom_field', 10, 3 );
function add_barcode_variation_custom_field( $loop, $variation_data, $variation ){
$variation_barcode = get_post_meta($variation->ID,"_barcode", true );
if( ! $variation_barcode ) $variation_barcode = "";
woocommerce_wp_text_input( array(
'id' => '_barcode['.$loop.']',
'label' => __('Variation Barcode','woocommerce'),
'placeholder' => 'Scan Barcode',
'desc_tip' => 'true',
'description' => __('This is the Scan barcode field for this variation.','woocommerce'),
'value' => get_post_meta($variation->ID,"_barcode", true ),
) );
}
// Save Variation Barcode custom field value
add_action( 'woocommerce_save_product_variation', 'save_barcode_variation_custom_field', 10, 2 );
function save_barcode_variation_custom_field( $variation_id, $i ){
if( isset($_POST['_barcode'][$i]) )
update_post_meta( $variation_id, '_barcode', sanitize_text_field($_POST['_barcode'][$i]) );
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for WooCommerce version 2.6+ and 3.0+