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

时间:2017-08-17 08:12:31

标签: php wordpress woocommerce categories product

早上好,

我创建了一段代码片段,用于从帖子图片中提取相关信息,并显示3个下载选项。

我已经改变了网站的设计,以便在Woocommerece的产品上运行,我如何调整我的代码片段以使此功能正常工作?

<?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 个答案:

答案 0 :(得分:1)

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

  

但您需要确保ID 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
    }
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

经过测试和工作......