placeholders like %wc_size% & %product_cat%

#1394
Viewing 10 replies - 16 through 25 (of 25 total)
  • Hello i have updated the so you can view what yiou need, i have also added detials to the Sensitive Data

    THanks

    Just to add. Please dont try on any thing thats a draft please.

    THanks again

    Hello,

    I removed the condition from the filter code I gave you before, now you’ll see those variables in Settings too.

    Hope that helps. Thank you.

    • This reply was modified 5 years, 9 months ago by Pratik.

    Hello Pratik. Thanks for that. I can now see them.

    But i still havea problem.

    The %wc_brand% – loads the brand name great.

    but %wc_size% & %product_category% do not load any as yet still..

    Sorry about this.

    Thanks again

    Hello,

    The code seems to be working fine on your site. When I use %product_category% placeholder in title the preview shows the Product category.

    Although, on frontend the changes are not being reflected that could be due to the cahce. Is server or anyother cache enabled on your site? If so, please disabled it and check again.

    Hope that helps. Thank you.

    Hello Pratik. THis is the problem i have been having…

    There is no cache on the server side, or install side, i disabled all and flushed prior to you making any changes.

    I have also disabled all plugins, & changed theme.

    Sstill the same problem. no change on front end. I cant quite figure it out as the ‘ Brand ‘ works great

    Thanks

    just added something to the sensitive data

    Hello,

    I updated the code on your site, the Product category, brand, size now appears on the frontend.

    Can you please confirm?

    Thank you.

    Pratik. Thats great thank you. I will keep a copy of that just incase,

    IS there an easy for it to show all categories that the item is in? Or can it only show the 1st?

    THanks

    Hello,

    Yes, you can use implode function to show product categories separated by the comma. I’ve done that for you.

    Here is a reference code:

    
    add_action( 'rank_math/vars/register_extra_replacements', function() {
    	RankMath\Helper::register_var_replacement(
    			'product_category',
    			'get_product_category',
    			array(
    				'name'    => esc_html__( 'Product category.', 'rank-math' ),
    				'desc'    => esc_html__( 'Product category of the current product', 'rank-math' ),
    				'example' => get_product_category(),
    			)
    		);
    
    	RankMath\Helper::register_var_replacement(
    			'wc_size',
    			'get_product_size',
    			array(
    				'name'    => esc_html__( 'Product size.', 'rank-math' ),
    				'desc'    => esc_html__( 'Product size of the current product', 'rank-math' ),
    				'example' => get_product_size(),
    			)
    		);
    });
    
    function get_product_category() {
    	$product_id = rank_math_get_product();
    	
    	if ( ! $product_id ) {
    		return null;
    	}
    
    	$product_cats = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'names' ) );
    	if ( empty( $product_cats ) ) {
    		return null;
    	}
    
    	return implode( ', ',$product_cats );
    }
    
    function get_product_size() {
    	$product_id = rank_math_get_product();
    	
    	if ( ! $product_id ) {
    		return null;
    	}
    
    	$product = wc_get_product( $product_id );
    	if ( ! $product ) {
    		return;
    	}
    	$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];
    	}
    
    	return $size;
    }
    
    function rank_math_get_product() {
    	$product_id = isset( $_GET['post'] ) ? $_GET['post'] : get_queried_object_id();
    	if ( ! $product_id && ( ! is_singular( 'product' ) || ! function_exists( 'wc_get_product' ) ) ) {
    		return null;
    	}
    
    	return $product_id;
    }
    

    Hope that helps. Thank you.

    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 10 replies - 16 through 25 (of 25 total)

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