Domanda

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);
È stato utile?

Soluzione

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!

Altri suggerimenti

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. ;)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top