Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Carlo Cannavò

ACF Column Value

Hi,
after updating plugin to latest version my custom code to add some calculated value on acf column not work in “product variation”

I have add this code:

function add_product_margini_content( $value, $id, AC\Column $column ) {
	if ( is_admin() ) {
		// Check for the ACF column
		if ( $column instanceof ACA\ACF\Column ) {
			
			$meta_key = $column->get_meta_key();
			$meta_type = $column->get_post_type();
			
			if ('cc_etichetta_margine' === $meta_key) {
				
			// Get product object
			$product = wc_get_product( $id );
			$fields = get_post_meta ($id);
			$costo = get_post_meta( $id, '_wc_cog_cost', true  );
			$marginemin = get_field('cc_etichetta_margine');
			$standardprice = $product->get_price();
			
			if (('product' === $column->get_post_type()) && ($product->is_type( 'variable' ))) {
				$prezzomin = 0;
			}else {
				$prezzomin = ($costo*($marginemin/100))+$costo;
			}
			
			/*//$prezzomin = ($costo*($marginemin/100))+$costo;
			if (is_array ($marginemin)){	
				$prezzomin = '';
			} else {
				$prezzomin = ($costo*($marginemin/100))+$costo;
			}*/
			//$value.= $costo."</br>".$marginemin."</br>".$standardprice."</br>".$prezzomin;
			
			if (!empty($marginemin)){
					//Verde
					if (($prezzomin>0) && ($standardprice>$prezzomin)){
						$value .= "</br><span style='font-size:12px; background: #90da8a;padding: 2px;border-radius: 2px;border: 1px solid #c3cac2;'>P.S. | <span style='padding: 5px;color: #fff;font-weight: 600;'>".number_format($prezzomin,2)." </span></span><hr>";
					} 
					//Rosso
					elseif (($prezzomin>0) && ($standardprice<$prezzomin)) {
						$value .= "</br><span style='font-size:12px; background: #f26666; padding: 2px;border-radius: 2px;border: 1px solid #c3cac2;'>P.S. | <span style='padding: 5px;color: #fff; font-weight: 600;'>".number_format($prezzomin,2)." </span></span><hr>";
					} 
					//Arancione
					elseif (($prezzomin>0) && ($standardprice=$prezzomin)) {
						$value .= "</br><span style='font-size:12px; background: #ff9933; padding: 2px;border-radius: 2px;border: 1px solid #c3cac2;'>P.S. | <span style='padding: 5px;color: #fff; font-weight: 600;'>".number_format($prezzomin,2)." </span></span><hr>";
					}
				}
			
			}
		}
	
		return $value;
	}
}
add_filter( 'ac/column/value', 'add_product_margini_content', 10, 3 );

that add some div after acf field “$marginemin = get_field(‘cc_etichetta_margine’);”

But the div is printed only in Product column and not in product variation column.

How can i fix please ?
Thanks

1 year, 8 months ago
Stefan van den Dungen Gronovius
Developer

Based on your snippet, I would expect no difference between the product and product variation. At least not after updating our plugin. Can you make sure that the correct ACF column is loaded on the Product Variation page?

And can you return a value in the hook inside your column conditional, just to be sure that that part is correct? After that, you could debug where in the snippet it goes wrong. Maybe $marginemin is empty. I recommend using the $id in the get_field() method to make sure that the field for the correct ID is loaded. Right now it depends on the global post, but since you have the $ID at your disposal it would be better to provide it to get_field.

I hope that will help you to debug/fix the issue.

1 year, 8 months ago

You must be logged in to reply to this topic.