Bring Your Business to Life › Support › Image Store › CD Implementation
- AuthorPosts
- December 18, 2013 at 4:22 pm #55515lch503Participant
Hello,
I have started the re-write of the CD Discount implementation that I sent to you a couple of weeks ago so that I just have to add one file rather than modifying admin files etc.
I plan to have this additional file stored in a subfolder cd in the _inc folder, and the file called cd.php
The one thing I have not got my head around in php is classes, and was hoping you could assist. Is it the same as classes in object-orientated languages such as Java?
When adding sagePay.php and therefore the class this was easy because I just added it to the list of payment gateways.
I know I need to add code somewhere in image store to let it know that my cd.php file is there, and use its class. Where do you think this would be best?
Remember, I need to change the appearance of pricing.php so that there is an additional tab for cd discounts, and I need to change cart.php so that the customer cannot change quantities etc.
Therefore, I presume this must be high in the class hierarchy.
Thanks,
Leon
December 20, 2013 at 12:47 am #55526Xpark MediaKeymasterDon’t add any files in the image-store directory, create a new plugin that uses the image store hooks.
The classes are similar to java, but the syntax is different. You can create your own class in your new plugin and interact with the global $ImStore and $ImStoreCart objects while using the hooks.
You can also create many instances of the object by using
$my_variable = new ClasName();
but we recommend that you interact with the global variables already created.We have attached a few files to get you started with the new plugin.
December 21, 2013 at 11:41 am #55542lch503ParticipantThank you very much.
This has really helped me get started.
Leon
December 21, 2013 at 5:33 pm #55545lch503ParticipantHello,
I am getting quite far in my implementation of the CD plugin, however I am having some issues I think mainly due my understanding.
I have performed all the administration side that I require so rather than changing the core plugin, I have made an extra tab appear in pricing discounts on CDs, added a meta box for adding a discount and removing discounts.
I have even added a few things such as the extension will not activate unless the image-store plugin is activated, as well as removing all discounts from the wordpress database upon uninstallation of the core plugin.
I am having issues with the customer side.
I have to be able to:
- If the customer tries to add more than one of the same CD image, let them know with an error and reduce the quantity to 1. (This makes sense as only one of the same image will be on a CD)
- Add the discounts and manipulate the total price accordingly
The basic problem I am having is changing the values of the cart from my extension plugin.
There are plenty of do_action() hooks to use, and there are some in the right place. The problem I am having is changing the values of the cart, such as quantity of the CD images, the total value of the cart etc. from my extension plugin.
Can you help?
Thanks,
Leon
December 22, 2013 at 3:33 am #55554lch503ParticipantI am getting there now.
I can now change values in the cart from my extension.
Of all the hooks, I think before_save_cart is the best to use as in both before_add_to_cart and before_update_cart change the values that I change in my extension, therefore my changes are overwritten.
At the moment I am working with the quantity value. I change this with the before_save_cart hook, however, I must update the cart to see my changes. A simple refresh of the page does not work, and I cant call update_cart from my extension as I get an infinite loop (as it is called again from the same before_save_cart hook).
I am going to think about this, but any insight would be greatly appreciated.
Thanks,
Leon
December 22, 2013 at 3:33 pm #55564Xpark MediaKeymasterYou could use “ims_after_add_to_cart” or “ims_before_save_cart”,
if you use “ims_after_add_to_cart” all processing by image store is completed and saved and you will have to modify the data and save it back again to the database using “update_post_meta( $orderid, ‘_ims_order_data’, $this->cart );”
There other hooks base on specific cart actions
ims_before_save_cart_{$action}
ims_after_add_to_cart_{$action}December 26, 2013 at 5:36 am #55592lch503ParticipantHello,
I am getting quite far now. I am working on the front end.
If the customer chooses a CD option and requests more than one of the same image on a CD I have implemented an error system where the image store throws an error “invalid cd quantity” and resets the quantity to 1.
This is implemented in the before_save_cart hook.
This is working for the most part, however sometimes the error is seen as:
“Successfully added to cartInvalid CD quantity”
In the popover. This is shown with a green background rather than red as an error usually does. If I try again the error is thrown correctly.
After some debugging I have narrowed it down to the expiry of the cookie set on line 566 in the add_to_cart() function of _inc/store.php. The line is setcookie(‘ims_message’)… If I set it to time() + 2 I don’t see the incorrect error. This is bad on my part because I am modifying your code.
Is there a correct hook I can use to unset the cookie before the message is displayed, or if not could you please create one in your next release?
Could you supply the correct code for invalidating this cookie correctly?
Note that there is also a setcookie(‘ims_message’)… line of code on line 591 in the update_cart() function in _inc/store.php if you need to add a hook.
Thanks,
Leon
December 27, 2013 at 1:10 pm #55598Xpark MediaKeymasterHow are you setting the error? what you want to do is overwrite the error message, when the cart is updated and there is an error. The plugin will do all the work.
$ImStore->message = false; $ImStore->error ="Error message"
December 27, 2013 at 5:46 pm #55603lch503Participantmy function linking the before_save_cart hook is:
function cd_in_cart( ){ global $ImStoreCart; if ( $ImStoreCart->cart['cderror'] = 1 ){ $ImStoreCart->cart['cderror'] = 0; } $cart = $ImStoreCart->cart['images']; foreach ( $cart as $id => $sizes ){ foreach ( $sizes as $size => $colors ) { foreach ( $colors as $color => $values ) { if ($cart[$id][$size][$color]['size'] = "CD"){ if ($ImStoreCart->cart['images'][$id][$size][$color]['quantity'] > 1){ $ImStoreCart->cart['cderror'] = 1; $ImStoreCart->cart['images'][$id][$size][$color]['quantity'] = 1; $ImStoreCart->error = __( 'Error in CD Quantity', 'ims' ); } } } } } } }
I find that if I use global $ImStore in this function I don’t have access to the $ImStore variables, but I do with the $ImStoreCart variables, hence I use that.
I cannot therefore do as you suggest and falsify the $ImStore message variable. I have tried your suggestion and unfortunately this does not work.
Thanks,
Leon
December 28, 2013 at 3:53 am #55608lch503ParticipantI think this is because the before_save_cart hook passes the cart, and not the store itself. Therefore I do not have access to the global $ImStore variables, only the cart.
December 28, 2013 at 9:43 am #55610lch503ParticipantHello,
I have managed to implement the change in the cookie without changing your code (its all done in the extension)
Using before_post_actions I have implemented my own add_to_cart() method that is exactly the same as seen in _inc/store.php apart from changing the cookie expiry to time()+2.
Thanks!
Leon
December 28, 2013 at 2:42 pm #55612Xpark MediaKeymasterWe are glad that you were able to find a solution, for future references, in your function you call the global objec $ImStoreCart in the same way you can get access to $ImStore.
global $ImStoreCart, $ImStore;
December 29, 2013 at 4:45 pm #55973lch503ParticipantHello,
I have completed the CD plugin.
You can mark this as closed.
Thanks,
Leon
January 13, 2014 at 6:25 pm #56730CJsFotosParticipantI would like to add the CD option, is there a place I may download the add-in?
- AuthorPosts
- You must be logged in to reply to this topic.