0

I'm creating my own Wordpress theme, and I'm trying to make it compatible with Woocommerce. I understand pretty much everything about theming woocommerce, including copying the template files and changing them, also I'm confortable with some of the Woocommerce hooks as well.

Now Woocommerce uses an archive-product.php file to show shop items, and it is also currently using the a page named 'Shop' for that. The problem is that I'm not able to use the custom meta box data in my archive-product.php file when I save them in the 'Shop' page.

So I just want to know if there is anyway of using meta box data in an archive template, such as the archive-product.php template of Woocommerce.

Any suggestions?

Edit: So I was able to solve my problem completely by using the following code in the file that I'm including:

if( is_shop() ) {
  $post_id = get_option( 'woocommerce_shop_page_id' );;
} else {
  $post_id = $post->ID;
}

$meta_key = get_post_meta($post_id, 'meta_key', true);
  • 1
    It should be the same as in any other theme. What have you tried? – s_ha_dum May 21 '14 at 0:30
2

Woocommerce does hijack the main query on the shop page / product queries, so it's possible that you're used to referencing your custom fields within the loop or in some other context where the global $post is the page you are on, but in this case the contents of $post will be Woocommerce products and not the page.

If this is for your base shop page you can reference the custom fields like this (change for the meta_key of your custom meta box fields)

//returns shop page post object
$your_shop_page = get_post( wc_get_page_id( 'shop' ) );

//access your meta fields like this
$your_shop-page->your_meta_key; 

I hope this helps in your situation, if you use the above code it would help to make sure you are on the shop page and those values exist before attempting to output them, always code defensively!

  • I agree that meta works the same... just need the right page ID. I think $page_id = get_queried_object_id(); ought to work too. – helgatheviking May 21 '14 at 9:07
  • @helgatheviking correct, great way to get the original, though wouldn't returning the whole object via get_queried_object be more useful? as archive.php is used for shop and categories extra code would be needed to differentiate between the two, as when on an archive it will return a category (term) object which will likely not have these meta box fields (most people don't add meta to terms, though it sure can be useful) – aj-adl May 21 '14 at 9:17
  • 1
    Any post meta can be accessed through the post object via the WP_Post's magic methods. This is what the 2nd part of the code I posted is: if you are working with the a post object $post_object->meta_box_key is the same thing as get_post_meta( id, 'meta_box_key', true). – aj-adl May 21 '14 at 11:38
  • Didn't know about the magic methods. Thanks for that bit of knowledge. +1 – helgatheviking May 21 '14 at 11:42
  • 1
    Full code would be $current_page = get_queried_object(); $your_meta =$current_page->meta_box_key; – aj-adl May 21 '14 at 11:43
0

In the template that is being included, you might be able to do something like this:

global $post;
if( get_queried_object_id() == wc_get_page_id( 'shop' ) ) {
  $post_id = get_queried_object_id();
} else {
  $post_id = $post->ID;
}

get_post_meta( $post_id, 'meta_key', true);

So that it will work for either a blog post or the archives.

Untested, so your mileage may vary. ;)

  • Yes, I tried this but get_post_meta() just doesn't work in the archive-product.php file, even when I directly put the post_id in the get_post_meta() function, like this: get_post_meta(4, 'meta_key', true); – user1981248 May 21 '14 at 13:28
  • If you are hard-coding get_post_meta( 4, 'meta_key', true ); and still get nothing, then you don't have a meta value for the meta_key for a post with an ID of 4. The function works everywhere regardless of template. – helgatheviking May 21 '14 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.