I am looking to add a new div/block above single product pages in woocommerce that shows the featured product in full width below header

huangapple go评论60阅读模式
英文:

I am looking to add a new div/block above single product pages in woocommerce that shows the featured product in full width below header

问题


/**
 * @snippet       在Storefront主题中编辑图像尺寸
 * @how-to        获取定制Woo.com免费
 * @作者          Rodolfo Melogli
 * @兼容          WooCommerce 6
 * @捐赠 $9       https://businessbloomer.com/bloomer-armada/
 */
 

add_filter( 'storefront_woocommerce_args', 'bbloomer_resize_storefront_images' );
 
function bbloomer_resize_storefront_images( $args ) {
   $args['single_image_width'] = 1000;
//    $args['thumbnail_image_width'] = 222;
   return $args;
}

/* 从产品图像中选择不同的目录缩略图 */
add_action( 'woocommerce_init', 'vista_replace_loop_product_thumbnail' );

function vista_replace_loop_product_thumbnail() {

    remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );

    function vista_replace_product_thumbnail() {
        global $product;
        $attachment_id = $product->get_gallery_attachment_ids()[0];
        echo "<img src='" . wp_get_attachment_url( $attachment_id ) . "'>";
    }

    add_action( 'woocommerce_before_shop_loop_item_title', 'vista_replace_product_thumbnail', 10 );
}
英文:

Question regarding woocommerce, storefront child theme:

Can anyone please help me with a custom code I can add to function.php that will create a new block above the single product (directly below the header) that will show the main product image?

The image will have to be different dimension than the ones I use in the gallery.

What I tried to do so far:

I wasn't able to do this well with CSS without stretching the entire gallery image. When I used custom code in function.php to set the product image width (by bbloomer) it was still limited to the width of the single page div (800px max, otherwise the text below is unreadable).

I also tried using a function that applies a different main image from the catalog thumbnail (by Vista) but I still have the max-width div problem.

So I figured the best way to accomplish this is to create a new div, with its own ID, above the single-product, that calls for the main product image. Then I can easily customize the look with CSS, without affecting anything below.

Thank you in advance! I do want to mention I searched all over Stack overflow and online for a solution before finally deciding to ask here.

Thanks again.

Code mentioned:


/**
 * @snippet       Edit Image Sizes @ Storefront Theme
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 

add_filter( &#39;storefront_woocommerce_args&#39;, &#39;bbloomer_resize_storefront_images&#39; );
 
function bbloomer_resize_storefront_images( $args ) {
   $args[&#39;single_image_width&#39;] = 1000;
//    $args[&#39;thumbnail_image_width&#39;] = 222;
   return $args;
}



/ *different catalog thumbnail from product image */
	add_action( &#39;woocommerce_init&#39;, &#39;vista_replace_loop_product_thumbnail&#39; );

	function vista_replace_loop_product_thumbnail() {

		remove_action( &#39;woocommerce_before_shop_loop_item_title&#39;, &#39;woocommerce_template_loop_product_thumbnail&#39;, 10 );

		function vista_replace_product_thumbnail() {
			global $product;
			$attachment_id = $product-&gt;get_gallery_attachment_ids()[0];
			echo &quot;&lt;img src=&#39;&quot; . wp_get_attachment_url( $attachment_id ) . &quot;&#39;&gt;&quot;;
		}

		add_action( &#39;woocommerce_before_shop_loop_item_title&#39;, &#39;vista_replace_product_thumbnail&#39;, 10 );
	}

答案1

得分: 1

好的,以下是翻译好的内容:

#main-product-image img {
	width: 100%;
	margin: 0 auto;
	margin-bottom: 45px;
}
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );

add_action( 'woocommerce_before_single_product', 'show_main_product_image', 5 );

function show_main_product_image() {
    global $product;

    $image_url = wp_get_attachment_url( $product->get_image_id() );

    echo '<div id="main-product-image">';
    echo '<img src="' . $image_url . '" />';
    echo '</div>';

}
英文:

OK this is what I ended up doing. I don't know if it's the most elegant, but it worked. Including the code I placed in functions and the css file.

I used the input from user580950 and woocommerce documentation.


#main-product-image img {
	width: 100%;
	margin: 0 auto;
	margin-bottom: 45px;
}

remove_action( &#39;woocommerce_before_single_product_summary&#39;, &#39;woocommerce_show_product_images&#39;, 20 );

add_action( &#39;woocommerce_before_single_product&#39;, &#39;show_main_product_image&#39;, 5 );

function show_main_product_image() {
    global $product;

    $image_url = wp_get_attachment_url( $product-&gt;get_image_id() );

    echo &#39;&lt;div id=&quot;main-product-image&quot;&gt;&#39;;
    echo &#39;&lt;img src=&quot;&#39; . $image_url . &#39;&quot; /&gt;&#39;;
    echo &#39;&lt;/div&gt;&#39;;

}

答案2

得分: 0

尝试将此代码添加到您的子主题的 functions.php 文件中,以在单个产品页面上方显示主要产品图像:

add_action( 'woocommerce_before_single_product', 'show_main_product_image', 5 ); function show_main_product_image() { global $product; echo '<div id="main-product-image">'; echo wp_get_attachment_image( $product->get_image_id(), 'full' ); echo '</div>'; } 
英文:

Try adding this code to your child theme's functions.php file to display the main product image above the single product page

add_action( &#39;woocommerce_before_single_product&#39;, &#39;show_main_product_image&#39;, 5 ); function show_main_product_image() { global $product; echo &#39;&lt;div id=&quot;main-product-image&quot;&gt;&#39;; echo wp_get_attachment_image( $product-&gt;get_image_id(), &#39;full&#39; ); echo &#39;&lt;/div&gt;&#39;; } – 

huangapple
  • 本文由 发表于 2023年2月18日 00:49:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定