placeholders like %wc_size% & %product_cat%

#1394
Viewing 15 replies - 1 through 15 (of 25 total)
  • Hello

    Please add below code in your theme’s functions.php file:

    
    /**
     * Filter to replace wc_brand with custom value.
     */
    add_filter( 'rank_math/replacements', function( $replacements ) {
    	if ( is_singular( 'product' ) ) {
    		$product = wc_get_product();
    
    		// Product Brand
    		if ( isset( $replacements['%wc_brand%'] ) ) {
    			$brand = $product->get_attribute('pa_brand');
    			// Use this code to get 1st element from brand attribute if multiple brands are assigned to a Product.
    			$brands = explode( ',', $brand );
    			if ( ! empty( $brands ) ) {
    				$brand = $brands[0];
    			}
    
    			$replacements['%wc_brand%'] = $brand; // Pass $brand to set meta value for brand.
    		}
    
    		// Product Size
    		if ( isset( $replacements['%wc_size%'] ) ) {
    			$size = $product->get_attribute('pa_size');
    			// Use this code to get 1st element from size attribute if multiple sizes are assigned to a Product.
    			$sizes = explode( ',', $size );
    			if ( ! empty( $sizes ) ) {
    				$size = $sizes[0];
    			}
    
    			$replacements['%wc_size%'] = $size; // Pass $size to set meta value for brand.
    		}
    
    		// Product Category
    		if ( isset( $replacements['%product_category%'] ) ) {
    			$size = $product->get_attribute('pa_size');
    			// Use this code to get 1st element from size attribute if multiple sizes are assigned to a Product.
    			$sizes = explode( ',', $size );
    			if ( ! empty( $sizes ) ) {
    				$size = $sizes[0];
    			}
    
    			$replacements['%wc_size%'] = wp_get_post_terms( get_the_ID(), 'product_cat', array( 'fields' => 'names', 'number' => 1 ) )[0];
    		}
    		
    	}
    	return $replacements;
    });
    
    /**
     * Filter to add custom variables
     */
    add_filter( 'rank_math/vars/replacements', function( $vars ) {
    	global $product;
    	if ( ! $product ) {
    		return $vars;
    	}
    
    	// Product Category
    	$vars['product_category'] = [
    		'name'    => 'Product\'s Category',
    		'desc'    => 'Current Product Category',
    		'example' => wp_get_post_terms( $_GET['post'], 'product_cat', array( 'fields' => 'names', 'number' => 1 ) )[0],
    	];
    
    	// Product Size
    	$size = $product->get_attribute('pa_size');
    	// Use this code to get 1st element from brand attribute if multiple brands are assigned to a Product.
    	$sizes = explode( ',', $size );
    	if ( ! empty( $sizes ) ) {
    		$size = $sizes[0];
    	}
    	$vars['wc_size'] = [
    		'name'    => 'Product\'s Size',
    		'desc'    => 'Current Product Size',
    		'example' => $size
    	];
    
    	// Proudct Brand.
    	if ( isset( $vars['wc_brand'] ) ) {
    		$brand = $product->get_attribute('pa_brand');
    
    		// Use this code to get 1st element from brand attribute if multiple brands are assigned to a Product.
    		$brands = explode( ',', $brand );
    		if ( ! empty( $brands ) ) {
    			$brand = $brands[0];
    		}
    		
    		$vars['wc_brand']['example'] = $brand;
    	}
    
    	return $vars;
    });
    

    After adding this code you will see 2 new variables in variable list %product_category% & %wc_size%.

    Hope that helps. Thank you.

    O A F O
    Rank Math free

    Hello thanks for that. It doesn’t show any new variables I’m afraid 🙁

    Hello,

    The code will show the new variables only in Product Post type posts.

    If you could share your WP logins, I would love to take a look and fix it for you.

    Looking forward to helping you. Thank you.

    O A F O
    Rank Math free

    Hello there. is there any other way of doing it? As i don’t really like giving access, since someone else logged in to fix there plugin & ruined my site.

    Thanks

    Todd
    Rank Math free

    Hello,

    You can use this plugin to provide us access and install the Audit log plugin as well to see what changes we make:
    https://wordpress.org/plugins/temporary-login-without-password/

    Unfortunately, there is no workaround. Sorry.

    O A F O
    Rank Math free

    I have replied in the Sensitive Data section,

    thanks again

    O A F O
    Rank Math free

    sorry user wasn’t active there… there was a space in the name. there is no space

    Hello,

    Your login details aren’t working for me. Can you please check again and add the correct username and password in the Sensitive Data Section.

    Also, the code I gave you before will only work on the Products page and it looks like you are trying to add those variables in global settings. Please edit 1 of the product on your site and see if it shows the new variables.

    Thank you.

    O A F O
    Rank Math free

    Hello, yes i posted above the details were incorrect, but they are right now.

    thanks

    O A F O
    Rank Math free

    Hello, no that doesn’t work with editing the item.

    I need to to be the same as you did with the adding the brand into the titles

    thanks

    Hello,

    I see you’ve only given us access to the Rank Math settings. Like I mentioned above, the code I gave you will only work on the Product page. Please Navigate to Dashboard >> Products and edit any product and see if those new variables are added.

    Hope that helps. Thank you.

    O A F O
    Rank Math free

    Hello. what do you need access to? No it doesnt work on product page.

    Will this not work in the same way you added the ‘ pa_brand ‘ in for me on products on – /wp-admin/admin.php?page=rank-math-options-titles

    This is what happens ion the products page – https://prnt.sc/n2szpo

    This works grea which is what you done for me – https://prnt.sc/n2t0q4 : as that gives me this – https://prnt.sc/n2t1c4

    Just the others are not working

    THanks

    • This reply was modified 5 years, 1 month ago by O A F O.
    O A F O
    Rank Math free

    Hello. any luck here?

    Thanks

    Hello,

    I want access to the Products posts on your site and to the theme editor to check why placeholders are not working on your site as on my setup it is working fine. Please see the recording: http://i.mythemeshop.com/7AQ96n

    Will this not work in the same way you added the ‘ pa_brand ‘ in for me on products on – /wp-admin/admin.php?page=rank-math-options-titles

    Like I mentioned before the code I gave you will only work on the Products Page, it will not show the placeholders in Settings and on the other pages of your site. If you want to show the placeholders in Settings then please remove the below condition from the rank_math/vars/replacements filter:

    
    if ( ! $product ) {
        return $vars;
    }
    

    Hope that helps. Thank you.

    O A F O
    Rank Math free

    Hello,

    yes i need them in the settings page but if i remove –

    if ( ! $product ) {
    return $vars;
    }

    my website stops working.

    okay i will sort that access you and then update you.

    Thanks

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 15 replies - 1 through 15 (of 25 total)

The ticket ‘placeholders like %wc_size% & %product_cat%’ is closed to new replies.