Woocommerce我的帐户

Woocommerce我的帐户

本文介绍了在Woocommerce我的帐户下载部分显示产品图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个似乎无法解决的问题.我正在尝试将产品图片添加到我的帐户/下载/"部分的每个项目中.我尝试了发现的多个代码,但似乎没有一个适合我.

I’ve got a problem that I can’t seem to figure out. I’m trying to add the product image to each item in the my-account/downloads/ section. I have tried multiple codes that I found but none seem to work for me.

我正在使用3个插件,而所有其他插件都被禁用,试图弄清楚这一点.这3个插件是必需的,因此我需要将其与激活的这3个插件一起使用.

I am using 3 plugins with all other plugins disabled trying to figure this out. These 3 plugins are required, so I need this to work with all 3 of these plugins activated.

这3个插件是:

Woocommerce会员资格

Woocommerce订阅

Woocommerce订阅下载

我所有的产品都是虚拟的,因此在下载"部分中的名称旁边需要有一张图片.我在这里拔头发...无论如何,我尝试过的东西.

All my products are virtual so I need an image next to their name in the downloads section. I'm pulling my hair out here... Anyway, to the things I've tried.

我已经尝试过在此表单发布中找到的代码

function my_account_downloads_column_download_product( $download ) {
    // Get $product object from product ID
    $product = wc_get_product( $download['product_id'] );

    // Get thumbnail
    $thumbnail = $product->get_image(array( 36, 36)); // Get the product thumbnail (from product object)

    // Image found
    if( $product->get_image_id() > 0 ) {
        $item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>';
        echo $item_name;
    }

    echo '<a href="' . $product->get_permalink() . '">' . $product->get_name() . '</a>';
 }

我还尝试了该表格中显示的其他代码,但它们对我不起作用.

I also tried the other codes present in that form but they didn’t work for me.

基于 将产品图片添加到Woocommerce我的帐户订单视图 答案代码中,该代码在视图订单页面上非常有用,我进行了一些更改,以使其适用于my-account/downloads/部分.

Based on Add the product image to Woocommerce my account order view answer code, which works great for the view-order page, I have made some changes trying to make it work for my-account/downloads/ section.

这是我的代码尝试:

// Display the product thumbnail in order view pages
add_filter( 'woocommerce_my_account_my_downloads_title', 'display_product_image_in_downloads', 20, 3
    );
function display_product_image_in_downloads( $item_name, $item, $is_visible ) {
    // Targeting view order pages only
    if( is_wc_endpoint_url( 'downloads' ) ) {
        $product       = $item->get_product(); // Get the WC_Product object (from order item)
        $product_image = $product->get_image(array( 80, 80)); // Get the product thumbnail (from product
    object)
        $item_name     = '<div class="item-thumbnail">' . $product_image . '</div>' . $item_name;
    }
    return $item_name;
}

我将 woocommerce_my_account_my_downloads_title 更改为我可以在 Woocommerce挂钩参考页面

但是,它们似乎都不适合我.

However, none of them seem to work for me.

我试图通过将模板文件复制到childtheme/woocommerce/order/order-downloads.php,以及childtheme/woocommerce/myaccount/downloads.php和childtheme/woocommerce/myaccount/my-downloads.php来修改模板文件.我无法执行任何操作,因此我将其从childtheme中删除,并在childthemes functions.php文件中尝试了上面的代码.

I tried to modify the template files by copying them to my childtheme / woocommerce / order / order-downloads.php, also childtheme / woocommerce / myaccount / downloads.php, and childtheme / woocommerce / myaccount / my-downloads.php. I couldn’t get any of that to work so I removed it from my childtheme and tried the codes above in my childthemes functions.php file.

我搜寻了互联网,试图找到解决方案,但是每个人似乎都不适合我.我觉得 Woocommerce订阅下载可能是问题所在,但这对我来说是必须的插件.我可能只是做不正确的事情或错过了一些事情,但是我似乎找不到答案.

I have scoured the internet trying to find a solution but, every one of them don’t seem to work for me. I have a feeling that Woocommerce Subscriptions Downloads could be the problem, but it's a must plugin for me. I probably just didn’t do something right or missed something but I can’t seem to find out what.

有人可以帮我解决这个问题吗?那太好了.

Can someone help me figure this out? That would be awesome.

我想要的只是可下载产品旁边的产品图片.我只出售可下载的产品,并且有数百种产品.图像只会帮助客户更加轻松地找到他们想要下载的内容.

All I want is the product image next to the downloadable product. I only sell downloadable products and there are hundreds of products. An image would just help the customer find what they want to download so much easier.

推荐答案

@LoicTheAztec的答案有效,但是我做了一点修改,原因是:
订购数字产品时,也会在 order-received 端点上调用 order-downloads.php 内容.因此,当使用 if(!is_wc_endpoint_url('downloads'))return; -它将在/my-account/downloads 页面上显示图片,但在/checkout/order-received 将变为空.没有产品标题,也没有图像.

@LoicTheAztec 's answer works but I changed it a little bit, because:
order-downloads.php content is also called on the order-received endpoint when a Digital product is ordered.So when using if ( ! is_wc_endpoint_url( 'downloads' ) ) return; - it will show the image on the /my-account/downloads page but the Product title field on the /checkout/order-received will become empty. No product title, and no image.

因此,我通过更改以下内容解决了该问题:
如果(!is_wc_endpoint_url('downloads'))返回;

if(is_wc_endpoint_url(array('downloads','order-received')))return;

So, I solved it by changing:
if ( ! is_wc_endpoint_url( 'downloads' ) ) return;
to
if ( is_wc_endpoint_url( array ('downloads' , 'order-received') )) return;

这篇关于在Woocommerce我的帐户下载部分显示产品图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 05:30