New Landing How can we help? Atelier SKU Display on Loops

Viewing 13 posts - 1 through 13 (of 13 total)
  • Posted in: Atelier
  • #185208
    purekarting
    Member
    Post count: 42

    How can we get the product standard/list/grid loops to show the SKU in place of the categories for each product?

    Attachments:
    You must be logged in to view attached files.
    #185301
    Kyle – SUPPORT
    Moderator
    Post count: 35880

    Hi

    Open up atelier/woocommerce/content-product.php and replace the 2 instances of:

    echo $product->get_categories( ', ', '<span class="posted_in">', '</span>' );

    with

    echo $product->get_sku( ', ', '<span class="posted_in">', '</span>' );

    – Kyle

    #185306
    purekarting
    Member
    Post count: 42

    Fantastic Kyle.

    Thanks for the help. I copied atelier/woocommerce/content-product.php over to my atelier child theme and made the change there.

    Also, of note (although I’m not sure if it changed anything) I used two different echo’s. The two instances of
    echo $product->get_categories( ', ', '<span class="posted_in">', '</span>' );
    where actually a little different from each other.

    One was surrounded by H5 tags, and the other just span. So I used
    echo $product->get_sku( ', ', '<h5 class="posted_in">', '</h5>' );
    on line 296 and
    echo $product->get_sku( ', ', '<span class="posted_in">', '</span>' );
    on line 325.

    Again. Thank you.

    #185309
    Kyle – SUPPORT
    Moderator
    Post count: 35880

    Ok no problem ๐Ÿ™‚

    #189404
    purekarting
    Member
    Post count: 42

    Hi Again.

    Since you were so helpful in getting the SKU to display on the category product loops, I was curious if you could help me get one more thing displayed.

    I’m matching my products to a category hero image that displays all the products arranged in an exploded view, with a reference letter/number next to the product image on that view. So for each product, I have custom meta fields on the products to set this reference letter/number per category for each product (so long as it is in that category, and matches one of the references on the image).

    So the first question is, how do we insert a custom field into the loop?
    And the second is, how do we dynamically call for the correct custom field based on the category slug?

    I’m using Advanced Custom Fields for the explosion reference fields because I can have a sidebar box displaying a group of all the custom explosion reference fields.

    The code below works to display the text on the loop for the Airbox Woo Category (category slug = airbox):

    if(get_field('eref-airbox'))
    	{
    		echo '<div><span>REF: ' . get_field('eref-airbox') . '</span></div>';
    	}
    

    However I need to test for many different fields, all of which have names matching the structure eref-{category-slug}. I don’t want to put hundreds of if statements in my content-product loop. So I’d like to inject the slug for the current category being viewed into the statement. I tried:

    $category = get_the_category();
    if(get_field('eref-' . $category[0]->slug . ''))
    	{
    		echo '<div><span>REF: ' . get_field('eref-' . $category[0]->slug . '') . '</span></div>';
    	}
    

    but it is not working. It passes PHP validation, but no longer loads the custom fields.

    #189435
    purekarting
    Member
    Post count: 42

    Solved my above question on how to load the custom meta. Final code on content-product.php, placed just below the code to load my SKU’s.

    $categories = get_the_terms($post_id, 'product_cat');
    if(get_field('eref-' . $categories[0]->slug . ''))
    	{
    		echo '<div><span>REF: ' . get_field('eref-' . $categories[0]->slug . '') . '</span></div>';
    	}

    PS
    I’m using Advanced Custom Fields, so that is where get_field comes from.

    #189524
    Kyle – SUPPORT
    Moderator
    Post count: 35880

    Great! Thanks for sharing ๐Ÿ™‚

    #190000
    purekarting
    Member
    Post count: 42

    Damn.

    I spoke too soon.

    The two categories I was testing did not contain any products which are located in multiple categories. If a product is located in multiple categories (with a REF letter set for multiple categories) then the PHP is loading the REF from the first category in the array. I added echo '<div>eref-' . $categories[0]->slug . '</div>'; into the loop to confirm that is what was happening. See the image below:

    Category Slug Inject Fail

    #190035
    purekarting
    Member
    Post count: 42

    <?php echo single_term_title(); ?> When placed outside or inside of the product archive loop correctly returns the proper title of the active Woocommerce category. So now I just need a way to break that result down to the slug, and I’d be in business.

    #190038
    purekarting
    Member
    Post count: 42

    Getting Closer - Category Name inside Product Category Loop

    #190384
    Rui Guerreiro – SUPPORT
    Keymaster
    Post count: 25779

    Hi,

    Take a look at
    https://codex.wordpress.org/Function_Reference/get_term_by

    Since it’s an advanced customization we can take more time than we took at the moment. Hope you understand.

    -Rui

    #190395
    purekarting
    Member
    Post count: 42

    Hello Rui,

    I totally understand and appreciate the pointers when you guys are able to do so. I dug into it some more this afternoon after taking a break from the computer screen. Bingo. Found the solution.

    The true solution:

    $catslug = get_query_var( 'product_cat' );
    if(get_field('eref-' . $catslug . ''))
    	{
    		echo '<div><span>REF: ' . get_field('eref-' . $catslug . '') . '</span></div>';
    	}
    

    I hope it can help someone else in the future too!

    #190444
    Kyle – SUPPORT
    Moderator
    Post count: 35880

    Great, thanks for sharing

    – Kyle

Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in and have valid license to reply to this topic.

License required for one of the following items
Login and Registration Log in · Register