代码之家  ›  专栏  ›  技术社区  ›  Anthony Cdf Whitefield

在WooCommerce自定义产品页面上有条件地添加自定义内容

  •  1
  • Anthony Cdf Whitefield  · 技术社区  · 8 年前

    早上好,

    我已经将网站的设计更改为在WooCommerce的产品上运行,我如何调整我的代码片段以允许此功能工作?

    <?php if(in_category(531)) { ?>
                    <?php if ( current_user_can('manage_options') ) { ?>
                    <p>
                    &darr; Download Image<br />
                    <a href="<?php $image_id = get_post_thumbnail_id();
                    $image_url = wp_get_attachment_image_src($image_id,'web', true);
                    echo $image_url[0]; ?>" ><img src="<?php bloginfo ('template_url' )?>/img/downloadForWeb.png" alt="download for web" /></a><br />
    
                    <a href="<?php $image_id = get_post_thumbnail_id();
                    $image_url = wp_get_attachment_image_src($image_id,'print', true);
                    echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url' )?>/img/downloadForPrint.png" alt="download for print" /></a><br />
    
                    <a href="<?php $image_id = get_post_thumbnail_id();
                    $image_url = wp_get_attachment_image_src($image_id,'', true);
                    echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url' )?>/img/downloadForProPrint.png" alt="download for pro print" /></a><br />
                    </p>
                    <?php } else { ?>
                    <h2>Special permission required</h2>
                    <p>In order to use this image you need special permission from the admin, please fill in the form below and we'll get back to
                    you as soon as possible...</p>
                    <?php echo do_shortcode( '[contact-form 11 "special permission"]' ) ?>
                    <?php } ?>
    
                    <?php } else { ?>
                    <p>
                    &darr; Download Image<br />
                    <a href="<?php $image_id = get_post_thumbnail_id();
                    $image_url = wp_get_attachment_image_src($image_id,'web', true);
                    echo $image_url[0]; ?>" ><img src="<?php bloginfo ('template_url' )?>/img/downloadForWeb.png" alt="download for web" /></a><br />
    
                    <a href="<?php $image_id = get_post_thumbnail_id();
                    $image_url = wp_get_attachment_image_src($image_id,'print', true);
                    echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url' )?>/img/downloadForPrint.png" alt="download for print" /></a><br />
    
                    <a href="<?php $image_id = get_post_thumbnail_id();
                    $image_url = wp_get_attachment_image_src($image_id,'', true);
                    echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url' )?>/img/downloadForProPrint.png" alt="download for pro print" /></a><br />
                    </p>
                    <?php } ?>
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   LoicTheAztec    8 年前

    正确的方法似乎是使用挂接的自定义函数 woocommerce_before_add_to_cart_form 动作挂钩,将代码嵌入其中并在产品页面中显示。

    511 是一个 产品类别 (并且不是普通WP类别。如果不是,您将不得不创建它,并用您的新产品类别的名称、slug或ID替换此ID。

    这应该是您的代码:

    add_action('woocommerce_before_add_to_cart_form','my_custom_product_content', 1, 0 );
    function my_custom_product_content(){
        global $post, $product;
    
        if(has_term( array(531), 'product_cat', $post->ID )) {
    
            if ( current_user_can('manage_options') ) { 
                ?>
                <p>
                &darr; Download Image<br />
                <a href="<?php $image_id = get_post_thumbnail_id();
                $image_url = wp_get_attachment_image_src($image_id,'web', true);
                echo $image_url[0]; ?>" ><img src="<?php bloginfo ('template_url' )?>/img/downloadForWeb.png" alt="download for web" /></a><br />
    
                <a href="<?php $image_id = get_post_thumbnail_id();
                $image_url = wp_get_attachment_image_src($image_id,'print', true);
                echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url' )?>/img/downloadForPrint.png" alt="download for print" /></a><br />
    
                <a href="<?php $image_id = get_post_thumbnail_id();
                $image_url = wp_get_attachment_image_src($image_id,'', true);
                echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url' )?>/img/downloadForProPrint.png" alt="download for pro print" /></a><br />
                </p>
                <?php
            } else {
                ?>
                <h2>Special permission required</h2>
                <p>In order to use this image you need special permission from the admin, please fill in the form below and we'll get back to
                you as soon as possible...</p>
                <?php echo do_shortcode( '[contact-form 11 "special permission"]' ) ?>
                <?php
            }
        } else {
            ?>
            <p>
            &darr; Download Image<br />
            <a href="<?php $image_id = get_post_thumbnail_id();
            $image_url = wp_get_attachment_image_src($image_id,'web', true);
            echo $image_url[0]; ?>" ><img src="<?php bloginfo ('template_url' )?>/img/downloadForWeb.png" alt="download for web" /></a><br />
    
            <a href="<?php $image_id = get_post_thumbnail_id();
            $image_url = wp_get_attachment_image_src($image_id,'print', true);
            echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url' )?>/img/downloadForPrint.png" alt="download for print" /></a><br />
    
            <a href="<?php $image_id = get_post_thumbnail_id();
            $image_url = wp_get_attachment_image_src($image_id,'', true);
            echo $image_url[0]; ?>" class="download"><img src="<?php bloginfo ('template_url' )?>/img/downloadForProPrint.png" alt="download for pro print" /></a><br />
            </p>
            <?php
        }
    }
    

    测试和工作

    推荐文章