Magento: Change Swiss B2B Customer to specific Magento Customer Group - php

I need a solution to change the customer group of customers with a default shipping address in Swiss or Norway. We have multiple store views. One of this is the b2b store view. I need a solution only for this store view. Is there a way to define this via Magento Tax Configuration?
I tried to solve this over the event trigger <customer_save_before> but at this point I can't get data from new saved shipping addresses.
Update - what i've tried so far:
public function customerSaveBefore(Varien_Event_Observer $observer)
{
/* #var $customer Mage_Customer_Model_Customer */
$customer = $observer->getCustomer();
if($customer->getPrimaryShippingAddress()) {
$adress = $customer->getDefaultShippingAddress()->getCountryId();
if (Mage::helper('core')->isCountryInEU($adress) === TRUE && $customer->getGroupId() == "5") {
$customer->setGroupId('3');
}
elseif (Mage::helper('core')->isCountryInEU($adress) === FALSE && $customer->getGroupId() == "3") {
$customer->setGroupId('5');
}
}
}

Related

WooCommerce calculate shipping via REST API

My implementation in app is just like my WooCommerce website. I want to achieve following points for calculating shipping:
Check if address is required or not for calculating shipping address?
If address is entered by user, how to check if this address falling between shipping method crieteria? Give error to user if entered address is not valid as per shipping method.
Once user enter valid address, calculate cost of all shipping methods.
Show all shipping methods to user in app with its cost with default method selected.
When user switch between shipping methods, it should save and sync with website too and get cart total as per selected method.
Till now what I achieve is point 4. I am able to get all shipping methods calculated via website but if not calculated via website then it returns me null there.
Here is my API code:
function register_get_cart_shipping() {
register_rest_route(
'custom-plugin', '/getCartShipping/',
array(
'methods' => ['GET'],
'callback' => 'getCartShipping',
)
);
function getCartShipping() {
$chosen_methods = WC()->session->get('chosen_shipping_methods');
$count = 0;
foreach( WC()->session->get('shipping_for_package_0')['rates'] as $method_id => $rate ){
$data[$count]->rate_id = $rate->id;
$data[$count]->method_id = $rate->method_id;
$data[$count]->instance_id = $rate->instance_id;
$data[$count]->label = $rate->label;
$data[$count]->cost = $rate->cost;
$data[$count]->taxes = $rate->taxes;
$data[$count]->chosen = $chosen_methods['rate_id'];
if($chosen_methods['rate_id'] == $rate->id ){
$data[$count]->isSelected = true;
} else {
$data[$count]->isSelected = false;
}
$count++;
}
return $data;
}
}
Also for point 5, when user switch between shipping methods, I am using this code but it doesn't update selected shipping method on website. Here is the code:
function updateCartShipping($request) {
$rate_id["rate_id"] = "table_rate:10:4";
// $rate_id["rate_id"] = "table_rate:9:3";
// $rate_id["rate_id"] = $request['shippingID'];
WC()->session->set('chosen_shipping_methods', $rate_id);
return "OK";
}
I also don't know which method id to set as a shipping method. It seems mysterious to me.
Any help will be appreciated. Thanks!

Magento 1.9 remove tax on checkout when user enter VAT

I'm trying to make magento modification when user enter vat number on checkout remove tax from order.
I found a code on stackoverflow which support on magento older version but it not work with new version 1.9,
I made few modifications for work the condition and return 0,even it return 0 checkout still shows tax.
here is my code which is on file
/app/code/core/Mage/Tax/Model/Calculation.php line number 268
public function getRate($request)
{
if (!$request->getCountryId() || !$request->getCustomerClassId() || !$request->getProductClassId()) {
return 0;
}
//my code
$ctax= Mage::getSingleton('checkout/session')->getQuote()->getCustomerTaxvat();
if ($this->getCustomer() && $ctax !='') {
//echo 'test';
return 0;
}
//end my code
$cacheKey = $this->_getRequestCacheKey($request);
if (!isset($this->_rateCache[$cacheKey])) {
$this->unsRateValue();
$this->unsCalculationProcess();
$this->unsEventModuleId();
Mage::dispatchEvent('tax_rate_data_fetch', array(
'request' => $request));
if (!$this->hasRateValue()) {
$rateInfo = $this->_getResource()->getRateInfo($request);
$this->setCalculationProcess($rateInfo['process']);
$this->setRateValue($rateInfo['value']);
} else {
$this->setCalculationProcess($this->_formCalculationProcess());
}
$this->_rateCache[$cacheKey] = $this->getRateValue();
$this->_rateCalculationProcess[$cacheKey] = $this->getCalculationProcess();
}
return $this->_rateCache[$cacheKey];
}
Anyone can help me to make tax 0 when user enters vat number on checkout, Thanks a lot
I've managed to solve my problem by following this thread : Modify tax rate on cart quote items and recalculate
I have added an Observer to the event sales_quote_collect_totals_before.
And here is the content of my observer, very simple :
public function removetax($observer)
{
$customer_id = Mage::getSingleton('customer/session')->getId();
$customer = Mage::getModel("customer/customer")->load($customer_id);
if($customer->getIsTaxExempt() == 1)
{
$items = $observer->getEvent()->getQuote()->getAllVisibleItems();
foreach($items as $item)
$item->getProduct()->setTaxClassId(0);
}
}
If customer is tax exempt, I grab the current cart content and for each item, I set the product tax class to 0. It is mandatory to not save the product or the item. The aim here is to set a value for the following calculation, not to save it in the database. Tax classes need to stay to the initial value in the db.
You can use sales_quote_collect_totals_before event.
then you have to define logic for remove tax on checkout page.
This link you can refer.
Go to Calculation.php and find the calcTaxAmount() and add billing condditions
public function calcTaxAmount($price, $taxRate, $priceIncludeTax = false, $round = true)
{
$billing = Mage::getModel('checkout/session')->getQuote()->getCustomerTaxvat();
if($billing !="")
{
return 0;
}
}

