How to get variable from functions.php to page - php

I'm trying to get $cert_url and use it in my custom page to create a sharable link to Facebook. However, I have no idea to get the variable functions.php and pass it to custom page.
Functions.php
add_action( 'woocommerce_thankyou', 'get_cert_url' );
function get_cert_url( $order_id ) {
global $wpdb;
$msf_campaign_status = array('wc-completed');
$msf_campaign_detail = get_post_meta($order_id);
$msf_campaign_fund = $msf_campaign_detail['msf_charity_fund'][0];
$order = wc_get_order( $order_id );
if( $order->has_status('processing') ){
$name_on_e_cert = str_replace(" ", "+", $msf_campaign_detail['name_on_e_cert'][0]);
$msf_charity_name = get_option('wc_fields_billing');
if($msf_campaign_detail['charity_organizations'][0] == 'msf_charity_1'){
$charity_name = $msf_charity_name['charity_organizations']['options']['msf_charity_1'];
}
elseif ($msf_campaign_detail['charity_organizations'][0] == 'msf_charity_2') {
$charity_name = $msf_charity_name['charity_organizations']['options']['msf_charity_2'];
}
elseif ($msf_campaign_detail['charity_organizations'][0] == 'msf_charity_3') {
$charity_name = $msf_charity_name['charity_organizations']['options']['msf_charity_3'];
}
elseif ($msf_campaign_detail['charity_organizations'][0] == 'msf_charity_4') {
$charity_name = $msf_charity_name['charity_organizations']['options']['msf_charity_4'];
}
$donateto = str_replace(" ", "+", $charity_name);
$subtotal = $order->get_subtotal();
$amount = (($subtotal*25)/100);
$request = wp_remote_retrieve_body(wp_remote_get('https://www.api_website.com.my/api/c/product.ashx?key=api_key&orderid='.$order_id.'&name='.$name_on_e_cert.'&donateto='.$donateto.'&amount='.$amount.''));
$data = json_decode($request);
$result[]= $data;
foreach($result[0] as $line) {
$cert_url = $line;
}
return $cert_url;
}
}
The code below is to create a share button to share the link to Facebook.
Custom page
Share this on Facebook
May I know how can I get the $cert_url and use it on custom page? Thanks in advance !

Thank you for your answer. I solved the problem by using add_query_arg().

Related

How to get all woocommerce shipping packages in a custom template

I need to let customer edit their pending payment order. By default, woocommerce only allow change the payment method. So, I have created a custom template for this feature.
The problem now I encountered is I can't get the shipping packages in the template.
Here is the code that I adapted from the wc_cart_totals_shipping_html() :
$packages = WC()->shipping->get_packages();
print_r($packages);
foreach ( $packages as $i => $package ) {
//blah blah blah
}
The print_r($packages) give me the null array. But on the checkout page, it's working fine.
Any idea why? Or, get the shipping packages by other method?
Please try this -
global $woocommerce;
$customerZipCode = 75098;
$zipResultArr = csd_check_zip_and_state($customerZipCode);
$bh_packages = $woocommerce->cart->get_shipping_packages();
$bh_packages[0]['destination']['state'] = $zipResultArr['state'];
$bh_packages[0]['destination']['postcode'] = $customerZipCode ;
$bh_packages[0]['destination']['city'] = $zipResultArr['city'];
$bh_packages[0]['destination']['address'] = '';
$bh_packages[0]['destination']['address_2'] = '';
//Calculate costs for passed packages
$bh_shipping_methods = array();
foreach( $bh_packages as $bh_package_key => $bh_package ) {
$bh_shipping_methods[$bh_package_key] = $woocommerce->shipping->calculate_shipping_for_package($bh_package, $bh_package_key);
}
$shippingArr = $bh_shipping_methods[0]['rates'];
if(!empty($shippingArr)) {
$response = array();
foreach ($shippingArr as $value) {
$shipping['label'] = $value->label;
$shipping['cost'] = $value->cost;
$response['shipping'][] = $shipping;
}
}
// This is your shipping
print_r($response);

WordPress theme is eliminating my PayPal purchase link's ampersand and all text afterwards

