douwei7203 2015-09-26 19:09
浏览 37

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

I am trying to display how many percent the user saves on the sales badge in WooCommerce. To make this work without this overwriting the "Featured" sales badge, I had to add some filters to my functions.php. Everything works fine now, exept that the product image on the product page disappears and I can't for the life of me understand why. The "onsale featured" code in the theme file product-image.php is almost the same as the "onsale" code, and I can't understand why the image would disappear when a product is on sale, but not when it is featured?

My code in 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>';
}
} 

The code in product-image.php in the ROEN theme

<?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>

I can't see why the problem arises? Featured products that are not on sale displays correctly both when viewed from a category and on the product page. Products that are on sale displays correctly when viewed from a category, but the image disappears on the product page.

Thank you for your time.

  • 写回答

1条回答 默认 最新

  • douhu2898 2015-09-27 15:44
    关注

    I got this fixed by changing

    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 );

    To

    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 );

    Turns out Wordpress did not like the percentage sign in my span that was going to show in the sales badge, because of the way the ROEN theme is coded.

    I tell you man, lot's of weird code in that theme.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么