how to add shipping charges by date in magento?

I want to make an option if the customer want same day delivery the delivery charges will be $10 else it will be standard charges. just want to add check-box for same day delivery.i have that much information only, if someone can help I would be really happy since its been tough for me.
Assuming you have passed deliveryDate in request shipping object.
If we consider code of flat rate.Then in below function
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
If($request->Deliverydate == Today )// replace code for getting today
{
ShippingPrice=10;
}
elseif ($this->getConfigData('type') == 'O') { // per order
$shippingPrice = $this->getConfigData('price');
} elseif ($this->getConfigData('type') == 'I') { // per item
$shippingPrice = ($request->getPackageQty() * $this->getConfigData('price')) - ($this->getFreeBoxes() * $this->getConfigData('price'));
} else {
$shippingPrice = false;
}

Magento disable tax when customer enter vat number on checkout

I have magento 1.7 site and i'm tiring to remove that tax from customers if they enter an tax number on checkout page.
i have two countries Netherlands and Belgium for tax rules both have 10% taxes.the default country is Belgium.
i need to remove the tax adding when Belgium customer enter there vat number on checkout page.
i tired using tax rules but no luck.
anyone have idea about how to do that using magento backend or using code level.any answers i appreciated.
thank you
Over ride Magento's Tax Model getRate() function is how we did something similar.
class My_Module_Model_Tax_Calculation extends Mage_Tax_Model_Calculation
{
public function getRate($request)
{
if (!$request->getCountryId() || !$request->getCustomerClassId() || !$request->getProductClassId()) {
return 0;
}
$country_id = $request->getCountryId();
if ($country_id == 'BE' && $this->getCustomer() && $this->getCustomer()->getTaxvat()) {
return 0;
}
$cacheKey = $this->_getRequestCacheKey($request);
if (!isset($this->_rateCache[$cacheKey])) {
$this->unsRateValue();
$this->unsCalculationProcess();
$this->unsEventModuleId();
Mage::dispatchEvent('tax_rate_data_fetch', array('request'=>$request));
if (!$this->hasRateValue()) {
$rateInfo = $this->_getResource()->getRateInfo($request);
$this->setCalculationProcess($rateInfo['process']);
$this->setRateValue($rateInfo['value']);
} else {
$this->setCalculationProcess($this->_formCalculationProcess());
}
$this->_rateCache[$cacheKey] = $this->getRateValue();
$this->_rateCalculationProcess[$cacheKey] = $this->getCalculationProcess();
}
return $this->_rateCache[$cacheKey];
}
}

PHP e-commerce system circular dependency issue

I'm just getting started with dependency injection and I have immediately hit a problem: I have two classes that depend on each other.
The classes are Basket and Shipping.
In my Basket class I have the following relevant methods:
public function totalShipping()
{
return $this->_shipping->rate();
}
public function grandTotal()
{
return $this->totalProductsPrice() + $this->totalShipping();
}
public function totalWeight()
{
$weight = 0;
$products = $this->listProducts();
foreach ($products as $product) {
$weight += $product['product_weight'];
}
return ($weight == '') ? 0 : $weight;
}
$this->_shipping is an instance of the Shipping class
In my Shipping class I have the following relevant methods:
public function rate()
{
if (isset($_SESSION['shipping']['method_id'])) {
$methodId = $_SESSION['shipping']['method_id'];
return $this->_rates[$methodId]['Shipping Price'];
}
// Method not set
return NULL;
}
// Available Methods depend on country and the total weight of products added to the customer's basket. E.g. USA and over 10kg
public function listAvailableMethods()
{
$rates = array();
if (isset($_SESSION['customer']['shipping_address']['country_code'])) {
foreach ($this->_rates as $method_id => $rate) {
if (($_SESSION['customer']['shipping_address']['country_code'] == $rate['Country']) && ($this->_basket->totalWeight() > $rate['Weight From']) && ($this->_basket->totalWeight() < $rate['Weight To'])) {
$rates[$method_id] = $rate;
}
}
}
return $rates;
}
$this->_basket is an instance of the Basket class.
I am totally clueless as to how to resolve this circular dependency. Thank you for your help in advance.
Update
In my Shipping Class I also have this method:
public function setMethod($method_id)
{
// A check to make sure that the method_id is one of the provided methods
if ( !array_key_exists($method_id, $this->listAvailableMethods()) ) return false;
$_SESSION['shipping'] = array(
'method_id' => $method_id
);
}
I have ended up renaming Shipping to Shipping_Methods and I have created a new class called Customer_Shipping_Methods. Essentially Customer_Shipping_Methods could be part of the Basket class but I'd rather keep it separate.
#RyanLaBarre was totally right. I was essentially mixing methods that should have been in the Basket Class with methods in my Shipping_Methods class. Shipping_Methods should have only contained general shipping data methods which were not specific to the current session.
I think what threw me, was that Shipping_Methods sources its data from a csv file rather than a database table. Once I started seeing Shipping_Methods as just another table, it all clicked in my mind.
#rdlowrey that is very good advice. I will be putting my session globals into my controllers immediately!

Categories