I've encountered a strange error in a theme called "Faith" (by Chimpstudio). When user publishes or updates the page, the PayPal link doesn't write to the database properly.
As far as I'm aware, the value being written to the database is contained in an XML array, like so:
noSimpleXMLElement Object ( [0] => https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick )
The link that should be publishing/updating (before hitting Publish/Update) is:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XXXXXXXXXXXXX
The "&hosted_button_id=XXXXXXXXXXXXX" is not being saved, which means users cannot purchase my client's product.
Potential solution: I've been digging through the theme's .php files, looking for an opportunity to intercept the value being passed to the WordPress database. The idea was to wrap/encode the variable before it gets send to the WP database, with the hope that the entire URL string remains unchanged at the "&hosted_button_id=XXXXXXXXXXXXX" part of the URL.
Any solutions or ideas which may help?
EDIT: This may be a relevant piece of code from the admin_functions.php file:
function events_meta_save($post_id) {
global $wpdb;
if (empty($_POST["event_ticket_price"])){ $_POST["event_ticket_price"] = "";}
if (empty($_POST["event_social_sharing"])){ $_POST["event_social_sharing"] = "";}
if (empty($_POST["event_buy_now"])){ $_POST["event_buy_now"] = "";}
if (empty($_POST["event_phone_no"])){ $_POST["event_phone_no"] = "";}
if (empty($_POST["switch_footer_widgets"])){ $_POST["switch_footer_widgets"] = "";}
if (empty($_POST["event_start_time"])){ $_POST["event_start_time"] = "";}
if (empty($_POST["event_end_time"])){ $_POST["event_end_time"] = "";}
if (empty($_POST["event_all_day"])){ $_POST["event_all_day"] = "";}
if (empty($_POST["event_address"])){ $_POST["event_address"] = "";}
if (empty($_POST["event_ticket_options"])){ $_POST["event_ticket_options"] = "";}
if (empty($_POST["event_map"])){ $_POST["event_map"] = "";}
$sxe = new SimpleXMLElement("<event></event>");
$sxe->addChild('event_ticket_price', $_POST['event_ticket_price'] );
$sxe->addChild('event_social_sharing', $_POST["event_social_sharing"]);
$sxe->addChild('event_buy_now', $_POST["event_buy_now"]);
$sxe->addChild('event_phone_no', $_POST["event_phone_no"]);
$sxe->addChild('switch_footer_widgets', $_POST["switch_footer_widgets"]);
$sxe->addChild('event_start_time', $_POST["event_start_time"]);
$sxe->addChild('event_end_time', $_POST["event_end_time"]);
$sxe->addChild('event_all_day', $_POST["event_all_day"]);
$sxe->addChild('event_ticket_options', $_POST["event_ticket_options"]);
$sxe->addChild('event_address', $_POST["event_address"]);
$sxe->addChild('event_map', $_POST["event_map"]);
echo "<pre>BPOST: ".print_r($_POST, true)."</pre>";
print_r($sxe);
$sxe = save_layout_xml($sxe);
print_r($sxe);
update_post_meta($post_id, 'cs_event_meta', $sxe->asXML());
}
Additionally, here's more code for the XML side from the events.php file:
// event custom fields start
add_action( 'add_meta_boxes', 'cs_event_meta' );
function cs_event_meta()
{
add_meta_box( 'event_meta', 'Event Options', 'cs_event_meta_data', 'events', 'normal', 'high' );
}
function cs_event_meta_data($post) {
$cs_event_meta = get_post_meta($post->ID, "cs_event_meta", true);
global $cs_xmlObject;
if ( $cs_event_meta <> "" ) {
$cs_xmlObject = new SimpleXMLElement($cs_event_meta);
$event_ticket_price = $cs_xmlObject->event_ticket_price;
$event_social_sharing = $cs_xmlObject->event_social_sharing;
$event_start_time = $cs_xmlObject->event_start_time;
$event_end_time = $cs_xmlObject->event_end_time;
$event_all_day = $cs_xmlObject->event_all_day;
$event_address = $cs_xmlObject->event_address;
$event_loc_lat = $cs_xmlObject->event_loc_lat;
$event_loc_long = $cs_xmlObject->event_loc_long;
$event_loc_zoom = $cs_xmlObject->event_loc_zoom;
$testVar = $cs_xmlObject->event_buy_now;
if (strstr($cs_xmlObject->event_buy_now, "&"))
{
echo "yes"; //$event_buy_now = ($cs_xmlObject->event_buy_now . 'FINDME(yes)');
print_r ($testVar);
}
else
{
echo "no"; //$event_buy_now = ($cs_xmlObject->event_buy_now . 'FINDME(no)');
print_r ($testVar);
print_r ($cs_xmlObject);
}
$event_ticket_options = $cs_xmlObject->event_ticket_options;
$event_map = $cs_xmlObject->event_map;
}
else {
$event_ticket_price = '';
$slider_id = '';
$event_social_sharing = '';
$event_related = '';
$event_start_time = '';
$event_end_time = '';
$event_all_day = '';
$event_address = '';
$event_loc_lat = '';
$event_loc_long = '';
$event_loc_zoom = '';
$inside_event_related_post_title = '';
$event_map = '';
$event_buy_now = '';
$event_ticket_options = '';
}
NOTE: Both of these code snippets are located in the parent theme's /include/ directory. At this point, there is no child theme -- and if necessary, a child theme will be created to implement these updates.
Just use jQuery and .change() to detect new content in an input. When the field gets changed, have it change the & to & amp; Might be a dirty hack, but if this is standing between you and payday, have at 'er.

woocommerce - billing order fields change with different countries

I'm working with woocommerce to build a webshop. I've customized the order of the billing address fields. The problem is, some countries still display a different order. How can I force the custom order for all countries?
EDIT:
<?php
/*
* Modifing the order of form fields.
* More information here: http://www.trottyzone.com/change-order-of-woocommerce-fields-on-checkout-page/
*/
add_filter('woocommerce_checkout_fields','reorder_woo_fields');
function reorder_woo_fields($fields) {
//move these around in the order you'd like
$fields2['billing']['billing_first_name'] = $fields['billing']['billing_first_name'];
$fields2['billing']['billing_last_name'] = $fields['billing']['billing_last_name'];
$fields2['billing']['billing_email'] = $fields['billing']['billing_email'];
$fields2['billing']['billing_company'] = $fields['billing']['billing_company'];
$fields2['billing']['billing_address_1'] = $fields['billing']['billing_address_1'];
$fields2['billing']['billing_postcode'] = $fields['billing']['billing_postcode'];
$fields2['billing']['billing_city'] = $fields['billing']['billing_city'];
$fields2['billing']['billing_country'] = $fields['billing']['billing_country'];
$fields2['billing']['billing_state'] = $fields['billing']['billing_state'];
$fields2['billing']['billing_phone'] = $fields['billing']['billing_phone'];
$fields2['shipping']['shipping_first_name'] = $fields['shipping']['shipping_first_name'];
$fields2['shipping']['shipping_last_name'] = $fields['shipping']['shipping_last_name'];
$fields2['shipping']['shipping_email'] = $fields['shipping']['shipping_email'];
$fields2['shipping']['shipping_company'] = $fields['shipping']['shipping_company'];
$fields2['shipping']['shipping_address_1'] = $fields['shipping']['shipping_address_1'];
$fields2['shipping']['shipping_postcode'] = $fields['shipping']['shipping_postcode'];
$fields2['shipping']['shipping_city'] = $fields['shipping']['shipping_city'];
$fields2['shipping']['shipping_country'] = $fields['shipping']['shipping_country'];
$fields2['shipping']['shipping_state'] = $fields['shipping']['shipping_state'];
$fields2['shipping']['shipping_phone'] = $fields['shipping']['shipping_phone'];
//just copying these keeps the standard order
$fields2['account'] = $fields['account'];
$fields2['order'] = $fields['order'];
return $fields2;
}
?>
Check the woocommerce class-wc-countries.php file there it sets the locale. I think your problem can solved by the following code
add_filter( 'woocommerce_get_country_locale', 'use_only_default_locale' );
function use_only_default_locale( $locale ) {
return array();
}
EDIT:
Try this code
add_filter( 'woocommerce_default_address_fields', 'my_default_address_fields' );
function my_default_address_fields( $fields ) {
//move these around in the order you'd like
$fields2['first_name'] = $fields['first_name'];
$fields2['last_name'] = $fields['last_name'];
$fields2['email'] = $fields['email'];
$fields2['company'] = $fields['company'];
$fields2['address_1'] = $fields['address_1'];
$fields2['postcode'] = $fields['postcode'];
$fields2['city'] = $fields['city'];
$fields2['city']['label'] = 'City';
$fields2['city']['placeholder'] = 'City';
$fields2['country'] = $fields['country'];
$fields2['state'] = $fields['state'];
$fields2['phone'] = $fields['phone'];
return $fields2;
}
comment out add_filter('woocommerce_checkout_fields','reorder_woo_fields');
You need to change the field classes a bit to fill properly.
By using the woocommerce_checkout_fields filter you have full controll of the checkout fields.
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
//Minipulate the `$fields` vaiable and return it
$fields['order']['order_comments']['placeholder'] = 'My new placeholder';
return $fields;
}
See the docs for more information: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters

