Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.27 KB | None | 0 0
  1. /**
  2.  * Filter to show brand value in snippet preview.
  3.  */
  4. add_filter( 'rank_math/vars/replacements', function( $vars ) {
  5.     global $product;
  6.  
  7.     if ( ! $product ) {
  8.         return $vars;
  9.     }
  10.  
  11.     if ( isset( $vars['wc_brand'] ) ) {
  12.         $brand = $product->get_attribute('pa_brand');
  13.  
  14.         // Use this code to get 1st element from brand attribute if multiple brands are assigned to a Product.
  15.         $brands = explode( ',', $brand );
  16.         if ( ! empty( $brands ) ) {
  17.             $brand = $brands[0];
  18.         }
  19.        
  20.         $vars['wc_brand']['example'] = $brand;
  21.     }
  22.  
  23.   return $vars;
  24. });
  25.  
  26. /**
  27.  * Filter to replace wc_brand with custom value.
  28.  */
  29. add_filter( 'rank_math/replacements', function( $replacements ) {
  30.     if ( is_singular( 'product' ) && isset( $replacements['%wc_brand%'] ) ) {
  31.         $product = wc_get_product();
  32.  
  33.         $brand = $product->get_attribute('pa_brand');
  34.         // Use this code to get 1st element from brand attribute if multiple brands are assigned to a Product.
  35.         $brands = explode( ',', $brand );
  36.         if ( ! empty( $brands ) ) {
  37.             $brand = $brands[0];
  38.         }
  39.  
  40.         $replacements['%wc_brand%'] = $brand; // Pass $brand to set meta value for brand.
  41.     }
  42.     return $replacements;
  43. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement