I have 3 entities: Subjects, Sections, Questions. Everyone has relations with the rest. I am trying to get the questions of the current $user. If there are none, I make a copy of the default ones with user = NULL.
But Doctrine gives me headaches. Initially it wouldn't get questions with null user. Now after flushing the new questions in the database it won't get them (they are inserted correctly).
Probably there is something that I don't see.
Why won't it retrieve the new questions?
<?php
public function getProfileQuestionsForUser( $user ) {
$queryBuilder = $this->createQueryBuilder( 's' );
$query = $queryBuilder
->select( 's, sc, q' )
->leftJoin( 's.sections', 'sc' )
->leftJoin( 'sc.questions', 'q', 'WITH', 'q.user = :USER' )
->where( 's.type = :TYPE' )
->setParameter( 'USER', $user )
->setParameter( 'TYPE', 'profile' )
->getQuery();
$sql = $query->getSQL();
/** #var SubjectEntity $result */
$result = $query->getOneOrNullResult();
$hasQuestions = false;
foreach ( $result->getSections() as $section ) {
$qs = count($section->getQuestions());
if ( count( $section->getQuestions() ) ) {
$hasQuestions = true;
break;
}
}
if ( $hasQuestions == false ) {
$queryBuilder = $this->createQueryBuilder( 's' );
$query = $queryBuilder
->select( 's, q, sc' )
->leftJoin( 's.sections', 'sc' )
->leftJoin( 's.questions', 'q', 'WITH', 'q.user IS NULL' )
->where( 's.type = :TYPE' )
->setParameter( 'TYPE', 'profile' )
->getQuery();
$sql = $query->getSQL();
/** #var SubjectEntity $subject */
$subject = $query->getOneOrNullResult();
if ( ! empty( $subject ) ) {
/** #var QuestionEntity $question */
$qs = count( $subject->getQuestions() );
foreach ( $subject->getQuestions() as $question ) {
$new_question = clone $question;
$new_question->setUser( $user );
$new_question->setAnswers( new ArrayCollection() );
$new_question->setQuestionStatuses( new ArrayCollection() );
$new_question->setOptions( new ArrayCollection() );
$this->getEntityManager()->persist( $new_question );
}
$this->getEntityManager()->flush();
// Retrieve new questions from DB
$queryBuilder = $this->createQueryBuilder( 's' );
$query = $queryBuilder
->select( 's, sc, q' )
->leftJoin( 's.sections', 'sc' )
->leftJoin( 'sc.questions', 'q', 'WITH', 'q.user = :USER' )
->where( 's.type = :TYPE' )
->setParameter( 'USER', $user )
->setParameter( 'TYPE', 'profile' )
->getQuery();
$sql = $query->getSQL();
$result = $query->getOneOrNullResult();
}
}
return $result;
}
Maybe something like this will work:
//In your Question entity add this
public function __clone() {
$this->id = null;
}
And modify your code like this:
foreach ( $subject->getQuestions() as $question ) {
$new_question = clone $question;
$this->getEntityManager()->detach($new_question);
$new_question->setUser( $user );
$new_question->setAnswers( new ArrayCollection() );
$new_question->setQuestionStatuses( new ArrayCollection() );
$new_question->setOptions( new ArrayCollection() );
$this->getEntityManager()->persist( $new_question );
}
$this->getEntityManager()->flush();
UPDATE
Hum, Here there is a more complete answer
Related
I've tried to copy my php code from foreach loop using ob start but failed. Does anyone knows how to do this? I wanted to add my php code in another function like below.
foreach($kekei as $good => $goodness) {
ob_start();
$GLOBALS['myfirstbowanstart'] .= "if ( $packing_fee === 'strtolower(".$goodness['shipping_code'].")' ) {
$label = __('Shipping fee');
$cost = ".$goodness['shipping_fee'].";
}"; // put here your recursive function name
ob_get_clean();
}
// Add a custom dynamic packaging fee
add_action( 'woocommerce_cart_calculate_fees', 'add_packaging_fee', 20, 1 );
function add_packaging_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
//$domain = "woocommerce";
$packing_fee = WC()->session->get( 'the_chosen_one' ); // Dynamic packing fee
echo $GLOBALS['myfirstbowanstart'];
/*
if ( $packing_fee === 'gzeatp1' ) {
$label = __("Shipping fee");
$cost = 3.00;
} elseif ( $packing_fee === 'box' ) {
$label = __("Shipping fee");
$cost = 9.00;
}
*/
if ( isset($cost) )
$cart->add_fee( $label, $cost );
}
<?php
$GLOBALS = array("results"=>array(), "tests" => array("test1", "test2", "test3"));
$label = $cost = "";
$packing_fees = array("gzeatp1" => "3.00", "box" => "9.00", "cotton" => "12.00");
$packing_fee = "cotton"; //WC()->session->get( 'the_chosen_one' );
$kekei = array(
"first_good" => array("param1" => "value1", "shipping_code" => "gzeatp1"),
"second_good"=> array("param2" => "value2", "shipping_code" => "box"),
"third_good" => array("param3" => "value3", "shipping_code" => "cotton"),
);
$callback =
function ($shipping_code, $packing_fee) use ($cost, &$packing_fees)
{
if($packing_fee === strtolower($shipping_code)) {
$label = 'Shipping fee';
$cost = $packing_fees[$shipping_code];
$GLOBALS["results"] = array("label" => $label, "cost"=> $cost);
}
};
foreach($kekei as $good => $goodness) {
$callback($goodness['shipping_code'], $packing_fee) ;
}
// Add a custom dynamic packaging fee
//add_action( 'woocommerce_cart_calculate_fees', 'add_packaging_fee', 20, 1 );
function add_packaging_fee( $cart = "" ) {
//Disabled for testing
#if ( is_admin() && ! defined( 'DOING_AJAX' ) )
# return;
//$domain = "woocommerce";
//$packing_fee = WC()->session->get( 'the_chosen_one' ); // Dynamic packing fee
// If $label and $cost are global variables, comment out next 2 lines.
$label = $GLOBALS["results"]["label"];
$cost = $GLOBALS["results"]["cost"];
/*
if ( $packing_fee === 'gzeatp1' ) {
$label = __("Shipping fee");
$cost = 3.00;
} elseif ( $packing_fee === 'box' ) {
$label = __("Shipping fee");
$cost = 9.00;
}
*/
echo json_encode($GLOBALS);
//if ( isset($cost) )
//$cart->add_fee( $label, $cost );
}
add_packaging_fee("test");
This is the modified code merged with yours, let me know if it works,
//Added this block
$packing_fees = array("gzeatp1" => "3.00", "box" => "9.00", "cotton" => "12.00"); //This is for testing purposes
$packing_fee = WC()->session->get( 'the_chosen_one' ); // make this a global variable
$callback =
function ($shipping_code, $packing_fee) use ($cost, &$packing_fees)
{
if($packing_fee === strtolower($shipping_code)) {
$label = __('Shipping fee');
$cost = $packing_fees[$shipping_code];
$GLOBALS["myfirstbowanstart"] = array("label" => $label, "cost"=> $cost);
}
};
foreach($kekei as $good => $goodness) {
$callback($goodness['shipping_code'], $packing_fee) ;
}
/* //Removed this block
foreach($kekei as $good => $goodness) {
ob_start();
$GLOBALS['myfirstbowanstart'] .= "if ( $packing_fee === 'strtolower(".$goodness['shipping_code'].")' ) {
$label = __('Shipping fee');
$cost = ".$goodness['shipping_code'].";
}"; // put here your recursive function name
ob_get_clean();
}
*/
// Add a custom dynamic packaging fee
add_action( 'woocommerce_cart_calculate_fees', 'add_packaging_fee', 20, 1 );
function add_packaging_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
//$domain = "woocommerce";
$packing_fee = WC()->session->get( 'the_chosen_one' ); // Dynamic packing fee
//changed this line
//echo $GLOBALS['myfirstbowanstart'];
$label = $GLOBALS['myfirstbowanstart']['label'];
$cost = $GLOBALS['myfirstbowanstart']['cost'];
/*
if ( $packing_fee === 'gzeatp1' ) {
$label = __("Shipping fee");
$cost = 3.00;
} elseif ( $packing_fee === 'box' ) {
$label = __("Shipping fee");
$cost = 9.00;
}
*/
if ( isset($cost) )
$cart->add_fee( $label, $cost );
}
I'm trying to insert a custom function in the function.php to copy more simply how to make plug-ins prepared, the data of a form filled out and sent. server sends me an error:
Parse error: syntax error, unexpected '$formDB' (T_VARIABLE) in C:\AppServ\www\ITI\SITO\wp-content\plugins\contact-form-7\includes\functions.php on line 375
this is my code:
remove_all_filters ('wpcf7_before_send_mail');
add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );
function contactform7_before_send_mail( $form_to_DB ) {
global $wpdb;
$submission = WPCF7_Submission::get_instance();
if ( $submission )
$formDB = $form_to_DB->get_posted_data();
$datains = now();
$username = $formDB['your-name'] $formDB['your-surname'];
$email = $formDB['your-email'];
$tel = $formDB['your-tel'];
$orario = $formDB['orario'];
$CentriITI = $formDB['CentriITI'];
$citta = $formDB['citta'];
$fasciadieta = $formDB['fasciadieta'];
$checkbox-info-sedi = $formDB['checkbox-info-sedi'];
$checkbox-checkup = $formDB['checkbox-checkup'];
$object = $formDB['your-object'];
$checkbox-trapianto = $formDB['checkbox-trapianto'];
$textarea-testo = $formDB['textarea-testo'];
$wpdb->insert( 'wp_form', array( 'datains' =>; $datains_form ), array( 'username_form' =>; $username ), array( 'email_form' =>; $email ) ,array( 'tel_form' =>; $tel ), array( 'orario_form' =>; $orario ), array( 'centro_form' =>; $centro ), array( 'citta_form' =>; $citta ), array( 'fasciaeta_form' =>; $fasciaeta ), array( 'object_form' =>; $object ), array( 'trapianto_form' =>; $checkbox-trapianto ) );
}
Thanks for the answers
I am writing a business extension (pdf printing) for the smart coupons.
Now I need a relation from the generated coupon (of the order) and the order item.
For example, which order item has generated the coupon?
Is there a way to get the order item_id from the coupon?
Is use this code to get the coupons:
$coupons = get_post_meta($order_id, 'sc_coupon_receiver_details', true));
Thank you very much
I have found a solution, but i had to put the code inside "woocommerce-smart-coupons.php" file.
Line 379: Extend the Filter Variables
add_filter( 'generate_smart_coupon_action', array( $this, 'generate_smart_coupon_action' ), 1, 10 );
Line 4783: Extend the Variable with the $item_id
if( $this->is_coupon_amount_pick_from_product_price( array( $coupon_title ) ) ) {
$email_to_credit[$receivers_emails[$coupon->id][0]][] = $coupon->id . ':' . $sc_called_credit_details[$item_id] . ':' . $item_id;
} else {
$email_to_credit[$receivers_emails[$coupon->id][0]][] = $coupon->id . ':' . $coupon->amount . ':' . $item_id;
}
Line 4816: Get the $item_id from the details and refer it to the "generate_smart_coupon" method
foreach ( $email_to_credit[$email_id] as $coupon_credit => $qty ) {
$coupon_details = explode( ':', $coupon_credit );
$coupon_title = get_the_title( $coupon_details[0] );
$coupon = new WC_Coupon( $coupon_title );
$credit_amount = $coupon_details[1];
$item_id = $coupon_details[2];
$message_index = array_search( $email_id, $email[$coupon->id], true );
if ( $message_index !== false && isset( $receivers_messages[$coupon->id][$message_index] ) && !empty( $receivers_messages[$coupon->id][$message_index] ) ) {
$message_from_sender = $receivers_messages[$coupon->id][$message_index];
} else {
$message_from_sender = '';
}
for ( $i = 0; $i < $qty; $i++ ) {
if ( $coupon->type != 'smart_coupon' ) continue; // only process smart_coupon here, rest coupon will be processed by function update_coupon
$this->generate_smart_coupon( $email_id, $credit_amount, $order_id, $coupon, 'smart_coupon', $gift_certificate_receiver_name, $message_from_sender, $gift_certificate_sender_name, $gift_certificate_sender_email, $item_id );
$smart_coupon_codes = array();
}
}
Line 5194: Change the Method and extend the variables
public function generate_smart_coupon( $email, $amount, $order_id = '', $coupon = '', $discount_type = 'smart_coupon', $gift_certificate_receiver_name = '', $message_from_sender = '', $gift_certificate_sender_name = '', $gift_certificate_sender_email = '', $item_id = '' ) {
return apply_filters( 'generate_smart_coupon_action', $email, $amount, $order_id, $coupon, $discount_type, $gift_certificate_receiver_name, $message_from_sender, $gift_certificate_sender_name, $gift_certificate_sender_email, $item_id );
}
Line 5212: Extend the filter method to
public function generate_smart_coupon_action( $email, $amount, $order_id = '', $coupon = '', $discount_type = 'smart_coupon', $gift_certificate_receiver_name = '', $message_from_sender = '', $gift_certificate_sender_name = '', $gift_certificate_sender_email = '', $item_id = '' ) {
Line 5307: Store the $item_id in the post_meta
update_post_meta( $smart_coupon_id, 'apply_before_tax', $apply_before_tax );
update_post_meta( $smart_coupon_id, 'free_shipping', $free_shipping );
update_post_meta( $smart_coupon_id, 'product_categories', $product_categories );
update_post_meta( $smart_coupon_id, 'exclude_product_categories', $exclude_product_categories );
update_post_meta( $smart_coupon_id, 'generated_from_order_id', $order_id );
update_post_meta( $smart_coupon_id, 'generated_from_item_id', $item_id );
I have removed pagination conditions from my wordpress page but it is still showing limited results and i want to show all results.
This is my originial code :
$seller_id = get_current_user_id();
$order_status = isset( $_GET['order_status'] ) ? sanitize_key( $_GET['order_status'] ) : 'all';
$pages = explode('/',$_SERVER['REQUEST_URI']);
if(isset($pages) && $pages[4]!=''){ $paged = $pages[4]; }else{ $paged = 1;}
$limit = 10;
$offset = ( $paged - 1 ) * $limit;
$user_orders = dokan_get_seller_orders( $seller_id, $order_status, $limit, $offset );
and this is my modified code, it is still only showing 10 results :
$seller_id = get_current_user_id();
$order_status = isset( $_GET['order_status'] ) ? sanitize_key( $_GET['order_status'] ) : 'all';
$user_orders = dokan_get_seller_orders( $seller_id, $order_status);
It can be done using :
$order_count = dokan_get_seller_orders_number( $seller_id, $order_status );
$limit = $order_count;
$offset = 0;
$user_orders = dokan_get_seller_orders( $seller_id, $order_status, $limit, $offset );
add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode');
function deny_pobox_postcode( $posted ) {
global $woocommerce;
$address = ( isset( $posted['shipping_address_1'] ) ) ? $posted['shipping_address_1'] : $posted['billing_address_1'];
$postcode = ( isset( $posted['shipping_postcode'] ) ) ? $posted['shipping_postcode'] : $posted['billing_postcode'];
$replace = array(" ", ".", ",");
$address = strtolower( str_replace( $replace, '', $address ) );
$postcode = strtolower( str_replace( $replace, '', $postcode ) );
if ( strstr( $address, 'pobox' ) || strstr( $postcode, 'pobox' ) ) {
$woocommerce->add_error( "Sorry, we don't ship to PO BOX addresses." );
}
}
i am getting:
fatal error :call to undefined add_error
when i am pasting on my function .php
add_error() has been renamed to wc_add_notice():
$woocommerce->wc_add_notice( "Sorry, we don't ship to PO BOX addresses." );
add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode');
function deny_pobox_postcode( $posted ) {
global $woocommerce;
$address = ( isset( $posted['shipping_address_1'] ) ) ?
$posted['shipping_address_1'] : $posted['billing_address_1'];
$postcode = ( isset( $posted['shipping_postcode'] ) ) ?
$posted['shipping_postcode'] : $posted['billing_postcode'];
$replace = array(" ", ".", ",");
$address = strtolower( str_replace( $replace, '', $address ) );
$postcode = strtolower( str_replace( $replace, '', $postcode ) );
if ( strstr( $address, 'pobox' ) || strstr( $postcode, 'pobox' ) ) {
$notice = sprintf( __( '%1$sSorry, we dont ship to PO BOX addresses..' , 'error' ) , '<strong>' , '</strong>' );
if ( version_compare( WC_VERSION, '2.3', '<' ) ) {
$woocommerce->add_error( $notice );
} else {
wc_add_notice( $notice, 'error' );
}
}
}
<?php
add_filter('woocommerce_package_rates',
'shomaris_hide_fedex_for_po_box_shipment',
10, 2);
function shomaris_hide_fedex_for_po_box_shipment($available_shipping_methods,
$package){
$shipping_method_to_hide = 'flat_rate:4';
// $shipping_method_to_hides = 'flat_rate:5';
global $woocommerce;
$address = ( !empty( $woocommerce->customer->get_shipping_address_1() ) ) ?
$woocommerce->customer->get_shipping_address_1() : $woocommerce->customer-
>get_billing_address_1();
$postcode = ( !empty( $woocommerce->customer->get_shipping_postcode() ) ) ?
$woocommerce->customer->get_shipping_postcode() : $woocommerce->customer-
>get_billing_postcode();
$replace = array(" ", ".", ",");
$address2 = strtolower( str_replace( $replace, '', $address ) );
if ( strstr( $address2, 'pobox' ) ) {
foreach ($available_shipping_methods as $shipping_method => $value) {
if( strpos( $shipping_method, $shipping_method_to_hide,
$shipping_method_to_hides ) !== false ) {
unset($available_shipping_methods[$shipping_method]);
}
}
}
return $available_shipping_methods;
}
?>