Opencart change languge with currency change options

I am just a newbie to opencart . I have made an ecommerce wesite with it. In that website there is the feature for multilingual with multi currency converter option. Now I want that when a user clicks on his language it should also convert the currency to his language as well. So is it possible to do this with Opencart. Can someone share any codes or any reference link so that I can do that. Any help and suggestions will be highly appreciable. Thanks.
There is a currency option in the opencart admin panel , the path is something like this :
system > localisation > currencies
there you can change and add currency options related to a particular language.
if ($currency && $this->has($currency)) {
$symbol_left = $this->currencies[$currency]['symbol_left'];
$symbol_right = $this->currencies[$currency]['symbol_right'];
$decimal_place = $this->currencies[$currency]['decimal_place'];
} else {
if($_SESSION['language']=="es"):
$symbol_right = $this->currencies[$this->code]['symbol_right'];
else:
$from="USD";
$to="MXN";
$path = "http://www.google.com/ig/calculator?hl=en&q=1".$from."=?".$to;
$rawdata = file_get_contents($path);
$data = explode('"', $rawdata);
$data = explode(' ', $data['3']);
$var = $data['0'];
$e=round($var,3);
if ($e):
$number=$number / $e;
else:
$default_dollar=12.863;
$number=$number / $default_dollar;
endif;
$symbol_right=" USD";
endif;
$symbol_left = $this->currencies[$this->code]['symbol_left'];
$decimal_place = $this->currencies[$this->code]['decimal_place'];
$currency = $this->code;
}
There is a function at :\system\library\currency.php
public function format($number, $currency = '', $value = '', $format = true) {
if ($currency && $this->has($currency)) {
$symbol_left = $this->currencies[$currency]['symbol_left'];
$symbol_right = $this->currencies[$currency]['symbol_right'];
$decimal_place = $this->currencies[$currency]['decimal_place'];
} else {
$symbol_left = $this->currencies[$this->code]['symbol_left'];
$symbol_right = $this->currencies[$this->code]['symbol_right'];
$decimal_place = $this->currencies[$this->code]['decimal_place'];
$currency = $this->code;
}
if you look carefully you can change the currency here as per the language or you can use jquery to send an ajax request for the currency change.

Make Search and Archives match homepage

I have my homepage http://www.faberunashop.com set up as a directory. When you click on the post image, it takes you over to the artists site. Here is the code that I used to make this happen by adding it to the functions.php:
function print_post_title() {
global $post;
$thePostID = $post->ID;
$post_id = get_post($thePostID);
$title = $post_id->post_title;
$perm = get_permalink($post_id);
$post_keys = array(); $post_val = array();
$post_keys = get_post_custom_keys($thePostID);
if (!empty($post_keys)) {
foreach ($post_keys as $pkey) {
if ($pkey=='url1' || $pkey=='title_url' || $pkey=='url_title') {
$post_val = get_post_custom_values($pkey);
}
}
if (empty($post_val)) {
$link = $perm;
} else {
$link = $post_val[0];
}
} else {
$link = $perm;
}
echo '<h2>'.$title.'</h2>';
}
Now I want to do the same to my search and archive page. What do I adjust to make them behave the same?
I suppose that you use WordPress.
In that case you can change the layout and the behavior of your search results by creating a file with name search.php into your theme for your search results, and another file for archives that called archives.php.
For more information about Template Hierarchy for WordPress you can find here http://codex.wordpress.org/Template_Hierarchy

Categories