Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active July 31, 2017 14:29
Show Gist options
  • Select an option

  • Save igorbenic/7e20e4214e5faa959235ded60a40e994 to your computer and use it in GitHub Desktop.

Select an option

Save igorbenic/7e20e4214e5faa959235ded60a40e994 to your computer and use it in GitHub Desktop.
<?php
add_filter( 'edd_add_to_cart_item', 'dp_check_item_to_add' );
/**
* Check the item we want to add in cart (Step #7)
* @param array $item
* @return array|boolean
*/
function dp_check_item_to_add( $item ) {
if( isset( $item['id'] ) ) {
$download_id = dp_get_download_id();
// Our Download <-> Questionnaire Product is the item
if( $download_id && $download_id == $item['id'] ) {
if( isset( $_REQUEST['questionnaire_id'] ) ) {
$item['options']['questionnaire_id'] = $_REQUEST['questionnaire_id'];
}
// We already have this one
if( edd_item_in_cart( $item['id'], $item['options'] ) ) {
return false;
}
}
}
return $item;
}
<?php
add_filter( 'edd_item_in_cart', 'dp_item_in_cart', 20, 3 );
/**
* Return if the Item is already in the cart
* @param boolean $return
* @param integer $download_id
* @param array $options
* @return boolean
*/
function dp_item_in_cart( $return, $download_id, $options ) {
// The item is already in the cart
if( $return ) {
$dp_download_id = dp_get_download_id();
// This is our Worksheet item
if( $dp_download_id && $dp_download_id == $download_id ) {
$cart = edd_get_cart_contents();
if ( is_array( $cart ) ) {
foreach ( $cart as $item ) {
if ( $item['id'] == $download_id ) {
if ( isset( $options['questionnaire_id'] ) && isset( $item['options']['questionnaire_id'] ) ) {
if ( $options['questionnaire_id'] == $item['options']['questionnaire_id'] ) {
// Yep, same Questionnaire ID for the product
$return = true;
break;
} else {
// The Questionnaire IDs are not the same
$return = false;
break;
}
} else {
$return = false;
break;
}
}
}
}
}
}
return $return;
}
<?php
add_action( 'edd_complete_download_purchase', 'dp_item_download_complete', 20, 5 );
/**
* Set the Questionnaire as paid when the purchase is completed
*/
function dp_item_download_complete( $download_id, $payment_id, $download_type, $download, $cart_index ) {
if( isset( $download['item_number']['options']['questionnaire_id'] ) ) {
$questionnaire = get_post( $download['item_number']['options']['questionnaire_id'] );
// Check if it is not null
if( $questionnaire ) {
// Do something
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment