Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Filter to show brand value in snippet preview.
- */
- add_filter( 'rank_math/vars/replacements', function( $vars ) {
- global $product;
- if ( ! $product ) {
- return $vars;
- }
- 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;
- });
- /**
- * Filter to replace wc_brand with custom value.
- */
- add_filter( 'rank_math/replacements', function( $replacements ) {
- if ( is_singular( 'product' ) && isset( $replacements['%wc_brand%'] ) ) {
- $product = wc_get_product();
- $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.
- }
- return $replacements;
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement