在ROEN主题中过滤销售Flash徽章的WooCommerce功能时,产品图像消失

时间:2015-09-26 19:09:12

标签: php wordpress woocommerce add-filter

我正在尝试显示用户在WooCommerce中保存销售徽章的百分比。为了使这项工作没有这个覆盖"特色"销售徽章,我不得不在我的functions.php中添加一些过滤器。现在一切正常,产品页面上的产品图片消失了,我不能理解为什么。 " onsale特色"主题文件product-image.php中的代码几乎与" onsale"相同。代码,我无法理解为什么当产品在销售时图像会消失,而不是在产品出现时消失?

我在functions.php中的代码

add_filter('woocommerce_sale_flash', 'my_custom_sale_flash' );
function my_custom_sale_flash($text) {
    global $product;
    if($product->is_on_sale()){
        $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
        return '<span class="onsale">SPAR '.$percentage.' %</span>';  
    }elseif($product->is_featured()){
    return '<span class="onsale featured">Utvalgt</span>';
    }else if(!$product->is_in_stock()){
        return '<span class="onsale">Utsolgt</span>';
    }else  if(!$product->get_price()){
        return '<span class="onsale">GRATIS</span>';
}
} 

ROEN主题中的product-image.php中的代码

<?php
/**
 * Single Product Image
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.0.14
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $post, $woocommerce, $product;

?>
<div class="images">

    <?php
        if ( has_post_thumbnail() ) {

            $image_title        = "";
            $image_link         = wp_get_attachment_url( get_post_thumbnail_id() );
            $image              = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), array('title' => $image_title) );

            $attachment_count   = count( $product->get_gallery_attachment_ids() );

            if ( $attachment_count > 0 ) {
                $gallery = '[product-gallery]';
                $zoom = "";
            } else {
                $gallery = '';
                $zoom = 'zoom';
            }



            $html = '';

            if($product->is_featured()){
                $html = apply_filters('woocommerce_sale_flash', '<span class="featured onsale">'.__('Featured','ROEN').'</span>', $post, $product);
            }else if($product->is_on_sale()){
                $html = apply_filters('woocommerce_sale_flash', '<span class="onsale">'.__( 'Sale!', 'woocommerce' ).'</span>', $post, $product);
            }else if(!$product->is_in_stock()){
                $html = '<span class="outofstock">'.__('Out of Stock','ROEN').'</span>';
            }else  if(!$product->get_price()){
                $html = '<span class="free onsale">'.__('Free','ROEN').'</span>';
            }

            echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<a href="%s" itemprop="image" class="woocommerce-main-image '.$zoom.'" title="%s"  rel="prettyPhoto' . $gallery . '" data-url="%s">%s'.$html.'</a>', $image_link, $image_title, $image_link ,  $image ), $post->ID );
        } else {

            echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src() ), $post->ID );

        }
    ?>

    <?php do_action( 'woocommerce_product_thumbnails' ); ?>

</div>

我无法理解问题出现的原因?从类别和产品页面查看时,未销售的特色产品可正确显示。从类别中查看时,正在销售的产品会正确显示,但图像会在产品页面上消失。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

我通过改变

来解决这个问题

echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<a href="%s" itemprop="image" class="woocommerce-main-image '.$zoom.'" title="%s" rel="prettyPhoto' . $gallery . '" data-url="%s">%s'.$html.'</a>', $image_link, $image_title, $image_link , $image ), $post->ID );

echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<a href="%s" itemprop="image" class="woocommerce-main-image '.$zoom.'" title="%s" rel="prettyPhoto' . $gallery . '" data-url="%s">%s%s</a>', $image_link, $image_title, $image_link , $image, $html ), $post->ID );

原因是由于ROEN主题的编码方式,Wordpress不喜欢销售徽章中显示的跨度中的百分号。

我告诉你那个男人,那个主题中很多奇怪的代码。