更改 WooCommerce 中的 download_url

问题描述 投票:0回答:2

我想更改 WooCommerce 中“我的帐户下载”部分的

download_url

  • 当前网址:http://localhost/i/?download_file=
  • 新网址:http://localhost/i/account/downloads/?download_file=

因此我使用下面的钩子将 home_url( '/' ) 更改为 home_url( '/account/downloads/' )

function filter_woocommerce_customer_available_downloads( $downloads, $customer_id ) { 
    $downloads   = array();
    $_product    = null;
    $order       = null;
    $file_number = 0;

    // Get results from valid orders only.
    $results = wc_get_customer_download_permissions( $customer_id );

    if ( $results ) {
        foreach ( $results as $result ) {
            $order_id = intval( $result->order_id );

            if ( ! $order || $order->get_id() !== $order_id ) {
                // New order.
                $order    = wc_get_order( $order_id );
                $_product = null;
            }

            // Make sure the order exists for this download.
            if ( ! $order ) {
                continue;
            }

            // Check if downloads are permitted.
            if ( ! $order->is_download_permitted() ) {
                continue;
            }

            $product_id = intval( $result->product_id );

            if ( ! $_product || $_product->get_id() !== $product_id ) {
                // New product.
                $file_number = 0;
                $_product    = wc_get_product( $product_id );
            }

            // Check product exists and has the file.
            if ( ! $_product || ! $_product->exists() || ! $_product->has_file( $result->download_id ) ) {
                continue;
            }

            $download_file = $_product->get_file( $result->download_id );

            // If the downloadable file has been disabled (it may be located in an untrusted location) then do not return it.
            if ( ! $download_file->get_enabled() ) {
                continue;
            }

            // Download name will be 'Product Name' for products with a single downloadable file, and 'Product Name - File X' for products with multiple files.
            $download_name = apply_filters(
                'woocommerce_downloadable_product_name',
                $download_file['name'],
                $_product,
                $result->download_id,
                $file_number
            );

            $downloads[] = array(
                'download_url'        => add_query_arg(
                    array(
                        'download_file' => $product_id,
                        'order'         => $result->order_key,
                        'email'         => rawurlencode( $result->user_email ),
                        'key'           => $result->download_id,
                    ),
                    home_url( '/account/downloads/' )
                ),
                'download_id'         => $result->download_id,
                'product_id'          => $_product->get_id(),
                'product_name'        => $_product->get_name(),
                'product_url'         => $_product->is_visible() ? $_product->get_permalink() : '', // Since 3.3.0.
                'download_name'       => $download_name,
                'order_id'            => $order->get_id(),
                'order_key'           => $order->get_order_key(),
                'downloads_remaining' => $result->downloads_remaining,
                'access_expires'      => $result->access_expires,
                'file'                => array(
                    'name' => $download_file->get_name(),
                    'file' => $download_file->get_file(),
                ),
            );

            $file_number++;
        }
    }
    return $downloads; 
} add_filter( 'woocommerce_customer_available_downloads', 'filter_woocommerce_customer_available_downloads', 10, 2 );

代码基于

wc_get_customer_available_downloads( $customer_id )
函数,可以在 /includes/wc-user-functions.php 文件中找到

可以总结一下hook吗?

php wordpress woocommerce hook-woocommerce
2个回答
2
投票

woocommerce_customer_available_downloads
过滤器钩子确实允许您更改
download_url
,但是没有必要通过钩子重写整个函数,因为您只想更改某个部分。 这可以使用以下代码来完成,该代码在代码中使用
str_replace()
:

/**
 * Function for `woocommerce_customer_available_downloads` filter-hook.
 * 
 * @param  $downloads   
 * @param  $customer_id 
 *
 * @return 
 */
function filter_woocommerce_customer_available_downloads( $downloads, $customer_id ) {
    // Only on my account downloads section
    if ( ! is_wc_endpoint_url( 'downloads' ) )
        return $downloads;

    // Loop though downloads
    foreach( $downloads as $key => $download ) {
        // Replace
        $downloads[$key]['download_url'] = str_replace( '/?download_file', '/account/downloads/?download_file', $download['download_url'] );
    }

    return $downloads;
}
add_filter( 'woocommerce_customer_available_downloads', 'filter_woocommerce_customer_available_downloads', 10, 2 );

0
投票

很抱歉两年多后我又把这条线砍掉了。这是与我正在尝试处理的用例相关的最相关的线程。

我的目标是能够将访客用户的下载放在某个位置后面的同一域上,而该位置又可以从页面缓存中排除。与此处使用

my-account/downloads
所做的类似。

我现在想弄清楚的问题是如何为访客用户应用类似的解决方案

order-received
wc_endpoint_url

推荐的解决方案非常适合登录用户。

我已经使用一些可用的 Woocommerce filters 尝试了几次迭代并搜索“下载”,但无法获得有效的实现。

© www.soinside.com 2019 - 2024. All rights reserved.