custom field in woocommerce category meta

#9962
  • Resolved comagic integra
    Rank Math free

    I created a custom field cat_price and writing it to WC through update_woocommerce_term_meta( $term_id, 'cat_price', esc_attr( $_POST['cat_price'] ) );
    How I can add this field value to category meta title and description?
    After adding %customfield(cat_price)% to meta input I got a js error in console:
    Uncaught TypeError: Cannot use 'in' operator to search for 'cat_price' in undefined
    and zero output in frontend.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello there,

    We might need to take a closer look at the settings. Please edit the first post on this ticket and include your WP logins in the designated Sensitive Data section.
    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    We really look forward to helping you.

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    Temp login added to sensitive data.

    Hi there,

    Thanks for the follow up.

    The %customfield(field-name)% variable replacement is designed to only work when loading values that are assigned to posts. In your use case, you are loading the custom field from the current term and this is why it does not work.
    I have gone ahead and registered the following custom variable replacement on your theme that will work on the frontend using the variable: %CustomCatPrice%:

    add_action( 'rank_math/vars/register_extra_replacements', function() {
       RankMath\Helper::register_var_replacement( 'CustomCatPrice','RMCustomfieldCallback',$args = array(
    	        'name'    => esc_html__( 'Custom cat_price', 'rank-math' ),
    			'desc'    => esc_html__( 'Custom price for current category', 'rank-math' ),
    			'example' => esc_html__(RMCustomfieldCallback())));
    	
    	
    } );
    
    function RMCustomfieldCallback(){
    	    
    		$term = get_queried_object();
            if ( $term && ! empty( get_woocommerce_term_meta( $term->term_id, 'cat_price', true ) ) ) {
    			return wc_format_content( htmlspecialchars_decode( get_woocommerce_term_meta( $term->term_id, 'cat_price', true ) ) );
    		}
    	
    
    }
    

    I hope this info helps. Thank you.

    Thanks so much for your help!

    Hello,

    We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/?rate=5#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

Viewing 6 replies - 1 through 6 (of 6 total)

The ticket ‘custom field in woocommerce category meta’ is closed to new replies.