Shipping method based on a custom field value ?

  • Posts: 9
  • Thank you received: 1
10 years 9 months ago #145286

Hello

I have :
- a custom field for users, which is a client account number (entered by user)
- a manuel shipping method,

I would like to display this shipping method ONLY if the custom field has a value (when the user has registered or after in the checkout page).

Do you know how to do this conditionnal shipping method please ?

Best regards,
Lora

Last edit: 10 years 9 months ago by lora.

Please Log in or Create an account to join the conversation.

  • Posts: 9
  • Thank you received: 1
10 years 9 months ago #145324

Hi

I'm trying to write a plugin, based on manual shipping plugin, but I'm not confident on succeed :(

Here is my code :

class plgHikaShopshippingMyfedex extends hikashopShippingPlugin
{
    var $multiple = true;
    var $name = 'myfedex';
    var $doc_form = 'myfedex';

    function onShippingDisplay(&$order,&$dbrates,&$usable_rates,&$messages){
                if(!hikashop_loadUser())
                      return false;
                $local_usable_rates = array();
                $local_messages = array();
                $ret = parent::onShippingDisplay($order, $dbrates, $local_usable_rates, $local_messages);
                
                //$isCustomFieldFedex = trim($order->shipping_address->adress_fedex_account) == '');
                //return $isCustomFieldFedex && $ret;
                return false;
        }
}

the purpose is to display this shipping method ("My Fedex", if the customer has entered a fedex account number .
I thought "return false" disable the display of that shipping method but no, it's still displayed.

Please Log in or Create an account to join the conversation.

  • Posts: 82865
  • Thank you received: 13372
  • MODERATOR
10 years 9 months ago #145366

Hi,

In the onShippingDisplay method, you need to move all the valid shipping methods in the $dbrates array to the $usables_rates array.
So it's not about returning something, but about having a foreach on the $dbrates and checking each shipping method to add it to the usable_rates array or not.

Please Log in or Create an account to join the conversation.

  • Posts: 9
  • Thank you received: 1
10 years 9 months ago #145510

hi

It seems a bit confused to me.
do you have a small piece of code for that ? I looked at other shipping methodes plugins and there are too many lines that I don't think i need to keep.

Then, concerning the need to check whether or not a custom field from customer is filled, what variable should i change in order to enable/disable this shipping method ?
I though I could add a restriction, but it seems to be a bad way.

regards

Please Log in or Create an account to join the conversation.

  • Posts: 9
  • Thank you received: 1
10 years 9 months ago #145515

hum I'm giving myself 1 hour otherwise... i think I will do it with a bit of javascript / css (hide or not depending on the value.
It will be less confusing than spending too many times on the plugin...

Please Log in or Create an account to join the conversation.

  • Posts: 12953
  • Thank you received: 1778
10 years 9 months ago #145550

Hi,
Using this kind of onShippingDisplay function will probably do the job.

	function onShippingDisplay(&$order,&$dbrates,&$usable_rates,&$messages){
		if(!hikashop_loadUser())
			return false;
		$local_usable_rates = array();
		$local_messages = array();
		$ret = parent::onShippingDisplay($order, $dbrates, $local_usable_rates, $local_messages);
		if($ret === false)
			return false;

	foreach ($local_usable_rates as $i => $rate) {
				//load your custom field and check its value
				$usable_rates[$rate->shipping_id] = $rate;
			}
		}

Please Log in or Create an account to join the conversation.

  • Posts: 3
  • Thank you received: 0
10 years 9 months ago #145611

Hi!

Were u able to fix your problem? do u know how to arrange layout on the front end of my cart?

Thanks :)

Javier

Please Log in or Create an account to join the conversation.

  • Posts: 12953
  • Thank you received: 1778
10 years 9 months ago #145657

Hi,
You'll probably find your answer through that thread .

Please Log in or Create an account to join the conversation.

  • Posts: 9
  • Thank you received: 1
10 years 8 months ago #146256

Hello Nicolas, Mohamed Thelji, javierymirna

Thank you for your informations, I managed to do this with your help.

Here is what I've done :

" If a field is filled, then I can create an entry for that shipping method and its related rates/limits"

class plgHikaShopshippingMyfedex extends hikashopShippingPlugin
{
    var $multiple = true;
    var $name = 'myfedex';
    var $doc_form = 'myfedex';

        function onShippingDisplay(&$order,&$dbrates,&$usable_rates,&$messages){

                if(!hikashop_loadUser())
                        return false;
                $local_usable_rates = array();
                $local_messages = array();
                $ret = parent::onShippingDisplay($order, $dbrates, $local_usable_rates, $local_messages);
                if($ret === false)
                        return false;

                //dumpTrace();
                //dumpTemplate($this);
                //dump($order, 'ORDER');
                //dump($dbrates, 'DBRATES');
                //dump($usable_rates, 'USABLE RATES BEFORE');
                //dump($local_usable_rates, 'LOCAL USABLE RATES');
                //dump($order->billing_address->adress_fedexaccount, 'address fedex account');
                if (trim($order->billing_address->adress_fedexaccount) != '') {
                        foreach ($local_usable_rates as $i => $rate) {
                                //load your custom field and check its value
                                $usable_rates[$rate->shipping_id] = $rate;
                        }
                }
                //dump($usable_rates, 'USABLE RATES AFTER');
        }
}

- Install JDump is very usefull to understand how variables simple or complex are filled.
- I understood that $usable_rates is an array that contains an array of shipping limits/rates, and you can deal with to add the limits/rates for that shipping_id.
- $dbrates are the shipping methods available.

Anway, this works at 95%. Here is my test case :
- I have a customer with 2 billing addresses. One has the feded field filled, the other not. When I change billing address, Hikashop does not enter onShippingDisplay(). I believe another function is used.
BUT it's a very rare test case and can be managed offline. To hikashop : is this normal ? should it be a bug or not ?

Thank you

Last edit: 10 years 8 months ago by lora.

Please Log in or Create an account to join the conversation.

  • Posts: 82865
  • Thank you received: 13372
  • MODERATOR
10 years 8 months ago #146441

The onShippingDisplay method result is cached by HikaShop so if the environment doesn't change you don't see it called.
When you edit an address via the popup or AJAX process, the call to that method is done direclty in the frame of the popup before closing or with the AJAX request, and thus, in such cases, you won't see your traces on the checkout page.
Would that explain the issue that you're seeing ?

Please Log in or Create an account to join the conversation.

  • Posts: 9
  • Thank you received: 1
10 years 8 months ago #146442

Hi Nicolas

That's right, the environnement is not changed (only address edition is made, with or without filling my custom field).
I believe it is called if the restrictions are affected, but in my case, as it is not a "restriction" (in the way Hikashop has implemented in the shipping methods), the onShippingDisplay method is not called.

Anyway, for my needs, it could generate a few manual action on the management side, but not enough to code more precise code. So That's okay.

Please Log in or Create an account to join the conversation.

  • Posts: 3
  • Thank you received: 0
10 years 8 months ago #146451

Hi Lora! I should hire you to teach me how to use hikashop, my store is a miss www.guerrilladrone.com , every time is ask tech support a question they come back at me with 5 questions!! :(

Javier

Please Log in or Create an account to join the conversation.

Time to create page: 0.101 seconds
Powered by Kunena Forum