• I’m trying to add a custom function to the functions.php page yet I seem to be getting an internal server error. Any ideas?

    The offending code string is this:

    $query1 = "SELECT order_item_id, order_item_name FROMwp_woocommerce_order_itemsWHERE order_item_type = 'line_item' AND order_id = $order_id ";

    When I attempt to execute the sql it shows an internal server error message on the store checkout page.

    Here is a portion of what does work…
    The queries are in the middle of this and have been taken out.

    add_action("woocommerce_checkout_order_processed", "hubspot_tie_in");
    
    function hubspot_tie_in($order_id) {
    global $woocommerce;
    
    $checkout = $woocommerce->checkout();
    $order = new WC_Order( $order_id );
    
    do_action( 'woocommerce_email_before_order_table', $order, true );
    
    $order_number_formatted = $checkout->get_value( 'order_number' );
    $billing_country        = $checkout->get_value( 'billing_country' );
    $billing_first_name     = $checkout->get_value( 'billing_first_name' );
    $billing_last_name      = $checkout->get_value( 'billing_last_name' );
    $billing_address_1      = $checkout->get_value( 'billing_address_1' );
    $billing_address_2      = $checkout->get_value( 'billing_address_2' );
    $billing_city           = $checkout->get_value( 'billing_city' );
    $billing_state          = $checkout->get_value( 'billing_state' );
    $billing_postcode       = $checkout->get_value( 'billing_postcode' );
    $billing_email          = $checkout->get_value( 'billing_email' );
    $billing_phone          = $checkout->get_value( 'billing_phone' );
    $order_shipping         = $checkout->get_value( 'order_shipping' );
    $order_discount         = $checkout->get_value( 'order_discount' );
    $total_tax              = $checkout->get_value( 'order_tax' );
    $order_total            = $checkout->get_value( 'order_total' );
    $customer_user_agent    = $checkout->get_value( 'customer_user_agent' );
    
    $store_used = 'MyWebStore.com';
    $base_currency = 'USD';
    $lifecyclestage = 'customer';
    $hs_context = array(
    'eyemagine_order_item_product_url_1' => '$eyemagine_order_item_product_url_1',
    'eyemagine_order_item_product_url_2' => '$eyemagine_order_item_product_url_2',
    'eyemagine_order_item_product_url_3' => '$eyemagine_order_item_product_url_3',
    'store_used' => '$store_used',
    'country' => '$billing_country',
    'firstname' => '$billing_first_name',
    'lastname' => '$billing_last_name',
    'address' => '$billing_address_1',
    'address+address' => '$billing_address_2',
    'city' => '$billing_city',
    'state' => '$billing_state',
    'zip' => '$billing_postcode',
    'email' => '$billing_email',
    'phone' => '$billing_phone',
    'lifecyclestage' => '$lifecyclestage',
    'eyemagine_order_increment_id' => '$order_number',
    'eyemagine_order_created_at' => '$completed_date',
    'eyemagine_order_item_product_name_1' => '$eyemagine_order_item_product_name_1',
    'eyemagine_order_item_product_name_2' => '$eyemagine_order_item_product_name_2',
    'eyemagine_order_item_product_name_3' => '$eyemagine_order_item_product_name_3',
    'eyemagine_order_base_currency' => '$base_currency',
    'eyemagine_order_subtotal' => '$order_subtotal',
    'eyemagine_order_discount' => '$order_discount',
    'eyemagine_order_tax' => '$total_tax',
    'eyemagine_order_shipping' => '$order_shipping',
    'eyemagine_order_grand_total' => '$order_total'
    );
    
    $hs_context_json = json_encode($hs_context);
    $str_post = "firstname=" . urlencode($billing_first_name) . "&lastname=" . urlencode($billing_last_name) . "&lifecyclestage=" . urlencode($lifecyclestage) . "&eyemagine_order_item_product_url_1=" . urlencode($eyemagine_order_item_product_url_1) . "&eyemagine_order_item_product_url_2=" . urlencode($eyemagine_order_item_product_url_2) . "&eyemagine_order_item_product_url_3=" . urlencode($eyemagine_order_item_product_url_3) . "&store_used=" . urlencode($store_used) . "&country=" . urlencode($billing_country) . "&address=" . urlencode($billing_address_1) . "&address+address=" . urlencode($billing_address_2) . "&city=" . urlencode($billing_city) . "&state=" . urlencode($billing_state) . "&zip=" . urlencode($billing_postcode) . "&email=" . urlencode($billing_email) . "&phone=" . urlencode($billing_phone) . "&eyemagine_order_increment_id=" . urlencode($order_number) . "&eyemagine_order_created_at=" . urlencode($completed_date) . "&eyemagine_order_item_product_name_1=" . urlencode($eyemagine_order_item_product_name_1) . "&eyemagine_order_item_product_name_2=" . urlencode($eyemagine_order_item_product_name_2) . "&eyemagine_order_item_product_name_3=" . urlencode($eyemagine_order_item_product_name_3) . "&eyemagine_order_base_currency=" . urlencode($base_currency) . "&eyemagine_order_subtotal=" . urlencode($order_subtotal) . "&eyemagine_order_discount=" . urlencode($order_discount) . "&eyemagine_order_tax=" . urlencode($total_tax) . "&eyemagine_order_shipping=" . urlencode($order_shipping) . "&eyemagine_order_grand_total=" . urlencode($order_total) . "&hs_context=" . urlencode($hs_context_json);
    
    //I replaced the values in this URL for Privacy
    $endpoint = 'https://forms.hubspot.com/uploads/form/v2/MyHubspotID/MyHubspotFormGUID';
    $ch = @curl_init();
    @curl_setopt($ch, CURLOPT_POST, true);
    @curl_setopt($ch, CURLOPT_POSTFIELDS, $str_post);
    @curl_setopt($ch, CURLOPT_URL, $endpoint);
    @curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $hs_response = @curl_exec($ch); //Log the response from HubSpot as needed.
    @curl_close($ch);
    
    }
    

    I’d be grateful for any help.

    Thanks!

    Steve

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,

    Please specify the changes made in “function.php” are made by woocommerce filter?

    and give the page Url in which the error occur.

    Thanks.

    Thread Starter LordGoran

    (@lordgoran)

    The URL would be the order recieved page https://www.MyWebStore.com/checkout/order-received/

    Here is the full code that causes the error on the functions.php page.

    What it does is this. When the order received page is displayed, it fires this function and sends the purchase data to a hubspot form using the hubspot API.

    add_action("woocommerce_checkout_order_processed", "hubspot_tie_in");
    
    function hubspot_tie_in($order_id) {
    global $woocommerce;
    
    $checkout = $woocommerce->checkout();
    $order = new WC_Order( $order_id );
    
    do_action( 'woocommerce_email_before_order_table', $order, true );
    
    $order_number_formatted = $checkout->get_value( 'order_number' );
    $billing_country        = $checkout->get_value( 'billing_country' );
    $billing_first_name     = $checkout->get_value( 'billing_first_name' );
    $billing_last_name      = $checkout->get_value( 'billing_last_name' );
    $billing_address_1      = $checkout->get_value( 'billing_address_1' );
    $billing_address_2      = $checkout->get_value( 'billing_address_2' );
    $billing_city           = $checkout->get_value( 'billing_city' );
    $billing_state          = $checkout->get_value( 'billing_state' );
    $billing_postcode       = $checkout->get_value( 'billing_postcode' );
    $billing_email          = $checkout->get_value( 'billing_email' );
    $billing_phone          = $checkout->get_value( 'billing_phone' );
    $order_shipping         = $checkout->get_value( 'order_shipping' );
    $order_discount         = $checkout->get_value( 'order_discount' );
    $total_tax              = $checkout->get_value( 'order_tax' );
    $order_total            = $checkout->get_value( 'order_total' );
    $customer_user_agent    = $checkout->get_value( 'customer_user_agent' );
    
    //Begin: When the below is included I get the error
    
    $connection = mysql_connect($dbhost,$dbuser,$dbpassword) or die ("Error code: 1");
    $db = mysql_select_db($dbdatabase,$connection) or die ("Error code: 2");
    
    $query = "SELECT * FROM <code>wp_postmeta</code> WHERE post_id = '$order_id' ";
    $result = mysql_query($query);
    while ($r = mysql_fetch_array($result)) {
    	extract ($r);
    if($meta_key == '_order_shipping'){$order_shipping = $meta_value;}
    if($meta_key == '_order_discount'){$order_discount = $meta_value;}
    if($meta_key == '_order_tax'){$order_tax = $meta_value;}
    if($meta_key == '_order_shipping_tax'){$order_shipping_tax = $meta_value;}
    if($meta_key == '_order_total'){$order_total = $meta_value;}
    if($meta_key == '_order_number_formatted'){$order_number = $meta_value;}
    if($meta_key == '_completed_date'){$completed_date = $meta_value;}
    }
    
    $i= '1';
    $item_name = array();
    $line_subtotal = array();
    $quantity = array();
    $query1 = "SELECT order_item_id, order_item_name FROM <code>wp_woocommerce_order_items</code> WHERE order_item_type = 'line_item' AND order_id = '$order_id' ";
    $result1 = mysql_query($query1) or die("Error Code 3");
    while ($r1 = mysql_fetch_array($result1)) {
    	extract ($r1);
    
    $order_item_url[$i] = strtolower($order_item_name);
    $item_name[$i] = $order_item_name;
    
        $query2 = "SELECT * FROM <code>wp_1_woocommerce_order_itemmeta</code> WHERE order_item_id = '$order_item_id' ";
        $result2 = mysql_query($query2);
        while ($r2 = mysql_fetch_array($result2)) {
        	extract ($r2);
            if($meta_key == '_qty'){$quantity[$i] = $meta_value;}
            if($meta_key == '_line_subtotal'){$line_subtotal[$i] = $meta_value;}
        }
    $i++;
    }
    
    //End: When the above is included I get the error
    
    $store_used = 'MyWebStore.com';
    $base_currency = 'USD';
    $lifecyclestage = 'customer';
    $hs_context = array(
    'eyemagine_order_item_product_url_1' => '$eyemagine_order_item_product_url_1',
    'eyemagine_order_item_product_url_2' => '$eyemagine_order_item_product_url_2',
    'eyemagine_order_item_product_url_3' => '$eyemagine_order_item_product_url_3',
    'store_used' => '$store_used',
    'country' => '$billing_country',
    'firstname' => '$billing_first_name',
    'lastname' => '$billing_last_name',
    'address' => '$billing_address_1',
    'address+address' => '$billing_address_2',
    'city' => '$billing_city',
    'state' => '$billing_state',
    'zip' => '$billing_postcode',
    'email' => '$billing_email',
    'phone' => '$billing_phone',
    'lifecyclestage' => '$lifecyclestage',
    'eyemagine_order_increment_id' => '$order_number',
    'eyemagine_order_created_at' => '$completed_date',
    'eyemagine_order_item_product_name_1' => '$eyemagine_order_item_product_name_1',
    'eyemagine_order_item_product_name_2' => '$eyemagine_order_item_product_name_2',
    'eyemagine_order_item_product_name_3' => '$eyemagine_order_item_product_name_3',
    'eyemagine_order_base_currency' => '$base_currency',
    'eyemagine_order_subtotal' => '$order_subtotal',
    'eyemagine_order_discount' => '$order_discount',
    'eyemagine_order_tax' => '$total_tax',
    'eyemagine_order_shipping' => '$order_shipping',
    'eyemagine_order_grand_total' => '$order_total'
    );
    
    $hs_context_json = json_encode($hs_context);
    $str_post = "firstname=" . urlencode($billing_first_name) . "&lastname=" . urlencode($billing_last_name) . "&lifecyclestage=" . urlencode($lifecyclestage) . "&eyemagine_order_item_product_url_1=" . urlencode($eyemagine_order_item_product_url_1) . "&eyemagine_order_item_product_url_2=" . urlencode($eyemagine_order_item_product_url_2) . "&eyemagine_order_item_product_url_3=" . urlencode($eyemagine_order_item_product_url_3) . "&store_used=" . urlencode($store_used) . "&country=" . urlencode($billing_country) . "&address=" . urlencode($billing_address_1) . "&address+address=" . urlencode($billing_address_2) . "&city=" . urlencode($billing_city) . "&state=" . urlencode($billing_state) . "&zip=" . urlencode($billing_postcode) . "&email=" . urlencode($billing_email) . "&phone=" . urlencode($billing_phone) . "&eyemagine_order_increment_id=" . urlencode($order_number) . "&eyemagine_order_created_at=" . urlencode($completed_date) . "&eyemagine_order_item_product_name_1=" . urlencode($eyemagine_order_item_product_name_1) . "&eyemagine_order_item_product_name_2=" . urlencode($eyemagine_order_item_product_name_2) . "&eyemagine_order_item_product_name_3=" . urlencode($eyemagine_order_item_product_name_3) . "&eyemagine_order_base_currency=" . urlencode($base_currency) . "&eyemagine_order_subtotal=" . urlencode($order_subtotal) . "&eyemagine_order_discount=" . urlencode($order_discount) . "&eyemagine_order_tax=" . urlencode($total_tax) . "&eyemagine_order_shipping=" . urlencode($order_shipping) . "&eyemagine_order_grand_total=" . urlencode($order_total) . "&hs_context=" . urlencode($hs_context_json);
    
    //I replaced the values in this URL for Privacy
    $endpoint = 'https://forms.hubspot.com/uploads/form/v2/MyHubspotID/MyHubspotFormGUID';
    $ch = @curl_init();
    @curl_setopt($ch, CURLOPT_POST, true);
    @curl_setopt($ch, CURLOPT_POSTFIELDS, $str_post);
    @curl_setopt($ch, CURLOPT_URL, $endpoint);
    @curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $hs_response = @curl_exec($ch); //Log the response from HubSpot as needed.
    @curl_close($ch);
    
    }
    
    Thread Starter LordGoran

    (@lordgoran)

    Never Mind. Found the problem. I was using mysql_ and I should have been using mysqli_

    We upgraded to php7

    Thanks anyway.

    Steve

    Thank you for using weblizar’s theme. Let us know for further query.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Function’ is closed to new replies.