Get WooCommerce attribute value (url) to use it as a href link - php

I have an attribute in WooCommerce: demo_url. Every product has it's own URL inserted there.
I have also added a button near ADD TO CART so the user can easily view the product on his head (glasses shop). Clicking this button will redirect the user to outside application. Every glasses will have a different link for clicking.
I have tried something like that:
echo 'Text';
But it is not working at all. I've tried it with different global variables (like $currentday) but to no available.
I desperately need this to work. Could You please help?
Thanks

Instead an using a product attribute you should use a custom field (a custom field in the product pages general settings tabs).
Here is that code:
// Create and display the custom field in product general setting tab
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_field_general_product_fields' );
function add_custom_field_general_product_fields(){
global $post;
$value = get_post_meta( $post->ID, '_demo_url', true );
if(empty($value))
$value = '';
echo '<div class="options_group">';
woocommerce_wp_text_input( array(
'id' => 'demo_url',
'label' => __( 'Demo Url', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the Demo Url.', 'woocommerce' ),
'value' => $value
) );
echo '</div>';
}
// Save the custom field
add_action( 'woocommerce_process_product_meta', 'save_custom_field_general_product_fields' );
function save_custom_field_general_product_fields( $post_id){
// Text Field
$demo_url = $_POST['demo_url'];
if( !empty( $demo_url ) )
update_post_meta( $post_id, '_demo_url', esc_attr( $demo_url ) );
}
Code goes in any php file of your active child theme (or theme) or also in any plugin php file.
This code is tested and works... Then you will get this:
Now you can add in your code:
// If you dont have the product id use this:
$global $product;
$product_id = $product->get_id();
// Getting your custom product demo URL
$demo_url = get_post_meta( $product_id, '_demo_url', true );
// Add it to your button:
echo 'Text';

Related

Save an empty value from a custom Text field in WooCommerce

I am working on a WordPress eCommerce website, with WooCommerce being the chosen Shopping Platform.
I have created a Custom Field, within the WooCommerce Product Dashboard by inserting the following code into the functions.php file:
function product_custom_fields_add(){
echo '<div class="product_custom_field">';
// Minimum Required Custom Letters
woocommerce_wp_text_input(
array(
'id' => '_minimum_engrave_text_option',
'name' => '_minimum_engrave_text_option',
'desc' => __('set custom minimum Lettering text field', 'woocommerce'),
'label' => __('Minimum Letters', 'woocommerce'),
'desc_tip' => 'true'
)
);
echo '</div>';
}
add_action('woocommerce_product_options_advanced', 'product_custom_fields_add');
In order to save the values, I have entered the following code into the functions.php file:
// Save Minimum Required Custom Letters
function woocommerce_product_custom_fields_save1($post_id){
if ( ! empty( $_POST['_minimum_engrave_text_option'] ) )
update_post_meta($post_id, '_minimum_engrave_text_option', esc_attr( $_POST['_minimum_engrave_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save1' );
The above code works when a value is entered into the Custom Field. My problem is that I am unable to successfully save a Blank Value. Whenever I save a Product, the Custom Field either auto populates the Field with '1' or saves a previously entered number.
I tried applying the answer, from a similar question, but could not quote get it to work.
Does anyone know where I am going wrong here?
Try to replace empty() by isset() as a condition in the if statement of your hooked function:
// Save Minimum Required Custom Letters
function woocommerce_product_custom_fields_save1($post_id){
if ( isset( $_POST['_minimum_engrave_text_option'] ) )
update_post_meta($post_id, '_minimum_engrave_text_option', esc_attr( $_POST['_minimum_engrave_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save1' );
Now this will works

Woocommerce Products Custom Field

I have added custom fields to my woocommerce products and have been able to enter and fill the custom field data on products. I am trying now to use the data from that custom field in a button I am adding on the woocommerce single product page.
The custom field is a URL for the product sample. I am then trying to add a "View Product Sample" button on the woocommerce single product page that navigates to the url entered int he custom field. Here is the code I have:
// Display Fields
add_action( ‘woocommerce_product_options_general_product_data’, ‘woo_add_custom_general_fields’ );
// Save Fields
add_action( ‘woocommerce_process_product_meta’, ‘woo_add_custom_general_fields_save’ );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
// Text Field
woocommerce_wp_text_input(
array(
‘id’ => ‘product_sample’,
‘label’ => __( ‘Sample Product Link’, ‘woocommerce’ ),
‘placeholder’ => ‘http://’,
‘desc_tip’ => ‘true’,
‘description’ => __( ‘Enter the sample product link here.’, ‘woocommerce’ )
)
);
}
function woo_add_custom_general_fields_save( $post_id ){
// Textfield
$woocommerce_text_field = $_POST[‘product_sample’];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, ‘product_sample’, esc_html( $woocommerce_text_field ) );
}
add_action(‘woocommerce_after_add_to_cart_button’,’cmk_additional_button’);
function cmk_additional_button() {
echo '<a href="CONTENT OF CUSTOM FIELD - URL" target="_blank" button
type="submit" class="button sample">View Product Sample</a>';
}
I need help determining how to get my custom field meta data into the link for the button that is echoed at the end of the code.
Thanks for any help anyone can give.
You Can use ACF plugin for it and call the shortcode on single product page
Reference Url:http://blog.adlivetech.com/get-custom-fields-woocomerce/
You need to use get_post_meta() function this way:
add_action( 'woocommerce_after_add_to_cart_button', 'cmk_additional_button' );
function cmk_additional_button() {
global $post;
$url = get_post_meta( $post->ID, 'product_sample', true );
echo 'View Product Sample';
}
Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.
This should work.
In the code of your question you are using ’ and you should replace them by ' instead, to avoid php errors.

How to add more custom field in Linked Product of Woocommerce

Thanks to all developers at StackOverflow.
I want to add more fields in Linked Product Section of Woocommerce. The fields should be similar to Upsell/Crosssell.
My code so far:-
add_action( 'woocommerce_product_options_linked_product_data', 'woocom_general_product_data_custom_field' );
woocommerce_wp_text_input(
array(
'id' => '_upsizing_products',
'label' => __( 'Upsizing Products', 'woocommerce' ),
'placeholder' => 'Upsizing Products',
'desc_tip' => 'true',
'description' => __( 'Select Products Here', 'woocommerce' )
)
);
In the above code i need the combobox, For example when you type 3 characters in input box, it will show a list of matched products which can be selected. Similar to Upsell / Cross Sell.
Please anyone help me implement this custom field. Thanks in Advance.
Edit: Anyone?
There are several things missing in your code above.
The hook you have used on the first line, do not exist. The right hook is called woocommerce_product_options_related
The code where you've designed your custom field, is not inside any function.
You are creating a standard text-field. If you want a "Select Product"-dropdown, you should use another approach.
This should be inside that function that you're using in the hook.
You are missing a hook and a function the actually save the data from your custom field
1. Finding the right hook/action
To find the right hook to use, just search for "woocommerce_product_options_" inside the WoocCommerce plugin, and about 6 PHP-files should appear. One of those files is called "html-product-data-linked-products.php". This file contains all the existing options in that specific WooCommerce section. It also contains the hook used to display those options.
Open the file and check it out. The hook is at the bottom of the page
Full Path:
/wp-content/plugins/woocommerce/includes/admin/metaboxes/views/
2. Creating the dropdown
To create a select-dropdown including a product search, you need a much different code than the one above.
The spare some time, you can just copy-paste one of the existing options, in the file mentioned above, and then modify it, to your needs.
All this should be placed inside a function called: woocom_linked_products_data_custom_field().
2.1. Modify the ID/name
The first things you need to modify in the code, is of course the unique ID/name of the field.
This is placed in the label-tag (for) and the select-tag (id and name).
In your example the ID/name should be upsizing_products and the label-text Upsizing Products:
<label for="upsizing_products"><?php _e( 'Upsizing Product', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;" id="upsizing_products" name="upsizing_products[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
Note: Don't forget to put an [] and the end of the name-tag, or your data will not be stored.
2.2. Show already chosen products
The next thing, is to show and highligt already chosen products in the WooCommerce section and the dropdown it self.
To do this replace the $product_ids-variable and the entire line with:
$product_ids = get_post_meta( $post->ID, '_upsizing_products_ids', true );
With this instead, the product id's is retrieved from your custom field in the database instead of one of the existing options (cross_sell_ids for example).
Note: The _upsizing_products_ids is the meta-key name in the database. The meta-value related to this key, contains all your field data. This is used to store and retrieve the custom field.
2.3. Display the field in the WooCommerce section
Lastly, the function should be properly hooked, so it can be displayed in the Linked Products section:
add_action( 'woocommerce_product_options_related', 'woocom_linked_products_data_custom_field' );
3. Save and store the data
Now your custom field is displayed inside the right section. The next thing is to save and store the data in the database.
Inside a new function, use $_POST to retrieve the data from the field, and update_post_meta to store the data in the database containing the post-ID, the unique field-id/name (meta-key) and the data it self (meta-value).
function woocom_linked_products_data_custom_field_save( $post_id ){
$product_field_type = $_POST['upsizing_products'];
update_post_meta( $post_id, '_upsizing_products_ids', $product_field_type );
}
add_action( 'woocommerce_process_product_meta', 'woocom_linked_products_data_custom_field_save' );
Here's the full code. Put it inside your themes functions.php or a plugin file:
// Display the custom fields in the "Linked Products" section
add_action( 'woocommerce_product_options_related', 'woocom_linked_products_data_custom_field' );
// Save to custom fields
add_action( 'woocommerce_process_product_meta', 'woocom_linked_products_data_custom_field_save' );
// Function to generate the custom fields
function woocom_linked_products_data_custom_field() {
global $woocommerce, $post;
?>
<p class="form-field">
<label for="upsizing_products"><?php _e( 'Upsizing Product', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;" id="upsizing_products" name="upsizing_products[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
<?php
$product_ids = get_post_meta( $post->ID, '_upsizing_products_ids', true );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select> <?php echo wc_help_tip( __( 'Select Products Here.', 'woocommerce' ) ); ?>
</p>
<?php
}
// Function the save the custom fields
function woocom_linked_products_data_custom_field_save( $post_id ){
$product_field_type = $_POST['upsizing_products'];
update_post_meta( $post_id, '_upsizing_products_ids', $product_field_type );
}
To display the stored data use _upsizing_products_ids:
echo get_post_meta( $post->ID, 'my-field-slug', true );
Check out this guide Mastering WooCommerce Products
Custom Fields for more information about Custom Fields for WooCommerce.

Wordpress WooCommerce store - Subtitle under Products Titles

I've been reading WP forums and trying different plugins for over a week now with no luck, so I decided to give it a try here.
I'm creating a WP website with a premium theme, that supports a woocommerce. What I need to do is the following:
Create a subtitle area (which would be named REG. NO:), so I could not only write Title of the product, but subtitle also. So when you would open a single product page, it would be like:
This_is_my_product_title
REG.NO: this_is_my_reg_no
another issue is, I would need a REG.NO: (subtitle) to be an external hyperlink to a different website.
Greatest thanks to anyone who could help me out.
If you want to go pure WooCommerce way, here's the gist.
1 - Add custom field ( this code goes in functions.php )
add_action( 'woocommerce_product_options_general_product_data', 'my_custom_field' );
function my_custom_field() {
woocommerce_wp_text_input(
array(
'id' => '_subtitle',
'label' => __( 'Subtitle', 'woocommerce' ),
'placeholder' => 'Subtitle....',
'description' => __( 'Enter the subtitle.', 'woocommerce' )
)
);
}
The field will appear as shown in this screen grab : http://i.imgur.com/fGC86DA.jpg
2 - Save the field's data when product is saved. ( this code goes in functions.php )
add_action( 'woocommerce_process_product_meta', 'my_custom_field_save' );
function my_custom_field_save( $post_id ){
$subtitle = $_POST['_subtitle'];
if( !empty( $subtitle ) )
update_post_meta( $post_id, '_subtitle', esc_attr( $subtitle ) );
}
3 - Edit single product template and display the field's value
<?php
global $post;
echo get_post_meta( $post->ID, '_subtitle', true );
?>
Ok so for everyone else who might have the same problem. Although both of options posted already are worth considering and will definitely save them as favorites because I'm sure I will need this in the future, this is the solution that worked best for me.
Although I'm trying to use as few plugins as possible, I ultimately decided to go with KIA SUBTITLE plugin. Then, you have to write this code in your functions.php:
function kia_add_subtitle_link_to_woocommerce(){
if( function_exists( 'the_subtitle' ) ){
$link = the_subtitle( '<h2 class="subtitle">', '</h2>', false );
printf( $link, get_permalink(), sprintf( __( 'Permalink to %s', 'your-text-domain' ), get_the_title() ) );
}
}
add_action( 'some_custom_hook', 'kia_add_subtitle_link_to_woocommerce' );
I used the following hook:
add_action( 'woocommerce_single_product_summary', 'kia_add_subtitle_link_to_woocommerce' );
You can use Advanced custom field plugin to create extra field in add product for REG NO
and you simple get field value on single page using the_field('name_u_give')
or you can also add post meta for post type product

Wordpress - Woocommerce

I found some custom fields turorial for WP and Woocommerce. So I played a bit with that. It all works ok, but I tried to customize part that saves values during checkbox checking, and unfortunately I'm not able to finish that. This is what I'm trying, piece of code in functions.php:
// Custom field for price labels
// Display Fields
add_action(
'woocommerce_product_options_general_product_data',
'woo_add_custom_general_fields' );
// Save Fields
add_action(
'woocommerce_process_product_meta',
'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
// Checkbox
woocommerce_wp_checkbox(
array(
'id' => '_primer',
'wrapper_class' => '',
'label' => __('Primer', 'woocommerce' ),
'description' => __( 'Check me!', 'woocommerce' )
)
);
}
function woo_add_custom_general_fields_save( $post_id ){
// Checkbox_1
$woocommerce_checkbox_primer = isset( $_POST['_primer'] ) ? 'yes' : '';
update_post_meta( $post_id, '_primer', $woocommerce_checkbox_primer );
}
...and then this peace of code in single-product.php
<span><?php echo get_post_meta( get_the_ID(), '_primer', true ); ?></span>
So when this is implemented I have one big "yes" on single product page, but what I want to get is image instead of that "yes", so if checkbox is checked to show image on single product page, and to save that selection in database. I hope I was clear enough, and thanks in advance.
I'm not 100% sure that I understand but it sounded like you wanted to show an image conditionally if the checkbox was true in the backend
if( 'yes' == get_post_meta( get_the_ID(), '_primer', true ) ) { ?>
<img src="your-image.png"/>
<?php }

Categories