Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscribe to orders #10

Merged
merged 14 commits into from
Nov 15, 2019
Merged

Subscribe to orders #10

merged 14 commits into from
Nov 15, 2019

Conversation

ole1986
Copy link
Owner

@ole1986 ole1986 commented Nov 10, 2019

This PR has its primary focus on moving the subscription concept from product to orders.

  • Provide a setting to select which product types can have a subscription
  • Allow choosing a subscription from shop cart (customer side) when product matches the above product type - code snipped
  • Store additional product fields (E.g. Domain) during the checkout procedure (customer side) as per WC_Order_Item (instead of WC_Order) - code snipped
  • Save the selected subscription into WC_Order meta data after checkout succeeded
  • Show the additional product field data (E.g. Domain) when opening the order from its backend
  • Migrate the previous Domain meta data stored in WC_Order into its individual WC_Order_Item meta data for compatibility reason

@ole1986
Copy link
Owner Author

ole1986 commented Nov 10, 2019

Some useful snipped

<?php

// show the custom meta data on the cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'tp_get_custom_item_data', 10, 2 );
function tp_get_custom_item_data( $item_data, $cart_item ) {
  if( isset( $cart_item['size'] ) ) {  
    $item_data[] = array( 'name' => 'Size', 'value' => $cart_item['size'] );
  }
  return $item_data;
}

// add custom meta to order
add_action( 'woocommerce_new_order_item', 'tp_add_custom_meta_to_order', 99, 3 );
function tp_add_custom_meta_to_order( $item_id, $item, $order_id ) {
  if( ! isset( $item->legacy_values ) ) { // applies to order_item_shipping
    return;
  }
  if( isset( $item->legacy_values['size'] ) ) {
    wc_add_order_item_meta( $item_id, 'Size', $item->legacy_values['size'] );
  }
}

@ole1986
Copy link
Owner Author

ole1986 commented Nov 10, 2019

A good place to add subscribtion section would be in action hook woocommerce_cart_contents

// add the action 
add_action( 'woocommerce_cart_actions', 'action_woocommerce_cart_actions, 10, 0 );

Followed by hook woocommerce_update_cart_action_cart_updated to save its post data

add_action( 'woocommerce_update_cart_action_cart_updated', 'on_action_cart_updated', 20, 1 );
function on_action_cart_updated( $cart_updated ){
    // save some data
    WC()->session->set( 'foo', 'some-data' );
    // after checkout proceeded, get the data to store into WC_Order meta data
    ///WC()->session->get( 'foo' );
}

See: https://stackoverflow.com/a/49496196/2106997

@ole1986 ole1986 merged commit 8a8d7eb into master Nov 15, 2019
@ole1986 ole1986 deleted the subscribe-orders branch February 12, 2020 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant