Woocommerce:根据元数据值过滤订单内的下载

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

我的电子商务是这样工作的:我有一些可下载的产品,其中附有一个可供下载的文件列表。

但是,我需要根据用户添加到购物车时所做的选择来过滤可用的下载。需要存储这些选项,以便根据所选选项“过滤”客户可用的下载。因此,将下载文件的名称与数组中的字符串进行比较应该可以解决问题。 示例: 下载产品包含名为

Weight 1、Weight 2、Weight 3 等的下载。

用户在添加到购物车时选择Weight 1Weight 3。 用户只能获取名为 Weight 1Weight 3 的文件。 到目前为止,这些选择已成功以字符串数组的形式存储到产品项元数据中。在

functions.php

中使用以下函数: // save selected weights into meta for minicart add_filter( 'woocommerce_add_cart_item_data', 'save_font_weights', 10, 3 ); function save_font_weights( $cart_item_data, $product_id, $variation_id ) { $fw = $_POST['font_weights']; if( !empty($fw) ) { $cart_item_data[ 'selected_font_weights' ] = $fw; // below statement make sure every add to cart action as unique line item $cart_item_data['unique_key'] = md5( microtime().rand() ); WC()->session->set( 'session_fw', $fw ); } return $cart_item_data; } // output custom Item value in Cart and Checkout pages add_filter( 'woocommerce_get_item_data', 'output_font_weights', 10, 2 ); function output_font_weights( $cart_data, $cart_item ) { if( isset( $cart_item['selected_font_weights'] ) ) { $cart_data[] = array( 'key' => __('Selected weights', 'woocommerce'), 'value' => $cart_item['selected_font_weights'], 'display' => $cart_item['selected_font_weights'], ); } return $cart_data; } // save cart item custom meta as order item meta data and display it everywhere on orders and email notifications. add_action( 'woocommerce_checkout_create_order_line_item', 'save_weights_to_order', 10, 4 ); function save_weights_to_order( $item, $cart_item_key, $values, $order ) { if( isset($values['selected_font_weights']) && ! empty($values['selected_font_weights']) ) { $item->update_meta_data( 'Selected weights', $values['selected_font_weights']); } $product_id = $item['product_id']; $w = get_post_meta( $product_id, 'selected_font_weights', true ); wc_add_order_item_meta($item, 'Selected weights', $w , true); }

通过这种方式,可以一直访问“已收到订单”页面的数据。
我现在正在努力的是过滤此页面和电子邮件等中可用的下载。
是否有一个钩子可以通过将文件名与数组中列出的字符串进行比较来过滤下载?

php wordpress woocommerce download product
1个回答
0
投票

// save selected weights as custom cart item data add_filter( 'woocommerce_add_cart_item_data', 'save_font_weights' ); function save_font_weights( $cart_item_data ) { if( isset($_POST['font_weights']) && !empty($_POST['font_weights']) ) { $cart_item_data['font_weights'] = wc_clean($_POST['font_weights']); $cart_item_data['unique_key'] = md5( microtime().rand() ); } return $cart_item_data; } // Display custom cart item data in mini cart, Cart and Checkout pages add_filter( 'woocommerce_get_item_data', 'output_font_weights', 10, 2 ); function output_font_weights( $cart_data, $cart_item ) { if( isset($cart_item['font_weights']) ) { $cart_data[] = array( 'key' => __('Selected weights', 'woocommerce'), 'value' => $cart_item['font_weights'] ); } return $cart_data; } // save cart item custom data as order item metadata and display it everywhere on orders and email notifications. add_action( 'woocommerce_checkout_create_order_line_item', 'save_weights_to_order_item', 10, 4 ); function save_weights_to_order_item( $item, $cart_item_key, $values, $order ) { if( isset($values['font_weights']) ) { $item->update_meta_data( 'Selected weights', $values['font_weights']); } }

下面除了现有代码之外我还使用了 2 个新函数:

我按产品添加选定的字体粗细作为订单元数据
  • 我过滤显示的下载(您需要找出正确的下载值来与选定的权重进行比较,以仅保留选定的字体权重。因此,您将用正确的变量替换
  • $some_variable_to_replace
  • 以与您的数组进行比较所选权重的数量
    (假设是一个数组)
    .
  • 注意:在最后一个函数中,
我首先列出了您可以从每个下载项目中获取的所有数据

,并将其放入您可以使用的变量中。 附加功能:

// save cart item custom data by product as order metadata add_action( 'woocommerce_checkout_create_order', 'save_weights_to_order' ); function save_weights_to_order( $order ) { $weights_by_product = array(); // Loop through cart items foreach( WC()->cart->get_cart() as $item ) { if( isset($item['font_weights']) ) { $weights_by_product[$item['data']->get_id()] = $item['font_weights']; } } if( !empty($weights_by_product) ) { // save weights as order metadata $order->update_meta_data( '_selected_weights', $weights_by_product); } } // filter downloadable items add_filter( 'woocommerce_order_get_downloadable_items', 'filter_order_downloadable_items', 10, 2 ); function filter_order_downloadable_items( $downloads, $order ) { foreach ( $downloads as $key => $download ) { // All available data in the $download variable $download_url = $download['download_url']; $file_name = $download['file']['name']; $file = $download['file']['file']; $download_id = $download['download_id']; $product_id = $download['product_id']; $product_name = $download['product_name']; $product_url = $download['product_url']; $download_name = $download['download_name']; $order_id = $download['order_id']; $order_key = $download['order_key']; $downloads_remaining = $download['downloads_remaining']; $access_expires = $download['access_expires']; // Get all selected weights by product if ( $weights_by_product = $order->get_meta('_selected_weights') ) { // Get the selected weights for the product ID $selected_weights = (array) $weights_by_product[$product_id]; // Here below, REPLACE $some_variable_to_compare with the correct variable if ( ! in_array( $some_variable_to_compare, $selected_weights ) ) { unset($downloads[$key]); // Remove the font weight if not selected } } } return $downloads; }

代码位于tour子主题的functions.php文件中(或插件中)。应该可以。

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