SilverCart Forum

We moderate this Forum and we're here to help. Have you already run a forum search to check if your problem has already been solved?

You can help us helping you by providing detailed error messages, screenshots and logfile entries.

SebastianRamon

Page: 1
Topic Paypal MiniCart 3923 Views

Paypal MiniCart

5 March 2012 at 1:38pm

Hi all,
I'm not a very experienced developer but I've started playing around with silvercart recently and really like it.

I've been toying with integrating Paypals MiniCart into the module. I've just removed the regular Cart from the site but am wondering how I can change the function of the "Quantity" box and "Add to cart" button to work with MiniCart instead?

I've been playing with it for a few days now and can't figure it out.

Does anyone have any thoughts on how I could do this? I kinda thought it would be easy enough but it's not the first time I've made that mistake :)

Any help is appreciated,

Thanks all :)

Re: Paypal MiniCart

5 March 2012 at 4:45pm Last edited: 5 March 2012 4:46pm

Hello AM,

great to hear that you like SilverCart - we're looking forward to see the another new module :)

The quantity box and the "add to cart" button both reside in "silvercart/code/custom_forms/SilvercartProductAddCartForm.php".
This form calls the method "addProduct" in "SilvercartShoppingCart", so that method would be the place to put your action in.

Fortunately there's already a PluginProvider available, so that you can overwrite this method without changing the original SilverCart code.

Just write a new plugin in two steps:

1) Create a new file "MyShoppingCartPlugin.php" somewhere in your projects code directory:

<?php
class MyShoppingCartPlugin extends DataObjectDecorator {

/**
* Overwrites the addProduct method.
*
* @param array &$arguments The arguments to pass
* $arguments[0]: formData
* @param mixed &$callingObject The calling object
*
* @return string
*
* @author ...
* @since ...
*/
public function pluginOverwriteAddProduct(&$arguments, &$callingObject) {

// In $arguments[0] all form parameters are included (e.g. the quantity)
// Do whatever you like here, just make sure to "return true".

return true;
}
}

2) Register the plugin in your project's "_config.php":

Object::add_extension('SilvercartShoppingCartPluginProvider', 'MyShoppingCartPlugin');

That's it, just call your website with a "dev/build" and everything's set. When something gets added to the shopping cart your new plugin will get called.

In the plugin's method "pluginOverwriteAddProduct" you can do whatever you want. Just make sure to return boolean true, so that the original method gets overwritten.

All the best,
Sascha

Re: Paypal MiniCart

5 March 2012 at 5:16pm

Hi Sascha,

Thank so much for your reply, I must've been on the right track as I was looking into the SilvercartProductAddCartForm.php page but I was just unsure of what to do there.

The New Plugin page seems like a better way to go but again my skills aren't there yet.

The code below is what the Paypal button for the MiniCart would normally look like but I'm not sure how I can take the necessary elements into the new plugin page to make it that when the Silvercart "Add to cart" button is clicked it updates the MiniCart instead of the SilverCart?

<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post" name="frmPaypal" class="paypalform">
   <input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="add" value="1">
   <input type="hidden" name="business" value="den@site.com">
   <input type="hidden" name="item_name" value="$Title">
   <input type="hidden" name="item_number" value="$Prodid">
   <input type="hidden" name="amount" value="$Price">
   <input type="hidden" name="currency_code" value="EUR">

   <input type="hidden" name="return" value="http://site.com/thank-you" />
   <input type="hidden" name="cancel_return" value="http://site.com/" />

   <input type="submit" name="submit" value="ADD TO CART" class="paypalbutton">

</form>

Re: Paypal MiniCart

7 March 2012 at 7:55pm

Hi Am,

it seems as if the addToCart form should jump to paypal directly on submit.

Currently CustomHtmlForms can not send data directly to other domains.

So you'd have to try to do a CURL call (or use fsockopen etc. if CURL is not available on your server) to send the data to paypal.
After that you'd probably have to reload the current page, so that the shoppingcart template can load the new values.
Sending the CURL call could happen in the method "pluginOverwriteAddProduct" from our class above.

For more information about sending CURL calls and the alternative have a look at:

- "http://www.html-form-guide.com/php-form/php-form-submit.html"
- "http://php.net/manual/de/book.curl.php"

All the best,
Sascha