• Resolved 008iresf

    (@008iresf)


    Hi

    I want to use WooCommerce product attributes in meta description. Just like custom fields, that can specify which custom field we want to load in meta descriptions automatically.

    I found this topic but I don’t know which function of WooCommerce could handle what I want.

    I wrote a code :

    /**
     * A custom replacement variable to get the current month in custom format
     */
    
    add_action( 'rank_math/vars/register_extra_replacements', function(){
     rank_math_register_var_replacement(
     'product_attributes',
     [
     'name'        => esc_html__( 'Product Attributes', 'rank-math' ),
     'description' => esc_html__( 'Current Product Attributes', 'rank-math' ),
     'variable'    => 'product_attributes',
     'example'     => product_attributes_callback(),
     ],
     'product_attributes_callback()'
     );
    });
    
    function product_attributes_callback(){
    	$result = wc_attribute_label( 'pa_gender' );
    	return $result;
    }

    But that just returns the label. I need value of the attribute.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Rank Math Support

    (@rankmathteam)

    Hello @008iresf,

    Thank you for contacting the support.

    Please try using the below code and see if it gives you the desired output:

    add_action( 'rank_math/vars/register_extra_replacements', function(){
    rank_math_register_var_replacement(
    'get_attribute_value',
    [
    'name' => esc_html__( 'Gets the attribute value', 'rank-math' ),
    'description' => esc_html__( 'Current product attribute value', 'rank-math' ),
    'variable' => 'get_attribute_value',
    'example' => 'get_attribute_value_callback()',
    ],
    'get_attribute_value_callback'
    );
    });
    function get_attribute_value_callback() {
    global $product;
    $attribute='';
    
    $attributes = get_the_terms( $product->id, 'pa_gender');
    foreach ( $attributes as $a ) {
    $attribute .= $a->name . ' ';
    }
    return $attribute;
    }

    Here’s how you can add the code to the website:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    After adding, you will need to use the variable %get_attribute_value% to fetch the values of the attribute “pa_gender”.

    Let us know how that goes.

    Thread Starter 008iresf

    (@008iresf)

    Hi

    Thanks for your help.

    I add the code using Code Snippets plugin.
    After adding %get_attribute_value% I got “get_attribute_value_callback()” in output. For example, this image

    After that, I found out there is a mistake in code. In :
    'example' => 'get_attribute_value_callback()'
    you used extra ” for the second part. Correct code is :
    'example' => get_attribute_value_callback(),

    And it works well.
    Thank you

    Plugin Support Rank Math Support

    (@rankmathteam)

    Hello @008iresf,

    The get_attribute_value_callback() that you saw is only shown on the backend/product edit page, and if you’ll check the source in the front end of the website, you’ll see the required output.

    However, removing the quotes would also present the value in the backend, which is even better.

    We’re super happy that we could address your concern. If you have any other questions in the future, know that we are here to help you.

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

    If you 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.

    Thread Starter 008iresf

    (@008iresf)

    Thanks a lot for your help.
    Sure, rank math deserve 5 stars 😉

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘use WooCommerce product attributes in meta description’ is closed to new replies.