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

zf2sa74q  于 2023-10-17  发布在  WordPress
关注(0)|答案(1)|浏览(161)

我的电子商务是这样运作的:我有一些可下载的产品,其中有一个文件列表附加到它,可供下载。
但是,我需要过滤器的下载根据用户将添加到购物车时所作的选择。需要存储这些选项,以便根据所选选项对客户可用的下载进行 * 筛选 *。因此,将下载文件的名称与数组中的字符串进行比较就可以了。
示例:下载产品包含名为Weight 1、Weight 2、Weight 3等的下载。用户在添加到购物车时选择Weight 1Weight 3。用户只获取这些文件,称为Weight 1Weight 3
到目前为止,这些选择已成功地存储到产品项Meta数据中作为字符串数组。在 functions.php 中使用以下函数:

  1. // save selected weights into meta for minicart
  2. add_filter( 'woocommerce_add_cart_item_data', 'save_font_weights', 10, 3 );
  3. function save_font_weights( $cart_item_data, $product_id, $variation_id ) {
  4. $fw = $_POST['font_weights'];
  5. if( !empty($fw) ) {
  6. $cart_item_data[ 'selected_font_weights' ] = $fw;
  7. // below statement make sure every add to cart action as unique line item
  8. $cart_item_data['unique_key'] = md5( microtime().rand() );
  9. WC()->session->set( 'session_fw', $fw );
  10. }
  11. return $cart_item_data;
  12. }
  13. // output custom Item value in Cart and Checkout pages
  14. add_filter( 'woocommerce_get_item_data', 'output_font_weights', 10, 2 );
  15. function output_font_weights( $cart_data, $cart_item ) {
  16. if( isset( $cart_item['selected_font_weights'] ) ) {
  17. $cart_data[] = array(
  18. 'key' => __('Selected weights', 'woocommerce'),
  19. 'value' => $cart_item['selected_font_weights'],
  20. 'display' => $cart_item['selected_font_weights'],
  21. );
  22. }
  23. return $cart_data;
  24. }
  25. // save cart item custom meta as order item meta data and display it everywhere on orders and email notifications.
  26. add_action( 'woocommerce_checkout_create_order_line_item', 'save_weights_to_order', 10, 4 );
  27. function save_weights_to_order( $item, $cart_item_key, $values, $order ) {
  28. if( isset($values['selected_font_weights']) && ! empty($values['selected_font_weights']) ) {
  29. $item->update_meta_data( 'Selected weights', $values['selected_font_weights']);
  30. }
  31. $product_id = $item['product_id'];
  32. $w = get_post_meta( $product_id, 'selected_font_weights', true );
  33. wc_add_order_item_meta($item, 'Selected weights', $w , true);
  34. }

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

siv3szwd

siv3szwd1#

你的代码中有一些错误和不必要的东西。请使用以下选项:

  1. // save selected weights as custom cart item data
  2. add_filter( 'woocommerce_add_cart_item_data', 'save_font_weights' );
  3. function save_font_weights( $cart_item_data ) {
  4. if( isset($_POST['font_weights']) && !empty($_POST['font_weights']) ) {
  5. $cart_item_data['font_weights'] = wc_clean($_POST['font_weights']);
  6. $cart_item_data['unique_key'] = md5( microtime().rand() );
  7. }
  8. return $cart_item_data;
  9. }
  10. // Display custom cart item data in mini cart, Cart and Checkout pages
  11. add_filter( 'woocommerce_get_item_data', 'output_font_weights', 10, 2 );
  12. function output_font_weights( $cart_data, $cart_item ) {
  13. if( isset($cart_item['font_weights']) ) {
  14. $cart_data[] = array(
  15. 'key' => __('Selected weights', 'woocommerce'),
  16. 'value' => $cart_item['font_weights']
  17. );
  18. }
  19. return $cart_data;
  20. }
  21. // save cart item custom data as order item metadata and display it everywhere on orders and email notifications.
  22. add_action( 'woocommerce_checkout_create_order_line_item', 'save_weights_to_order_item', 10, 4 );
  23. function save_weights_to_order_item( $item, $cart_item_key, $values, $order ) {
  24. if( isset($values['font_weights']) ) {
  25. $item->update_meta_data( 'Selected weights', $values['font_weights']);
  26. }
  27. }

下面我使用2个新的功能,除了现有的代码:

  • 我将所选的字体权重按产品添加为订单元数据 (假设这是一个逗号分隔的权重字符串)。在保存之前,我将这个逗号分隔的字符串转换为选定权重的数组。
  • 我过滤显示的下载(您需要找出正确的下载值与选定的权重进行比较,以仅保留选定的字体权重。因此,您将用正确的变量替换$some_variable_to_replace,以便与所选权重数组进行比较

注意:在最后一个函数中,我首先列出了您可以从每个下载项目中获取的所有数据,并将其放入您可以使用的变量中。
附加功能:

  1. // save cart item custom data by product as order metadata
  2. add_action( 'woocommerce_checkout_create_order', 'save_weights_to_order' );
  3. function save_weights_to_order( $order ) {
  4. $weights_by_product = array(); // Initializing
  5. // Loop through cart items
  6. foreach( WC()->cart->get_cart() as $item ) {
  7. if( isset($item['font_weights']) ) {
  8. $font_weights = str_replace(', ', ',', $item['font_weights']); // removing white spaces
  9. $font_weights = explode(',', $font_weights); // converting to an array
  10. $weights_by_product[$item['data']->get_id()] = $font_weights;
  11. }
  12. }
  13. if( !empty($weights_by_product) ) {
  14. // save weights as order metadata
  15. $order->update_meta_data( '_selected_weights', $weights_by_product);
  16. }
  17. }
  18. // filter downloadable items
  19. add_filter( 'woocommerce_order_get_downloadable_items', 'filter_order_downloadable_items', 10, 2 );
  20. function filter_order_downloadable_items( $downloads, $order ) {
  21. foreach ( $downloads as $key => $download ) {
  22. // All available data in the $download variable
  23. $download_url = $download['download_url'];
  24. $file_name = $download['file']['name'];
  25. $file = $download['file']['file'];
  26. $download_id = $download['download_id'];
  27. $product_id = $download['product_id'];
  28. $product_name = $download['product_name'];
  29. $product_url = $download['product_url'];
  30. $download_name = $download['download_name'];
  31. $order_id = $download['order_id'];
  32. $order_key = $download['order_key'];
  33. $downloads_remaining = $download['downloads_remaining'];
  34. $access_expires = $download['access_expires'];
  35. // Get all selected weights by product
  36. if ( $weights_by_product = $order->get_meta('_selected_weights') ) {
  37. // Get the selected weights for the product ID
  38. $selected_weights = (array) $weights_by_product[$product_id];
  39. // Here below, REPLACE $some_variable_to_replace with the correct variable
  40. if ( ! in_array( $some_variable_to_replace, $selected_weights ) ) {
  41. unset($downloads[$key]); // Remove the font weight if not selected
  42. }
  43. }
  44. }
  45. return $downloads;
  46. }

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

展开查看全部

相关问题