CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Reposted on 2023-10-01
by LoicTheAztec

Original Post

Original - Posted on 2023-09-30
by LoicTheAztec



            
Present in both answers; Present only in the new answer; Present only in the old answer;

To make *"this md5 value to be saved only when"* specific products are in cart (or in order items), we need a reusable conditional function that will check items: ```php // Conditional function: Check if specific products are in cart or order items function has_items_for_email_md5( $object_items ) { $found_120 = $found_260 = false; // Initializing foreach ( $object_items as $item ) { $product_id = isset($item['product_id']) ? $item['product_id'] : $item->get_product_id(); $variation_id = isset($item['variation_id']) ? $item['variation_id'] : $item->get_variation_id();
if ( in_array('120', [$product_id , $variation_id]) ) $found_120 = true; if ( in_array('260', [$product_id , $variation_id]) ) $found_260 = true; } return ( $found_120 && $found_260 ); } ``` Now we can make some small changes to our previous code, that will use this conditional function: ```php // When order is created after checkout add_action( 'woocommerce_checkout_create_order', 'save_billing_email_md5', 20, 2 ); function save_billing_email_md5( $order, $data ) { if ( ! has_items_for_email_md5( WC()->cart->get_cart() ) ) return;
if( $billing_email = $order->get_billing_email() ) { $order->add_meta_data('_billing_email_md5', md5($billing_email)); } }
// When an order is manually created/updated in admin add_action( 'woocommerce_process_shop_order_meta', 'admin_save_billing_email_md5' ); function admin_save_billing_email_md5( $order_id ) { $order = wc_get_order( $order_id );
if ( ! has_items_for_email_md5( $order->get_items() ) ) return;
if( $billing_email = $order->get_billing_email() ) { update_post_meta($order_id, '_billing_email_md5', md5($billing_email)); } } ``` The last function stay unchanged: ```php add_filter( 'woocommerce_shop_order_search_fields', 'extending_admin_orders_search_field', 10, 1 ); function extending_admin_orders_search_field( $meta_keys ){ $meta_keys[] = '_billing_email'; $meta_keys[] = '_billing_phone'; $meta_keys[] = '_payment_method_title'; $meta_keys[] = '_billing_email_md5'; // <=== HERE
return $meta_keys; } ``` It should work.
------
Then to execute some code after purchasing those two specific products (IDs 120 and 260), we will use our conditional function as follows (from an order ID): ```php $order = wc_get_order( $order_id ); // Get WC_Order object (if needed)
if ( has_items_for_email_md5( $order->get_items() ) ) { $pro_message = __('Hi dear');
echo $pro_message . __('custom php code'); } ```
First, you need to save as custom metadata the **email md5** when the order is created after checkout: ```php add_action( 'woocommerce_checkout_create_order', 'save_billing_email_md5', 20, 2 ); function save_billing_email_md5( $order, $data ) { if( $billing_email = $order->get_billing_email() ) { $order->add_meta_data('_billing_email_md5', md5($billing_email)); } } ``` Then also when an order is manually created/updated in admin, add the following: ```php add_action( 'woocommerce_process_shop_order_meta', 'admin_save_billing_email_md5' ); function admin_save_billing_email_md5( $order_id ) {
if( $billing_email = get_post_meta($order_id, '_billing_email', true) ) { update_post_meta($order_id, '_billing_email_md5', md5($billing_email)); } } ``` Then replace function `extending_admin_orders_search_field()` with: ```php add_filter( 'woocommerce_shop_order_search_fields', 'extending_admin_orders_search_field', 10, 1 ); function extending_admin_orders_search_field( $meta_keys ){ $meta_keys[] = '_billing_email'; $meta_keys[] = '_billing_phone'; $meta_keys[] = '_payment_method_title'; $meta_keys[] = '_billing_email_md5'; // <=== HERE
return $meta_keys; } ``` Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
This will only work for orders that have the billing email md5 set as custom metadata.


        
Present in both answers; Present only in the new answer; Present only in the old answer;