Display ACF custom field in WooCommerce product variations - php

I successfully created a ACF custom field in WooCommerce within my product variations in the backend. The field is properly shown within each variation.
But after editing this or other fields within the variations I can't save the whole variations tab anymore. The loading/saving indicator circle continues to rotate indefinitely. And the custom field is not showing in the single product variation in frontend.
What I did, was to add the following code to my functions.php:
/* ACF filter for Variations */
// Render fields at the bottom of variations - does not account for field group order or placement.
$GLOBALS['wc_loop_variation_id'] = null;
function is_field_group_for_variation($field_group, $variation_data, $variation_post) {
return (preg_match( '/Variation/i', $field_group['title'] ) == true);
}
add_action( 'woocommerce_product_after_variable_attributes', function( $loop_index, $variation_data, $variation_post ) {
$GLOBALS['wc_loop_variation_id'] = $variation_post->ID;
foreach ( acf_get_field_groups() as $field_group ) {
if ( is_field_group_for_variation( $field_group, $variation_data, $variation_post ) ) {
acf_render_fields( $variation_post->ID, acf_get_fields( $field_group ) );
}
}
$GLOBALS['wc_loop_variation_id'] = null;
}, 10, 3 );
add_action( 'woocommerce_save_product_variation', function( $variation_id, $loop_index ) {
if ( !isset( $_POST['acf_variation'][$variation_id] ) ) {
return;
}
$_POST['acf'] = $_POST['acf_variation'][$variation_id];
acf()->input->save_post( $variation_id );
}, 10, 2 );
add_filter( 'acf/prepare_field', function ( $field ) {
if ( !$GLOBALS['wc_loop_variation_id'] ) {
return $field;
}
$field['name'] = preg_replace( '/^acf\[/', 'acf_variation[' . $GLOBALS['wc_loop_variation_id'] . '][', $field['name'] );
return $field;
}, 10, 1);
//add ACF rule
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';
//print_r($choices);
return $choices;
}
/* End */
Any help would be appreciated.

acf()->input->save_post( $variation_id );
Should be
do_action( 'acf/save_post', $variation_id );
Credit to onoweb from here https://support.advancedcustomfields.com/forums/topic/acf-on-product-variations-almost-works/

Related

Automatically add custom order shipping item meta data in WooCommerce

