custom field in woocommerce category meta

#73640
  • Resolved comagic integra
    Rank Math free

    Some time ago you helped me with custom variables. It was appended to text in custom field in category description. And all was working fine.
    After deprecating register_var_replacement I got an error about it. So I changed my code to:

    
    add_action('rank_math/vars/register_extra_replacements', function () {    
        rank_math_register_var_replacement(
          'CustomCatPrice',
          [
            'name'        => esc_html__( 'Custom cat_price', 'rank-math' ),
            'description' => esc_html__( 'Custom price for current category', 'rank-math' ),
            'variable'    => 'CustomCatPrice',
            'example'     => RMCustomfieldCallback(),
          ],
          'RMCustomfieldCallback'
        );   
    });
    function RMCustomfieldCallback(){	    
    		$term = get_queried_object();
            if ( $term && ! empty(get_term_meta( $term->term_id, 'cat_price', true ) ) ) {
    			return wc_format_content( htmlspecialchars_decode( get_term_meta( $term->term_id, 'cat_price', true ) ) );
    		}	
    
    }
    
    function cat_widget_init() {
    register_sidebar(array(
       'name' => 'cat widget',
       'id' => 'cat_widget',
       'description' => 'cat widget',
       'before_widget' => '<div class="cat widget">',
       'after_widget' => '</div>',
       'before_title' => '<h3>',
       'after_title' => '</h3>',
     )
    );
    }
    add_action( 'widgets_init', 'cat_widget_init' );
    add_action( 'woocommerce_before_shop_loop', 'before_shop_loop' );
    function before_shop_loop() {
    	if ( is_active_sidebar( 'cat_widget' ) ) : ?> 
    	<div id="before_loop_banner" class="before_loop_banner_sidebar"> 
    		<?php dynamic_sidebar( 'cat_widget' ); ?> 
    	</div> 
    	<?php endif; 
    }
    add_action( 'woocommerce_after_shop_loop', 'after_shop_loop' );
    function after_shop_loop() {
    	$term_id = get_queried_object()->term_id;			 
    	$post_id = 'product_cat_'.$term_id;			 
    	$custom_field = get_field('custom_category_descr', $post_id);	
            //$custom_field = "sometext";			
    	echo "<div class='custom_descr'>".$custom_field."</div>";
    }
    

    Old error disappear, but now I got an error:
    Fatal error: Uncaught Error: Call to undefined function get_field() in /home/o/oublieg2/dev2.iphonesmart.ru/public_html/wp-content/themes/page-builder-framework-child/functions.php:253 Stack trace: #0 /home/o/oublieg2/dev2.iphonesmart.ru/public_html/wp-includes/class-wp-hook.php(287): after_shop_loop(”) #1 /home/o/oublieg2/dev2.iphonesmart.ru/public_html/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array) #2 /home/o/oublieg2/dev2.iphonesmart.ru/public_html/wp-includes/plugin.php(478): WP_Hook->do_action(Array) #3 /home/o/oublieg2/dev2.iphonesmart.ru/public_html/wp-content/plugins/woocommerce/templates/archive-product.php(81): do_action(‘woocommerce_aft…’) #4 /home/o/oublieg2/dev2.iphonesmart.ru/public_html/wp-content/plugins/woocommerce/includes/wc-core-functions.php(344): include(‘/home/o/oublieg…’) #5 /home/o/oublieg2/dev2.iphonesmart.ru/public_html/wp-content/plugins/woocommerce/templates/taxonomy-product_cat.php(22): wc_get_template(‘archive-product…’) #6 /home/o/oublieg2/dev2.iphonesmart.ru in /home/o/oublieg2/dev2.iphonesmart.ru/public_html/wp-content/themes/page-builder-framework-child/functions.php on line 253

    When I return static content instead of custom category description – all working fine. It means that mistake somewhere in my new code. Please, help me me to find this mistake.

Viewing 1 replies (of 1 total)
  • Hello,

    Thank you for contacting Rank Math

    You seem to have marked this as resolved. Please let us know if you still need our assistance.

    Looking forward to helping you. Thank you.

    ​​​​​​​

Viewing 1 replies (of 1 total)

You must be logged in to reply to this ticket.