WPML Woocommerce Multilingual does not support currency setting depending on user location, so we made our own code:
function geoIPLocator() {
global $woocommerce_wpml;
$currency='EUR';
$geo=new WC_Geolocation();
$geo->init();
$country=$geo::geolocate_ip($geo::get_ip_address() );
if(isset($_SESSION['locator'])) {
if($_SESSION['locator']['IP']= =$_SERVER['REMOTE_ADDR'] && strlen($_SESSION['locator']['IP' ])>0) {
$woocommerce_wpml->multi_currency_support->s et_client_currency($_SESSION['locator'][ 'currency']);
return;
}
}
if($country['country']=="RU" || $country['country']=="BY") {
$woocommerce_wpml->multi_currency_support->s et_client_currency('RUB');
$currency='RUB';
} else {
$woocommerce_wpml->multi_currency_support->s et_client_currency('EUR');
$currency='EUR';
}
$_SESSION['locator']=array("IP" =>$_SERVER['REMOTE_ADDR'], "ISO"=>$country['country'], "currency"=>$currency);
}
add_action( 'init', 'geoIPLocator', 5);
The problem is that it breaks regular currency switcher. When this code is on, we can't switch currency to any other. Is it because we don't save or check if a user already has some currency set up to him (like the manual switcher does)?
This is corrected code
function geoIPLocator() {
try {
global $woocommerce;
if ( isset($woocommerce) && gettype($woocommerce) == 'object' && property_exists($woocommerce,'session') && gettype($woocommerce->session) == 'object' && property_exists($woocommerce->session,'get') ) {
$manual_switch=$woocommerce->session->get('client_currency', null);
if ( $manual_switch == null ) {
global $woocommerce_wpml;
$currency='EUR';
$geo=new WC_Geolocation();
$geo->init();
$country=$geo::geolocate_ip($geo::get_ip_address());
if(isset($_SESSION['locator'])) {
if($_SESSION['locator']['IP']==$_SERVER['REMOTE_ADDR'] && strlen($_SESSION['locator']['IP'])>0) {
$woocommerce_wpml->multi_currency_support->set_client_currency($_SESSION['locator']['currency']);
return;
}
}
if($country['country']=="RU" || $country['country']=="BY") {
$woocommerce_wpml->multi_currency_support->set_client_currency('RUB');
$currency='RUB';
}
else {
$woocommerce_wpml->multi_currency_support->set_client_currency('EUR');
$currency='EUR';
}
$_SESSION['locator']=array("IP"=>$_SERVER['REMOTE_ADDR'], "ISO"=>$country['country'], "currency"=>$currency);
}
}
} catch (Exception $e) {
$e->getMessage();
}
}
add_action( 'init', 'geoIPLocator', 5);
Related
I need to collect data through a contact form and track customer referrals. I am creating a tag for the form, but I need it to be filled only if there was a transition from a referral that contained partner
my code
// Original Referrer
function wpshore_set_session_values()
{
if (!session_id())
{
session_start();
}
if (!isset($_SESSION['OriginalRef']))
{
$_SESSION['OriginalRef'] = $_SERVER['HTTP_REFERER'];
}
if (!isset($_SESSION['LandingPage']))
{
$_SESSION['LandingPage'] = $_SERVER["REQUEST_URI"];
}
}
add_action('init', 'wpshore_set_session_values');
///
function getRefererPage3( $form_tag ){
$partner = array (
'/partner/',
);
$parts = parse_url($_SERVER['HTTP_REFERER'];
if (!empty($parts['path']) || in_array($parts['path'], $partner)){ //this row. I doubt how it should be
if ( $form_tag['name'] == 'referer-page3' ) {
$form_tag['values'][] = $_SESSION['LandingPage'];
}
}
return $form_tag;
}
if ( !is_admin() ) {
add_filter( 'wpcf7_form_tag', 'getRefererPage3' );
}
I found my problem. Maybe this solution will help anyone in the future.
function getRefererPage3( $form_tag ){
if (strpos($_SESSION['LandingPage'], 'partner') !== false) {
if ( $form_tag['name'] == 'referer-page3 ) {
$form_tag['values'][] = $_SESSION['LandingPage'];
}
}
return $form_tag;
}
if ( !is_admin() ) {
add_filter( 'wpcf7_form_tag', 'getRefererPage3' );
}
Hi can anyone help why URL string parameters post again and again?
HTTP://127.0.0.1/ab/1936.html?cart=yes?cart=yes
i m using this parameter to open mini cart when we added product into cart in magneto 1.9
Please help me how to protect this?
i am using this code-
<?php
if ($_GET['cart']=='yes') {
echo "<script type='text/javascript'>
jQuery('.minicart_open').show();
</script>";
}
?>
cartController.php
protected function _goBack()
{
$returnUrl = $this->getRequest()->getParam('return_url');
// print_r($returnUrl);exit;
if ($returnUrl) {
if (!$this->_isUrlInternal($returnUrl)) {
throw new Mage_Exception('External urls redirect to "' . $returnUrl . '" denied!');
}
$this->_getSession()->getMessages(true);
$this->getResponse()->setRedirect($returnUrl);
} elseif (!Mage::getStoreConfig('checkout/cart/redirect_to_cart')
&& !$this->getRequest()->getParam('in_cart')
&& $backUrl = $this->_getRefererUrl()
) {
$this->getResponse()->setRedirect($backUrl.'?cart=yes');
} else {
if (
(strtolower($this->getRequest()->getActionName()) == 'add')
&& !$this->getRequest()->getParam('in_cart')
) {
$this->_getSession()->setContinueShoppingUrl($this->_getRefererUrl());
}
$this->_redirect('checkout/cart');
}
return $this;
}
i got the answer
Just replace below line
$this->getResponse()->setRedirect($backUrl.'?cart=yes');
To
$url = $backUrl;
// Search substring
if (strpos($url, $key) == false) {
$this->getResponse()->setRedirect($backUrl.'?cart=yes');
}
else {
$this->getResponse()->setRedirect($backUrl);
}
//exit;
its working for me...Hope its helpful for you...
I want to get URL before the current one in Magento and check if is shopping cart and checkout page. For example now I am in the My account page, I want to check if the visited page before My account was Shopping Cart page.
I try to add this function but is not work because my last URL is login, not
shopping cart
public function customerRegistration(Varien_Event_Observer $observer)
{
$lastUrl = Mage::getSingleton('core/session')->getLastUrl();
if(preg_match("#onestepcheckout/index#", $lastUrl)){
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('onestepcheckout/index'));
Mage::app()->getResponse()->sendResponse();
exit;
}
else{
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account'));
Mage::app()->getResponse()->sendResponse();
exit;
}
}
Update:
i find a solution, to redirect after register to checkout page if there exist a product in cart, but is a problem, after register is complete in the checkout the Billing fields is not complete and the user is not login. Anyone?
New Update:
This is what I made to redirect after login and it is work perfect, I need to do the same for Register. The problem with Register is because here the lastUrl is login. Anyone? with any idea?
public function customerLogin(Varien_Event_Observer $observer)
{
if (Mage::helper('customerredirect')->isEnabled() && !Mage::getSingleton("core/session")->getRedirectregister()){
$lasturl = Mage::getSingleton('core/session')->getLastUrl();
if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout') === false){
if (! preg_match("#customer/account/create#", $lasturl) && Mage::helper('customerredirect')->isoptionEnabled('login_customerredirect')) {
if(Mage::getSingleton('core/session')->getIsFromCart() == 1 || Mage::getSingleton('core/session')->getIsFromCheckout() == 1){
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('onestepcheckout/index'));
Mage::app()->getResponse()->sendResponse();
exit;
}
else
{
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account'));
Mage::app()->getResponse()->sendResponse();
exit;
}
}
}
}
Mage::getSingleton("core/session")->setRedirectregister(false);
Mage::getSingleton('core/session')->setIsFromCart('0');
Mage::getSingleton('core/session')->setIsFromCheckout('0');
}
Update:
A good developer told me that is a way to do this. To add an event before going to register page then add an event observer after registration is complete and then check what is necessary. But I don't know to do this, maybe someone can help me with this? Thank you
Thank you
My Original Code
public function customerLogin(Varien_Event_Observer $observer)
{
if (Mage::helper('customerredirect')->isEnabled() && !Mage::getSingleton("core/session")->getRedirectregister()){
$lasturl = Mage::getSingleton('core/session')->getLastUrl();
if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout') === false){
if (! preg_match("#customer/account/create#", $lasturl) && Mage::helper('customerredirect')->isoptionEnabled('login_customerredirect')) {
if(Mage::getSingleton('core/session')->getIsFromCart() == 1 || Mage::getSingleton('core/session')->getIsFromCheckout() == 1){
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('onestepcheckout/index'));
Mage::app()->getResponse()->sendResponse();
exit;
}
else
{
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account'));
Mage::app()->getResponse()->sendResponse();
exit;
}
}
}
}
Mage::getSingleton("core/session")->setRedirectregister(false);
Mage::getSingleton('core/session')->setIsFromCart('0');
Mage::getSingleton('core/session')->setIsFromCheckout('0');
}
/*method for SignUp Customerredirect*/
public function customerRegistration(Varien_Event_Observer $observer)
{
Mage::getSingleton("core/session")->setRedirectregister(true);
if (Mage::helper('customerredirect')->isEnabled() && Mage::helper('customerredirect')->isoptionEnabled('registration_customerredirect') ) {
$_session = $this->_getSession();
$_session->setBeforeAuthUrl(Mage::helper('customerredirect')->setRedirectOnSignup());
}
}
This is my config.xml of this extension
<events>
<customer_login>
<observers>
<customerredirect>
<class>customerredirect/observer_customer</class>
<method>customerLogin</method>
</customerredirect>
</observers>
</customer_login>
<customer_register_success>
<observers>
<customerredirect>
<class>customerredirect/observer_customer</class>
<method>customerRegistration</method>
</customerredirect>
</observers>
</customer_register_success>
</events>
So all I want now is the Register page to work like Login, In present the login redirection is perfect, but for Register always I am redirect to My Account page. Not to the checkout page.
New Option:
if (strpos(Mage::getSingleton('core/session')->getLastUrl(), 'checkout/cart') !== false) {
Mage::getSingleton('core/session')->setIsFromCart('1');
} else {
Mage::getSingleton('core/session')->setIsFromCart('0');
}
if (strpos(Mage::getSingleton('core/session')->getLastUrl(), 'onestepcheckout/index') !== false) {
Mage::getSingleton('core/session')->setIsFromCheckout('1');
} else {
Mage::getSingleton('core/session')->setIsFromCheckout('0');
}
There is an easier solution.
Create an override of:
/app/code/core/Mage/Customer/controllers/AccountController.php
Copy _loginPostRedirect() method
In the newly created file, in _loginPostRedirect method edit:
FROM:
$this->_redirectUrl($session->getBeforeAuthUrl(true));
TO:
$sUrl = $session->getBeforeAuthUrl( TRUE );
// Do url exclusions and conditional checks here
// Force user to go home on login.
$sUrl = Mage::getBaseUrl();
$this->_redirectUrl( $sUrl );
Cleaned up with your conditionals (they are a bit convoluted):
$sUrl = $session->getBeforeAuthUrl( TRUE );
if (Mage::helper('customerredirect')->isEnabled() && !Mage::getSingleton("core/session")->getRedirectregister())
{
$lasturl = Mage::getSingleton('core/session')->getLastUrl();
if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout') === false)
{
if (! preg_match("#customer/account/create#", $lasturl) && Mage::helper('customerredirect')->isoptionEnabled('login_customerredirect'))
{
if( Mage::getSingleton('core/session')->getIsFromCart() == 1 || Mage::getSingleton('core/session')->getIsFromCheckout() == 1 )
{
$sUrl = Mage::getUrl('onestepcheckout/index');
}
else
{
$sUrl = Mage::getUrl('customer/account');
}
}
}
}
$this->_redirectUrl( $sUrl );
Edited for register:
$sUrl = $session->getBeforeAuthUrl( TRUE );
if (Mage::helper('customerredirect')->isEnabled() && !Mage::getSingleton("core/session")->getRedirectregister())
{
if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout') === false)
{
if ( Mage::helper('customerredirect')->isoptionEnabled('login_customerredirect'))
{
$lasturl = Mage::getSingleton('core/session')->getLastUrl();
if( Mage::getSingleton('core/session')->getIsFromCart() == 1 || Mage::getSingleton('core/session')->getIsFromCheckout() == 1
|| preg_match("#customer/account/create#", $lasturl) )
{
$sUrl = Mage::getUrl('onestepcheckout/index');
}
}
}
}
I have multi-language web store and use WPML plugin with Relevanssi better search plugin together.
Relevanssi plugin has an option
Limit results to current language: If this option is checked,
Relevanssi will only return results in the current active language.
Otherwise results will include posts in every language.
The problem is that, after I check this option, Relevanssi better search plugin returns results not in the current active language, but in default language only (English in my case). And if I don't check this option, Relevanssi better search plugin returns results in All 3 language!
This is the Relevanssi wpml filter code. Maybe someone knows what to do with this code to get the results in the current active language?!
Relevanssi author has no time to do this :(
function relevanssi_wpml_filter($data) {
$use_filter = get_option('relevanssi_wpml_only_current');
if ('on' == $use_filter) {
//save current blog language
$lang = get_bloginfo('language');
$filtered_hits = array();
foreach ($data[0] as $hit) {
if (isset($hit->blog_id)) {
switch_to_blog($hit->blog_id);
}
global $sitepress;
if (function_exists('icl_object_id') && !function_exists('pll_is_translated_post_type')) {
if ($sitepress->is_translated_post_type($hit->post_type)) {
if ($hit->ID == icl_object_id($hit->ID, $hit->post_type, false, $sitepress->get_current_language())) $filtered_hits[] = $hit;
}
else {
$filtered_hits[] = $hit;
}
}
elseif (function_exists('icl_object_id') && function_exists('pll_is_translated_post_type')) {
if (pll_is_translated_post_type($hit->post_type)) {
global $polylang;
if ($polylang->model->get_post_language($hit->ID)->slug == ICL_LANGUAGE_CODE) {
$filtered_hits[] = $hit;
}
else if ($hit->ID == icl_object_id($hit->ID, $hit->post_type, false, ICL_LANGUAGE_CODE)) {
$filtered_hits[] = $hit;
}
}
else {
$filtered_hits[] = $hit;
}
}
Change icl_object_id to wpml_object_id_filter.
It should become like this:
if (function_exists('wpml_object_id_filter') && !function_exists('pll_is_translated_post_type')) {
if ($sitepress->is_translated_post_type($hit->post_type)) {
if ($hit->ID == wpml_object_id_filter($hit->ID, $hit->post_type, false, $sitepress->get_current_language())) $filtered_hits[] = $hit;
}
else {
$filtered_hits[] = $hit;
}
}
elseif (function_exists('wpml_object_id_filter') && function_exists('pll_is_translated_post_type')) {
if (pll_is_translated_post_type($hit->post_type)) {
if (PLL()->model->get_post_language($hit->ID)->slug == ICL_LANGUAGE_CODE) {
$filtered_hits[] = $hit;
}
else if ($hit->ID == wpml_object_id_filter($hit->ID, $hit->post_type, false, ICL_LANGUAGE_CODE)) {
$filtered_hits[] = $hit;
}
}
else {
$filtered_hits[] = $hit;
}
}
I'm creating a buy-now button cs-cart add-on. I've created an add-on and I've made the add-to-cart functionality work. But cant redirect it to checkout page. I have used "fn_redirect("checkout")" function for the redirection. And used this function below this:
"fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);"
What should I do for redirection?
Edit:
My controller (buy_now.php)
if ($mode == 'add') {
if (empty($auth['user_id']) && Registry::get('settings.General.allow_anonymous_shopping') != 'allow_shopping')
{
return array(CONTROLLER_STATUS_REDIRECT, "auth.login_form?return_url=" . urlencode($_REQUEST['return_url']));
}
// Add to cart button was pressed for single product on advanced list
if (!empty($dispatch_extra)) {
if (empty($_REQUEST['product_data'][$dispatch_extra]['amount'])) {
$_REQUEST['product_data'][$dispatch_extra]['amount'] = 1;
}
foreach ($_REQUEST['product_data'] as $key => $data) {
if ($key != $dispatch_extra && $key != 'custom_files') {
unset($_REQUEST['product_data'][$key]);
}
}
}
$prev_cart_products = empty($cart['products']) ? array() : $cart['products'];
fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);
fn_save_cart_content($cart, $auth['user_id']);
$previous_state = md5(serialize($cart['products']));
$cart['change_cart_products'] = true;
fn_calculate_cart_content($cart, $auth, 'S', true, 'F', true);
if (md5(serialize($cart['products'])) != $previous_state && empty($cart['skip_notification'])) {
$product_cnt = 0;
$added_products = array();
if (!empty($added_products)) {
Registry::get('view')->assign('added_products', $added_products);
if (Registry::get('config.tweaks.disable_dhtml') && Registry::get('config.tweaks.redirect_to_cart')) {
Registry::get('view')->assign('continue_url', (!empty($_REQUEST['redirect_url']) && empty($_REQUEST['appearance']['details_page'])) ? $_REQUEST['redirect_url'] : $_SESSION['continue_url']);
}
fn_redirect(Registry::get('config.https_location') . "/checkout");
// $msg = Registry::get('view')->fetch('views/checkout/components/product_notification.tpl');
//fn_set_notification('I', __($product_cnt > 1 ? 'products_added_to_cart' : 'product_added_to_cart'), $msg, 'I');
$cart['recalculate'] = true;
} else {
fn_set_notification('N', __('notice'), __('product_in_cart'));
}
}
unset($cart['skip_notification']);
$_suffix = '.checkout';
}
`
hook template : add_to_cart.post.tpl
{$id = "buy_now_{$product.product_id}"}
<button id="opener_{$id}" name="dispatch[buy_now.add..{$product.product_id}]" class=" vs-button buynow_btn_">Buy Now</button>
I'd consider putting your fn_redirect("checkout") code into an if/else statement to see what's happening with it.
If you have this code:
fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);
Then surely you can put the redirection to the checkout page underneath it?
$redirect_url = "http://testcheckout.com?test=test"; //change this!
$errorMessage = "I should have redirected already....";
if (!empty($redirect_url)) {
//if it's not empty then it should redirect.
fn_redirect($redirect_url);
//Then put a die statement in here just to check it's not executing
//anything else and you can see if the redirect has a problem:
die(print_r($errorMessage, true ));
}
else {
//otherwise there is a problem with the supplied redirect url (empty)
die(print_r($redirect_url, true ));
}
I'd also take a look at this page which might help more:
http://forum.cs-cart.com/topic/6873-direct-buy-button/
Hope that helps!