I know how to add a meta on the order shipping method :
$shippingMethodItem->update_meta_data('num_packages', 0);
But I want to add/update this meta automatically when the order is placed OR when someone add/edit order items manually.
I already tried with no result :
add_action( 'woocommerce_checkout_create_order_shipping_item', [$this, 'actionCheckoutCreateOrderShippingItem'] );
...
public function actionCheckoutCreateOrderShippingItem ($shippingMethodItem, $package_key, $package, $order)
{
$shippingMethodItem->update_meta_data( 'num_packages', 0);
$shippingMethodItem->save();
}
The following will add custom specific order "shipping" item meta data on orders via checkout and/or manual admin orders too:
// For new orders via checkout
add_action( 'woocommerce_checkout_create_order_shipping_item', 'action_checkout_create_order_shipping_item', 10, 4 );
function action_checkout_create_order_shipping_item ( $item, $package_key, $package, $order ){
$item->update_meta_data( 'num_packages', 0 );
}
// For manual admin orders
add_action( 'woocommerce_saved_order_items', 'action_saved_order_items_callback', 10, 2 );
function action_saved_order_items_callback( $order_id, $items ) {
if ( isset( $items['shipping_method_id'] ) ) {
foreach( $items['shipping_method_id'] as $item_id ) {
$num_packages = wc_get_order_item_meta($item_id, 'num_packages');
if ( empty($num_packages) || $num_packages != 0 ) {
wc_update_order_item_meta( $item_id, 'num_packages', 0 );
}
}
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
To use this code in a plugin with a class (OOP), add the following inside the constructor function:
// For checkout order
add_action( 'woocommerce_checkout_create_order_shipping_item', [$this, 'action_checkout_create_order_shipping_item'], 10, 4 );
// For checkout order
add_action( 'woocommerce_saved_order_items', [$this, 'action_saved_order_items_callback'], 10, 2 );
And the following outside the constructor function:
public function action_checkout_create_order_shipping_item ( $item, $package_key, $package, $order ){
$item->update_meta_data( 'num_packages', 0 );
}
public function action_saved_order_items_callback( $order_id, $items ) {
if ( isset( $items['shipping_method_id'] ) ) {
foreach( $items['shipping_method_id'] as $item_id ) {
$num_packages = wc_get_order_item_meta($item_id, 'num_packages');
if ( empty($num_packages) || $num_packages != 0 ) {
wc_update_order_item_meta( $item_id, 'num_packages', 0 );
}
}
}
}

About Woocommerce product insertion hooks

I added a field to the product display page. I did some research but was not successful.
I have a form element like the one below, it may be text in this picture.
https://prnt.sc/vql93m
I want to save a field as a post meta while creating the product.
Then I want to show the relevant meta on the order page, actually I did it partially, but I can't save the relevant data.
https://prnt.sc/vqlbfs
And I can only add to cart on the single product display page, all other sections should be removed.
https://prnt.sc/vql8mj
Hooks and codes I used.
add_action('woocommerce_before_add_to_cart_button', 'woocommerce_custom_fields_display');
function woocommerce_custom_fields_display()
{
?>
<label>Product type:</label><br><input type="text" id="product-type" name="product-type"><br><br>
<?php
}
add_action( 'woocommerce_before_order_itemmeta', 'so_32457241_before_order_itemmeta', 10, 3 );
function so_32457241_before_order_itemmeta( $item_id, $item, $_product ){
$order = json_decode( $item );
echo get_post_meta( $order['order_id'], 'product-type', true );
}
add_action( 'woocommerce_new_order', 'custom_woocommerce_order' );
function custom_woocommerce_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
var_dump( $order );
var_dump( $_POST );
}
add_action('woocommerce_add_to_cart', 'custome_add_to_cart');
function custome_add_to_cart() {
$p_id=$_POST['product-type'];
WC()->cart->add_to_cart( $p_id );
}

Display a product custom field value in WooCommerce cart and checkout table

In WooCommerce and I have added a custom field "description" for each product.
I was able to find a way to show both, the label name and the value:
add_filter( 'woocommerce_add_cart_item_data', 'save_days_field', 10, 2 );
function save_days_field( $cart_item_data, $product_id ) {
$special_item = get_post_meta( $product_id , 'description',true );
if(!empty($special_item)) {
$cart_item_data[ 'description' ] = $special_item;
// below statement make sure every add to cart action as unique line item
$cart_item_data['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'description', $special_item );
}
return $cart_item_data;
}
// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data','rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_on_cart_and_checkout( $cart_item_data, $cart_item ) {
if( isset( $cart_item['description'] ) ) {
$cart_item_data[] = array( "name" => __( "Description", "woocommerce" ), "value" => $cart_item['description'] );
}
return $cart_item_data;
}
Now I need to display ONLY the value (not the label name "Description") of this custom field in the cart and checkout table. I need to display with <small>, like the attribute I am displaying with this code:
add_filter('woocommerce_cart_item_name', 'wp_woo_cart_attributes', 10, 2);
function wp_woo_cart_attributes($cart_item, $cart_item_key){
$productId = $cart_item_key['product_id'];
$product = wc_get_product($productId);
$taxonomy = 'pa_color';
$value = $product->get_attribute($taxonomy);
if ($value) {
$label = get_taxonomy($taxonomy)->labels->singular_name;
$cart_item .= "<small>$value</small>";
}
return $cart_item;
}
How can I make it for this custom field, displaying the value only?
You don't need to include a product custom field as custom cart item data, as it's directly accessible from the product object (or the product ID).
Note: On a cart item variable $cart_item, the WC_Product Object is included and available using $cart_item['data'].
Try the following to add a custom field after the item name in cart and checkout pages:
// Display in cart and checkout pages
add_filter( 'woocommerce_cart_item_name', '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('description') ) {
$product_name .= '<small>'.$value.'</small>';
}
return $product_name;
}
To display it on orders and email notifications use:
// Display in orders and email notifications
add_filter( 'woocommerce_order_item_name', 'customizing_order_item_name', 10, 2 );
function customizing_order_item_name( $product_name, $item ) {
$product = $item->get_product(); // Get the WC_Product Object
if ( $value = $product->get_meta('description') ) {
$product_name .= '<small>'.$value.'</small>';
}
return $product_name;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Woocommerce: Hide Attribute based on word in product Title

I am trying to hide product attributes in woocommerce based on product title. currently I have code that removes the attributed based on category. It works fine for what it is, but I would prefer to use title and a strpos array instead.
Here is the code that allows me to remove attributes based on categories
add_action( 'wp', 'remove_product_content11' );
function remove_product_content11() {
if ( has_term( array('Flush Mount', 'Semi Flush'), 'product_cat' ) ) {
function mycode_hide_attributes_from_additional_info_tabs( $attributes, $product ) {
$hidden_attributes = [
'pa_item-length-or-depth',
'pa_item-minimum-height',
];
foreach ( $hidden_attributes as $hidden_attribute ) {
if ( ! isset( $attributes[ $hidden_attribute ] ) ) {
continue;
}
$attribute = $attributes[ $hidden_attribute ];
$attribute->set_visible( false );
}
return $attributes;
}
add_filter( 'woocommerce_product_get_attributes', 'mycode_hide_attributes_from_additional_info_tabs', 20, 2 );
}
}
I would just like to change this code so It uses strpos to search though the product title, instead of using category.
check this code, tell me if it helps.
add_action( 'wp', 'remove_product_content11' );
function remove_product_by_strpos_title() {
global $post;
// check if its a product page, so the code is not executed for every page
// and check if title contains 'my-title'
if ( is_product() && strpos('my-title', $post->post_title) ) {
function mycode_hide_attributes_from_additional_info_tabs( $attributes, $product ) {
$hidden_attributes = [
'pa_item-length-or-depth',
'pa_item-minimum-height',
];
foreach ( $hidden_attributes as $hidden_attribute ) {
if ( ! isset( $attributes[ $hidden_attribute ] ) ) {
continue;
}
$attribute = $attributes[ $hidden_attribute ];
$attribute->set_visible( false );
}
return $attributes;
}
add_filter( 'woocommerce_product_get_attributes', 'mycode_hide_attributes_from_additional_info_tabs', 20, 2 );
}
}

Set a custom field value in cart and save it as order item data in Woocommerce 3

In wooCommerce plugin I want to send $item['_custom_sizer'] this value to check out page and email template but i am not able to pass the value. please help me out.
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_custom_data_vase', 10, 2 );
function add_cart_item_custom_data_vase( $cart_item_meta, $product_id ) {
global $woocommerce;
$new_value = array();
$new_value['_custom_sizer'] = $_POST['sizer'];
if(empty($cart_item_data)) {
return $new_value;
} else {
return array_merge($cart_item_data, $new_value);
}
}
//Get it from the session and add it to the cart variable
function get_cart_items_from_session( $item, $values, $key ) {
if (array_key_exists( '_custom_sizer', $values ) ) {
$item['_custom_sizer'] = $values['_custom_sizer'];
}
return $item;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'get_cart_items_from_session', 1, 3 );
Updated… Try the following instead:
// Add "Sizer" as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_custom_data', 20, 2 );
function add_cart_item_custom_data( $cart_item_data, $product_id ) {
if( isset($_POST['sizer']) && ! empty($_POST['sizer']) ){
$cart_item_data['custom_sizer'] = sanitize_text_field( $_POST['sizer'] );
}
return $cart_item_data;
}
// Display "Sizer" in cart and checkout
add_filter( 'woocommerce_get_item_data', 'display_sizer_in_cart_checkout', 20, 2 );
function display_sizer_in_cart_checkout( $cart_item_data, $cart_item ) {
if( isset($cart_item['custom_sizer']) ){
$cart_item_data[] = array(
'name' => __('Metal Type'),
'value' => $cart_item['custom_sizer'],
);
}
return $cart_item_data;
}
// Save "sizer" as custom order item meta data
add_action( 'woocommerce_checkout_create_order_line_item', 'update_order_item_meta', 20, 4 );
function update_order_item_meta( $item, $cart_item_key, $values, $order ) {
if( isset($values['custom_sizer']) )
$item->update_meta_data( 'Sizer', $values['custom_sizer'] );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Categories