Support

Account

Home Forums General Issues Fields from pages to product (woocommerce)

Solving

Fields from pages to product (woocommerce)

  • Hello!

    I’ve added some custom fields to the Page (url and images – simple ads system). How can I view them on single-product.php in Woocommerce?

    Variable get_field('my_custom_link_in_pages') is empty.

    thx!

  • I take it you created a Field Group containing my_custom_link_in_pages and assigned it to the Page post type. But is it assigned to the Product post type as well?

  • I set as below:

    https://i.imgur.com/n7XhKK5.png

    Page “Banner 1” is a page containing a link and image banner.

    It’s possible view this custom links in product page?

  • By this setup this particular page (“Banner 1”) and all products each are given two fields: A link and a banner. Now you can assign them a link and a banner each. So, if you call get_field( 'my_custom_link_in_pages' ) on a product page, you are grabbing the link that you have assigned to this product. If you have not assigned this product a link, the function will return an empty value.

    Depending on what exactly your are trying to accomplish, there are three ways to solve this:

    1. You can assign your product the same link and banner that you have assigned “Banner 1”, so they now belong to the product as well.

    2. You can fetch the fields of “Banner 1” directly on every product page like this: get_field( 'my_custom_link_in_pages' , $the_id_of_the_banner1_page )

    3. You can remove the second field group condition, so the fields no longer apply to product pages, then create a second field group, which contains a Post Object field. Then you can assign each product a page the link and banner of which to use, like this:

    <?php
    $the_page_id = get_field( 'the_assigned_page' );
    ( is_object( $the_page_id ) ) ? $the_page_id = $the_page_id->ID : null ;
    $the_link = get_field( 'my_custom_link_in_pages' , $the_page_id  );
  • For some annoying reason my last reply was just gone, but I managed to restore it somehow. Maybe I pushed the wrong button, so please excuse any mail spam that might occur now.

    By your current setup this particular page (“Banner 1”) and all products each are given two fields: A link and a banner. Now you can assign them a link and a banner each. So, if you call get_field( 'my_custom_link_in_pages' ) on a product page, you are grabbing the link that you have assigned to this product. If you have not assigned this product a link, the function will return an empty value.

    Depending on what exactly your are trying to accomplish, there are three ways to solve this:

    1. You can assign your product the same link and banner that you have assigned “Banner 1”, so they now belong to the product as well.

    2. You can fetch the fields of “Banner 1” directly on every product page like this:

    <?php
    get_field( 'my_custom_link_in_pages' , $the_id_of_the_banner1_page )

    3. You can remove the second field group condition, so the fields no longer apply to product pages, then create a second field group, which contains a Post Object field, and then assign each product a page, the link and banner of which to use, like this:

    <?php
    $the_page_id = get_field( 'the_assigned_page' );
    is_object( $the_page_id ) ? $the_page_id = $the_page_id->ID : null ;
    $the_link = get_field( 'my_custom_link_in_pages' , $the_page_id  );
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Fields from pages to product (woocommerce)’ is closed to new replies.