简体   繁体   中英

Wordpress, Woocommerce Storefront Related Products

I would like to move the Related Products on the Products page to a lower position with a full width column, so I have defined the following in the functions.php file to do just that;

// Move related products
   remove_action('woocommerce_after_single_product_summary','woocommerce_output_related_products',20);
   add_action('storefront_before_footer','woocommerce_output_related_products',20);

However, by doing so the related products now presumably are outside of some important loop, because they are now being displayed on the category archive pages.

Does anyone know of (a) How to either restrict the output to product pages (without display:none; in the style sheet) -or- (b) another defined position where I can place the content outside (below) the div and presumably sidebar but retain relevance of the related products to the product page displayed?

I'd prefer not to be trying to re-engineer the html output for the and , I'm sure you'll understand that doing so would have a great deal of impact.

Another idea I guess (whilst not ideal or the right way), could be to use jQuery.

EDITED: This should restrict the output to product pages.

remove_action('woocommerce_after_single_product_summary','woocommerce_output_related_products',20);

add_action('storefront_before_footer','woo_related_product_addition');

function woo_related_product_addition() {

    global $post;

    if (function_exists( 'get_product' )) {
      add_action('storefront_before_footer','woocommerce_output_related_products',20);
    }
}

If that doesn't work for you...

remove_action('woocommerce_after_single_product_summary','woocommerce_output_related_products',20);

add_action('storefront_before_footer','woo_related_product_addition');

function woo_related_product_addition() {

    global $post;

    if (function_exists( 'get_product' )) {
    $product = get_product( $post->ID );

    if ($product->is_type( 'single' || 'grouped' || 'external' || 'variable' )) {
      add_action('storefront_before_footer','woocommerce_output_related_products',20);
    }
}
}

EDIT 2: You could then remove the products from the archive pages by doing something like.

remove_action('woocommerce_after_single_product_summary','woo_related_product_removal');

function woo_related_product_removal() {

    global $post;

    if (function_exists( 'get_product' )) {
    $product = get_product( $post->ID );

    if (!$product->is_type( 'single' || 'grouped' || 'external' || 'variable' )) {
        remove_action('woocommerce_after_single_product_summary','woocommerce_output_related_products',20);
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM