Handle wiaas documents
This commit is contained in:
@@ -12,6 +12,11 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
*/
|
||||
class Wiaas_Cart {
|
||||
|
||||
private static $cart_doc_types = array(
|
||||
'template_questionaire' => 'order_questionaire',
|
||||
'template_agreement' => 'order_agreement'
|
||||
);
|
||||
|
||||
|
||||
public static function init() {
|
||||
add_action( 'woocommerce_checkout_create_order_line_item', array( __CLASS__, 'add_order_item_meta' ), 10, 3 );
|
||||
@@ -24,6 +29,76 @@ class Wiaas_Cart {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve cart documents (templates and uploded documents)
|
||||
* @return array
|
||||
*/
|
||||
public static function get_cart_documents() {
|
||||
$templates = self::_get_cart_templates();
|
||||
$uploaded_documents = self::_get_cart_uploaded_documents();
|
||||
|
||||
$has_pending_uploads = false;
|
||||
|
||||
foreach ($templates as $type => $package_documents) {
|
||||
$uploaded_type = self::$cart_doc_types[$type];
|
||||
|
||||
if (!isset($uploaded_documents[$uploaded_type])) {
|
||||
$has_pending_uploads = true;
|
||||
break;
|
||||
}
|
||||
|
||||
$uploaded_package_documents = $uploaded_documents[$uploaded_type];
|
||||
|
||||
foreach ($package_documents as $package_id => $package_document) {
|
||||
if (!isset($uploaded_package_documents[$package_id])) {
|
||||
$has_pending_uploads = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'templates' => $templates,
|
||||
'uploaded' => $uploaded_documents,
|
||||
'pending' => $has_pending_uploads
|
||||
);
|
||||
}
|
||||
|
||||
/** Uploaded document for cart
|
||||
*
|
||||
* @param $doc_type
|
||||
* @param $package_item_key
|
||||
*
|
||||
* @return string | WP_Error
|
||||
*/
|
||||
public static function upload_cart_document($doc_type, $package_item_key) {
|
||||
try {
|
||||
$version = Wiaas_Document_Upload::upload_document_version();
|
||||
|
||||
if (is_wp_error($version)) {
|
||||
return $version;
|
||||
}
|
||||
|
||||
$cart_document = array(
|
||||
'version' => $version,
|
||||
'key' => wp_generate_uuid4(),
|
||||
);
|
||||
|
||||
// persist uploaded cart document in cart
|
||||
WC()->cart->cart_contents[ $package_item_key ]['_wiaas_documents'][$doc_type] = $cart_document;
|
||||
|
||||
// persist changes to cart session
|
||||
// Note: TODO Refactor this logic of persisting updates made to cart
|
||||
$s = new WC_Cart_Session(WC()->cart);
|
||||
$s->set_session();
|
||||
|
||||
return $cart_document['key'];
|
||||
|
||||
} catch( Exception $e) {
|
||||
return new WP_Error('wiass_cart_upload_error', 'Error while uploading cart file!');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles adding standard wiaas package to cart along with selected addons and options
|
||||
*
|
||||
@@ -61,7 +136,8 @@ class Wiaas_Cart {
|
||||
'_wiaas_addon_items' => array(),
|
||||
'_wiaas_option_items' => array(),
|
||||
'_wiaas_currency' => isset($country) ? $country['currency'] : get_woocommerce_currency(),
|
||||
'_wiaas_payment' => $package_prices[$selected_price_index] ? $package_prices[$selected_price_index] : null
|
||||
'_wiaas_payment' => $package_prices[$selected_price_index] ? $package_prices[$selected_price_index] : null,
|
||||
'_wiaas_documents' => array()
|
||||
);
|
||||
|
||||
$cart_item_key = WC()->cart->add_to_cart($package_id, 1, 0, array(), $wiaas_cart_item_data);
|
||||
@@ -79,8 +155,6 @@ class Wiaas_Cart {
|
||||
return true;
|
||||
|
||||
} catch( Exception $e) {
|
||||
|
||||
error_log($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -181,7 +255,7 @@ class Wiaas_Cart {
|
||||
* @param $cart_item
|
||||
* @param $order
|
||||
*
|
||||
* @return array
|
||||
* @return WC_Order_Item
|
||||
*/
|
||||
public static function add_order_item_meta( $order_item, $cart_item_key, $cart_item ) {
|
||||
if (wc_pb_is_bundle_container_cart_item($cart_item) && isset($cart_item['_wiaas_payment'])) {
|
||||
@@ -224,6 +298,40 @@ class Wiaas_Cart {
|
||||
if (isset($cart_item['_wiaas_addon_for'])) {
|
||||
$order_item->add_meta_data( '_wiaas_addon_for', $cart_item['_wiaas_addon_for'], true );
|
||||
}
|
||||
|
||||
// add documents associated with the item
|
||||
$cart_documents = isset($cart_item['_wiaas_documents']) ? $cart_item['_wiaas_documents'] : array();
|
||||
$attachment_document_ids = wiaas_get_object_attached_documents($cart_item['product_id']);
|
||||
$item_documents = array();
|
||||
|
||||
foreach ($cart_documents as $type => $cart_document) {
|
||||
$item_documents[] = array(
|
||||
'key' => $cart_document['key'],
|
||||
'version' => $cart_document['version'],
|
||||
'type' => $type
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($attachment_document_ids as $attachment_document_id) {
|
||||
$doc_info = Wiaas_Document::get_doc_info($attachment_document_id);
|
||||
|
||||
// add customer visible attachment documents to order
|
||||
if ($doc_info['visible'] || !$doc_info['type']) {
|
||||
$item_documents[] = array(
|
||||
'key' => wp_generate_uuid4(),
|
||||
'name' => $doc_info['name'],
|
||||
'version' => $doc_info['version'],
|
||||
'type' => $doc_info['type']['id'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($item_documents) > 0) {
|
||||
$order_item->add_meta_data( '_wiaas_documents', $item_documents, true );
|
||||
}
|
||||
|
||||
|
||||
return $order_item;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -249,6 +357,7 @@ class Wiaas_Cart {
|
||||
'_wiaas_option_group_name',
|
||||
'_wiaas_standard_package',
|
||||
'_wiaas_currency',
|
||||
'_wiaas_documents'
|
||||
) );
|
||||
}
|
||||
|
||||
@@ -401,6 +510,103 @@ class Wiaas_Cart {
|
||||
|
||||
//PRIVATE
|
||||
|
||||
/**
|
||||
* Retrieve cart templates
|
||||
* @return array
|
||||
*/
|
||||
private static function _get_cart_templates() {
|
||||
$items = WC()->cart->get_cart_contents();
|
||||
|
||||
$documents_ids = array();
|
||||
|
||||
foreach ($items as $key => $item) {
|
||||
if (!isset($item['_wiaas_standard_package'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$package_documents_ids = get_post_meta(
|
||||
$item['product_id'],
|
||||
'_wiaas_attached_documents',
|
||||
true);
|
||||
|
||||
foreach ($package_documents_ids as $package_document_id) {
|
||||
$package_document_id = absint($package_document_id);
|
||||
$documents_ids[$package_document_id] ?: array();
|
||||
$documents_ids[$package_document_id][] = $item['product_id'];
|
||||
}
|
||||
}
|
||||
|
||||
$q = new WP_Query();
|
||||
$retrieved_items = $q->query(array(
|
||||
'post_status' => 'publish',
|
||||
'post_type' => 'wiaas_doc',
|
||||
'post__in' => array_keys($documents_ids),
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'wiaas_doc_type',
|
||||
'field' => 'slug',
|
||||
'terms' => array_keys(self::$cart_doc_types),
|
||||
)
|
||||
)
|
||||
));
|
||||
|
||||
$cart_documents = array();
|
||||
|
||||
foreach ($retrieved_items as $retrieved_item) {
|
||||
|
||||
$doc_info = Wiaas_Document::get_doc_info($retrieved_item->ID);
|
||||
$type = $doc_info['type']['id'];
|
||||
|
||||
$cart_documents[$type] ?: array();
|
||||
|
||||
$package_ids = $documents_ids[$doc_info['id']] ?: array();
|
||||
foreach ($package_ids as $package_id) {
|
||||
$cart_documents[$type][$package_id] = array(
|
||||
'id' => $doc_info['id'],
|
||||
'name' => $doc_info['name'],
|
||||
'type' => $type,
|
||||
'extension' => $doc_info['extension']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $cart_documents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve cart uploaded documents
|
||||
* @return array
|
||||
*/
|
||||
private static function _get_cart_uploaded_documents() {
|
||||
$items = WC()->cart->get_cart_contents();
|
||||
|
||||
$cart_documents = array();
|
||||
|
||||
foreach ($items as $key => $item) {
|
||||
if (!isset($item['_wiaas_standard_package'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$package_id = $item['product_id'];
|
||||
|
||||
$documents = isset($item['_wiaas_documents']) ? $item['_wiaas_documents'] : array();
|
||||
|
||||
foreach ($documents as $type => $document) {
|
||||
$cart_documents[$type] ?: array();
|
||||
$cart_documents[$type][$package_id] = array(
|
||||
'key' => $document['key'],
|
||||
'name' => wiaas_get_doc_version_filename($document['version']),
|
||||
'type' => $type,
|
||||
'extension' => wiaas_get_doc_version_extension($document['version'])
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $cart_documents;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add selected package options and addons after parent standard package is added to cart
|
||||
|
||||
Reference in New Issue
Block a user