Hi sorry for the delay the replies filtered to my junk for some reason.
I don’t fully understand.
So I have set up featured_page_link as a taxonomy product_cat and selected the category in the page editor. So basically all I want to do is set the link in my template to go to the woocommerce category I have selected within the admin tool.
Thanks,
Kev
Hi, Nayrod!
Have you already created the ‘price_per_kilo’ field in the ACF fields for products?
If you change files in the plugin folder itself, then after the update all edits will disappear. So you need a new folder in your theme folder.
You need to create a new folder in your theme. Like this directory
/ wp-content / themes / your-child-theme / woocommerce / single-product /
Copy the price.php template from the / wp-content / plugins / woocommerce / templates / single-product directory to your new folder (/ wp-content / themes / your-child-theme / woocommerce / single-product /)
Open the price.php template and paste my code (don’t duplicate the global $product).
If the code doesn’t work try this. Оpen the product in the admin panel. An empty “price_per_kilo” field that you created at the beginning should appear. Make sure the price and weight (in delivery) are specified. Save the product. Go to the front-end of the product page, the code should work.
If there are any mistakes, write down which ones. Write down, step by step, what you are doing and in which files.
It works in my local project. Good luck!
If i place acf_form() in a woocommerce checkout hook, do you think it could work ?
https://www.businessbloomer.com/woocommerce-visual-hook-guide-checkout-page/
thanks
Hi Celestial and thank you for your precious help
I tried the code you’ve given to me by creating a file in /wp-content/plugins/woocommerce/templates
Nothing happens in my product page in my dashboard.
I also tried to copy paste this code in Rich Snippets but it tells me there is an error line 3 so I presume it’s not a snippet that I can use with this plugin.
I might do something wrong though …
Howdy, James – great name, btw.
Just upgraded to Pro and looking for the way to hook a Field Group into the dropdown found at Attributes >> Custom product attribute
. Had no issue adding 35 Select dropdowns to WooCommerce Single Products.
But I don’t want them in a separate place like this. I need to add Custom Fields to the Attributes >> Custom product attribute
dropdown. This one.
Ideally, these custom taxonomies would also be automatically added to the Products >> Attributes
page. This one.
Are you able to point me in the right direction to make this happen?
Thanks!
Hi, is there any update or news about repeater field ?
i was able to save normal fields to product variation but when i create repeater field it doesnt save the value, here is my code;
add_filter('acf/location/rule_values/post_type', 'acf_location_rule_values_Post');
function acf_location_rule_values_Post( $choices ) {
$choices['product_variation'] = 'Product Variation';
return $choices;
}
add_action( 'woocommerce_product_after_variable_attributes', function( $loop, $variation_data, $variation ) {
global $abcdefgh_i;
$abcdefgh_i = $loop;
add_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );
$acf_field_groups = acf_get_field_groups();
foreach( $acf_field_groups as $acf_field_group ) {
foreach( $acf_field_group['location'] as $group_locations ) {
foreach( $group_locations as $rule ) {
if( $rule['param'] == 'post_type' && $rule['operator'] == '==' && $rule['value'] == 'product_variation' ) {
acf_render_fields( $variation->ID, acf_get_fields( $acf_field_group ) );
break 2;
}
}
}
}
remove_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );
}, 10, 3 );
function acf_prepare_field_update_field_name( $field ) {
global $abcdefgh_i;
$field['name'] = preg_replace( '/^acf\[/', "acf[$abcdefgh_i][", $field['name'] );
return $field;
}
add_action( 'woocommerce_save_product_variation', function( $variation_id, $i = -1 ) {
if ( ! empty( $_POST['acf'] ) && is_array( $_POST['acf'] ) && array_key_exists( $i, $_POST['acf'] ) && is_array( ( $fields = $_POST['acf'][ $i ] ) ) ) {
foreach ( $fields as $key => $val ) {
update_field( $key, $val, $variation_id );
}
}
}, 10, 2 );
function xxx_admin_head_post() {
global $post_type;
if ($post_type === 'product') {
wp_register_script( 'xxx-acf-variation', get_template_directory_uri() . '/inc/ss.js', array(
'jquery-core',
'jquery-ui-core'
), '1.1.0',
true ); // Custom scripts
wp_enqueue_script( 'xxx-acf-variation' ); // Enqueue it!
}
}
/* actions fired when adding/editing posts or pages */
/* admin_head-(hookname) */
add_action( 'admin_head-post.php', 'xxx_admin_head_post' );
add_action( 'admin_head-post-new.php', 'xxx_admin_head_post' );
My end goal is to be able to use a custom price for specified customers. I’ve figured that part out and the alter_price
function is working. the hard part, making that translate into the cart is where I’m struggling – it seems to be a different ball game.
function alter_price( $price ) {
$customer = get_field('customer',$product->id);
$special_customer_ids = [$customer];
if ( in_array( get_current_user_id(), $special_customer_ids, true ) && (get_field('special_price',$product->id))) {
$price = get_field('special_price');
return $price;
} else {
return $price;
}
}
add_filter( 'woocommerce_product_get_price', 'alter_price', 10, 1 );
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 20, 1);
function add_custom_price( $cart_obj ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Avoiding hook repetition (when using price calculations for example)
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart_obj->get_cart() as $cart_item ) {
$customer = get_field('customer',$product->id);
$special_customer_ids = [$customer];
if ( in_array( get_current_user_id(), $special_customer_ids, true ) && (get_field('special_price',$product->id))) {
$newprice = get_field('special_price');
$cart_item['data']->set_price( $newprice );
}
}
}
I have been looking all over the place and been through several alterations of add_custom_price
. I think I should probably be using the_field
and a product ID. Any help is appreciated.
Hello, I have the same problem.
I Have Polylang for Woocommerce, but the custom fields that I created for product pages are not showing after translating a page.
So the translated page is showing the custom fields in the original language.
It is possible to translate ACF with Polylang for woocommerce?
Thanks for the response.
However, I updated the update_field to:
update_field('affiliate_link', $value ,$object->get_id());
With WooCommerce, I had to use get_id().
I can now programmatically set the custom field value.
Hello, I have some code from a previous dev working on this who made a PDF generator for creating licenses sent to the purchasers email, includes info from a woocommerce order, and contains some info from an ACF form field and subfield, specifically the price. Getting the price to the form works fine.
I added a new row to the form’s field sub-field name type_usage
as I want the PDF to contain usage information. type_price
is also in this section and is used in the PDF. I tried calling type_usage
the same way but nothing comes up and I’ve been stuck.
Here’s the code to the PDF generator
/**
* Generate song license PDF
* @param integer $order_id
* @param integer $product_id
* @param integer $license_number
*/
function generate_song_license_pdf($order_id, $product_id) {
require_once('page-licenses-pdf.php');
$order = wc_get_order( $order_id );
$data = $order->get_data(); // order data
$song_info = get_transient('song_info_'.$product_id);
$license_number = get_post_meta($order_id, 'mp_license', true);
$license_types = implode(', ', $song_info['license_types']);
$product_price = get_post_meta($product_id, '_price',true);
$type_usage = get_post_meta($product_id, '_usage',true); //test 1
$order_date_created = $data['date_created']->date('Y-m-d');
$usage_subfield = get_sub_field('type_usage'); //test 2
//add the new array to the user meta data
$html = "
<h1><u>COMPANY — LICENSE AGREEMENT</u></h1>
<h1>Here is the TYPE USAGE: ".$type_usage." or ".$usage_subfield."</h1>
<p>DATE: <strong>".$order_date_created."</strong> LICENSE No. <strong>".$license_number." </strong></p>
<p>In consideration of the payment and the receipt by the undersigned of the sum of $".$product_price." and other good and valuable considerations as well as all license fees prior to the effectiveness hereof, COMPANY (“Licensor”) does hereby grant to <strong>".$data['billing']['first_name'] ." " . $data['billing']['last_name'] ."</strong> (“Licensee”) as agency of the production specified in paragraph #2 hereof, the non-exclusive, irrevocable right, license, privilege and authority to use the recorded musical selections(s) hereinafter specified, for the sole, limited and restricted purpose of mechanically reproducing such recorded selection(s) only in the specific production hereinafter designated as an integral part thereof, all in accordance with terms, conditions and limitations hereinafter set forth.</p>
<ol>
It’s also been added to my Get License Type Function
/** REST API Licenses's endpoints */
add_action('rest_api_init', function () {
//license-types/(?P<type>\+s)
register_rest_route( 'music-factory/v1', 'license-types/(?P<type>[a-zA-Z0-9-]+)',array(
'methods' => 'GET',
'callback' => 'get_license_variants_by_type'
));
});
/**
* Get License types
* @param $request
* @return JSON $response
*
*/
function get_license_variants_by_type($request) {
$post_id = get_id_by_slug('licenses');
$types = [];
if( have_rows($request['type'], $post_id) ) {
while( have_rows($request['type'], $post_id)) : the_row();
$types[] = [
'type_name'=> get_sub_field('type_name', $post_id),
'type_price'=> get_sub_field('type_price', $post_id),
'type_usage'=> get_sub_field('type_usage', $post_id),
];
endwhile;
}
$response = new WP_REST_Response($types);
$response->set_status(200);
return $response;
}
/**
* Get License types
* @param $request
* @return JSON $response
*
*/
function get_project_types() {
$post_id = get_id_by_slug('licenses');
$project_type = sanitize_text_field($_POST['project_type']);
$types = [];
if( have_rows($project_type, $post_id) ) {
while( have_rows($project_type, $post_id)) : the_row();
$types[] = [
'type_name'=> get_sub_field('type_name', $post_id),
'type_price'=> get_sub_field('type_price', $post_id),
'type_description'=> get_sub_field('type_description', $post_id),
'type_usage'=> get_sub_field('type_usage', $post_id),
];
endwhile;
}
echo json_encode($types);
wp_die();
}
add_action( 'wp_ajax_mb_request_quote', 'mb_request_quote' );
add_action( 'wp_ajax_nopriv_mb_request_quote', 'mb_request_quote' );
add_action('wp_ajax_mb_ajax_add_to_cart', 'mb_ajax_add_to_cart');
add_action('wp_ajax_nopriv_mb_ajax_add_to_cart', 'mb_ajax_add_to_cart');
add_action( 'wp_ajax_get_project_types', 'get_project_types' );
add_action( 'wp_ajax_nopriv_get_project_types', 'get_project_types' );
Any help is appreciated, thank you!
is called
“WooCommerce Product Feed for Google, Bing, eBay and Many More
By WebAppick”
and i cant afford the pro version to get what i need so i am looking for alternatives.
but cant afford the pro version that does what i need. :/
p.s i cant post directly urls here i see. this was my 3rd etempt
https://wordpress.org/plugins/webappick-product-feed-for-woocommerce/
this one, but i cant afford the “pro” version that says “it supports dynamic attributes”..
plugin costs are keep adding in these days. :/
this: https://wordpress.org/plugins/webappick-product-feed-for-woocommerce/#developers
unfortunately cant afford the “pro” version for just 1 site that i need that it says it allows you to “create dynamic” attributes.
i found the solution!
After hours of searching, I found a post on stackoverflow.com
1) create text field in acf. example : text field name is video_url
2) Please enter a youtube video link in acf text field.
3) show anywhere in wordpress template page
Here’s what you are looking for with regards to WooCommerce Memberships and Restricted Content:
if (! wc_memberships_is_post_content_restricted()) {
// show acf content
}
this doesn’t give me an error, but nothing is shown …
function ts_add_iframe_video() {
$output = '<div class="woocommerce-product-gallery images">';
$iframe = get_field('produktvideo-url');
// Use preg_match to find iframe src.
preg_match('/src="(.+?)"/', $iframe, $matches);
$src = $matches[1];
// Add extra parameters to src and replcae HTML.
$params = array(
'controls' => 0,
'hd' => 1,
'autohide' => 1
);
$new_src = add_query_arg($params, $src);
$iframe = str_replace($src, $new_src, $iframe);
// Add extra attributes to iframe HTML.
$attributes = 'frameborder="0"; allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen';
$iframe = str_replace('></iframe>', ' ' . $attributes . '></iframe>', $iframe);
// Replace your iframe code from youtube here.
$output .= 'echo $iframe;';
$output .= '</div>';
echo $output;
}
Hello John, was there any more resolution on this?
I am having the same issue. When I run this code from inside a woocommerce hook it does not work, but when I run the same exact code from inside a function on another page that is not run from inside a woocommerce hook it works fine. The last line of code is not updating that particular meta field that I added with ACF. But again the same exact code is working when run outside of a woocommerce hook. Do you have any guidance on this?
$user_id = get_current_user_id();
$user_pickup_location_key = ‘default_pickup_location’;
$subscription_pickup_location_key = ‘subscription_pickup_location’;
$user_default_pickup_location = get_field($user_pickup_location_key, “user_{$user_id}”);
update_field($subscription_pickup_location_key, $user_default_pickup_location, 6369);
Thank you for your time,
Josh
Hay, I am thinking to install ACF plugin in my woocommerce store, But I mostly use woocommerce shortcodes for customization, So, Does ACF plugin compatible with woocommerce shortcodes?
I didn’t think it was possible with just a function. I’ve just been editing/creating the template files for it.
So in your child theme folder create a sub folder called woocommerce then another sub folder called single-product then inside that another sub folder called tabs.
You can duplicate the files over from the woocommerce plugin, it follows the same file suture. And rename one of the duplicates to your-tab-title.php and there you can pull the data in from ACF.
Also dupliate the tab.php over and add in the new title for that tab too in there
Maybe I should be using the code from Woocommerce’s template archive-product.php
as that page displays products the same way I need to. So maybe i should try that code as a starting point and replacing the appropriate pieces of code with ACF fields (the three products chosen on the product’s edit page.)
Wanted to add that idea which i am still looking into deeper.
Hi @amitbiswas06.
Thank you for your reply. But I don’t think you’ve fully understood what I was after, apologies if I have cause any confusion.
I have created an ACF field called class_dates which is only visible on the product category called ‘courses’.
However, I want to give this information a title so that customers can easily see what this date is. i.e.:
Class Dates & Time (h2 title)
31/12/2020 at 9pm (class date field).
Ive used the ACF shortcode to add this field to the Woocommerce product page template but I can’t pass through the title to the product page just the field text.
@bulldogsnare As I can understand, you have created a date field for the product category taxonomy and you want to display it in your product category archives. Here is one approach to do that –
function tax_date_field(){
// get the current taxonomy term
$term = get_queried_object();
//ACF field
$date = get_field('class_dates', $term);
$class_date = '';
if( is_tax('product_cat') && $date ) {
$class_date = sprintf(
'<div class="class-date"><label>%s</label>%s</div>',
esc_html__('Post Dates:', 'your-themes-text-domain'),
esc_html( $date )
);
}
return $class_date;
}
Now you can echo the function to your product category archives. To do that, WooCommerce have some hooks for the taxonomy archive page which you can find inside archive-product.php
template file inside WooCommerce plugin folder.
Here I am hooking it to woocommerce_before_shop_loop
. In your functions.php –
add_action('woocommerce_before_shop_loop', 'show_post_dates', 11);
function show_post_dates(){
echo tax_date_field();
}
Now the field will be shown at product taxonomy archives. Let me know if you have any questions.
thanks,
this code solved the issue.
add_action('woocommerce_product_options_general_product_data', function() {
acf_form();
});
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.