skip to Main Content

I’m using a function to output a collection of products in Shopify, onto a WordPress page.

I’ve got most of the data displaying correctly, except a custom value that is entered as a tag in Shopify. Using the api, I’m then trying to get this specific tag for the product subtitle which is formatted with att:Subtitle: before each product custom value/text.

This is the code I have got to (I’ve commented other unsuccessful attempts in the middle) – this is inside the overall code to show the 4 products in the Shopify collection:

 // Using tags to output custom data from products
$tags = $current_product['tags'];

// $tags is a string, this turns the values into an array
$product_tags = explode(',', $tags);

// Evaluate if there is a string with att:Subtitle in the product tags
// https://tecadmin.net/check-string-contains-substring-in-php/
$subtitle_attribute_key = "att:Subtitle:";

if (strpos($tags, 'att:Subtitle:') !== false) {  
    // Returns a numbered value corresponding to my subtitle attribute
    // $key = array_search($subtitle_attribute_key, $product_tags);
    $sub = strpos($tags, $subtitle_attribute_key);

    // Turns the numbered value into a text value
    // $numArray = explode(" ", $sub);
    // var_dump($numArray);
    $value = print_r($sub, true);
    // $value = array_search("att:Subtitle:",$product_tabs);
    // $value = array_search("att:Subtitle:", $tabs); // Warning: array_search() expects parameter 2 to be array, null given
    // $result = $product_tags['$value']; // my attempt to return the text

    // Remove att:Subtittle: in front of the subtitle value to output the clean final value
    $subtitle = ltrim($value, 'att:Subtitle:');
}

So far it comes out as a number value where I’m displaying $subtitle… but I can’t figure out how to display the custom text value instead.

Any ideas?

Thanks

EDIT: I am working with products that have multiple tags and I do not control these. Out of the tags I’m trying to find the one that starts with att:Subtitle: but only show the custom value after that marker.

When I echo $tags, the ist comes out something like this:

att:Benefit:balance, att:Perfect:Combination Skin, att:Size: 1.8 oz, att:Subtitle: Multi-Tasking Product, Key Ingredient 1, Key Ingredient 2, Essentials, meta-related-product-xyz, meta-related-product-brandname

They will all have a different list of tags

2

Answers


  1. So for what I understand you’re trying to get rid of ‘att:Subtitle:’ from the tags on your products descriptions? maybe you should try replacing that value on your tags array

    // Using tags to output custom data from products
    $tags = $current_product['tags'];
    
    // $tags is a string, this turns the values into an array
    $product_tags = explode(',', $tags);
    
    // Get rid of attribute on product tag
    for($i = 0; $i < count($product_tags); $i++){
        $subtitle = str_replace(':att:Subtitle','',$product_tags[i]);
    }
    
    Login or Signup to reply.
  2. Of course it comes out as a number. You’re getting a number from the strpos() and ultimate trying to run ltrim() on it.

    If you’re expecting the desired text to be in $tags immediately following ‘att:Subtitle:’ and have nothing else in that string, $subtitle = substr($tags, strpos('att:Subtitle:') + strlen('att:Subtitle:')); should give it to you. If you’re expecting it to possibly be an element in the $product_tags array, need to loop (I’m assuming it shows up once, at most):

    foreach ($product_tags as $product_tag) {
        if (strpos($product_tag, 'att:Subtitle:') !== false) { 
            $subtitle = substr($product_tag, strpos($product_tag, 'att:Subtitle:') + strlen('att:Subtitle:'));
            break;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search