Jump to content

Can't seem to hook Padloper::updateCart


alexm
 Share

Recommended Posts

@kongondo

I'm trying to hook Padloper::updateCart so I can grab a discount code from an input field named 'discount_code' in cart-edit.php so I can set it to the users session if the discount code matches however I can't seem to hook this. It looks like it's hookable, but have tried hook, hookBefore and hookAfter and simply stuck a bd('Test') and wire('log') in there to check but not getting anything. Am I going about this the wrong way. It would be nice to show the user their discount in the cart before proceeding to checkout you see.

Then I can hook PadloperCart::getProductPrice to apply the new product prices like in your example code.

  • Like 1
Link to comment
Share on other sites

Hi @alexm,

6 hours ago, alexm said:

I'm trying to hook Padloper::updateCart so I can grab a discount code from an input field named 'discount_code' in cart-edit.php so I can set it to the users session if the discount code matches however I can't seem to hook this. It looks like it's hookable, but have tried hook, hookBefore and hookAfter and simply stuck a bd('Test') and wire('log') in there to check but not getting anything.

I have tested and it works for me with hook, hookBefore and hookAfter, i.e. all the below work (in ready.php):

<?php

namespace ProcessWire;

// $this->addHookBefore('Padloper::updateCart', null, 'updateCartHook');
$this->addHookAfter('Padloper::updateCart', null, 'updateCartHook');
// $this->addHook('Padloper::updateCart', null, 'updateCartHook');

padloper_hook_updateCart.thumb.png.0be1e999d956f151131e4572ce8671af.png

However, it is not clear to me how hooking into Padloper::updateCart helps with what you are trying to achieve. 676 in the example above is the cart_row_id of some product and 6 is the quantity of the product in the cart. What information about the cart are you after in relation to your discount?

  • Like 1
Link to comment
Share on other sites

Yeah, apologies on my part for being a bit wishy washy there bro.

So... whilst I know there will be a future release with in-built discount codes, that I know is on your roadmap, in the meantime, I have a client who requires some very rudimentary discount code magic for one discount code.

With this in mind, I'm trying to implement a discount code input field on my shop-cart.php page/template which calls $padloper->cartRender->editCart() and renders my custom site/templates/padloper/cart-edit.php file. I've added a field called "discount_code" within the form area which I was hoping would get posted on "update cart" submission so that I can then hook 'Padloper::updateCart' to simply grab the posted value from the 'discount_code' field perhaps like so within the hook.

if (input()->post->discount_code) {
    $discountCode = sanitizer()->text(input()->post->discount_code);

    if ($discountCode == "T/O-FF") {
      wire('session')->set('orderDiscountCode', true);
    }
}

Then I know that in respect to this given session the customer/users discount code is valid I could then do something like:

$this->addHookAfter('PadloperCart::getProductPrice', null, 'discountCodePrice');

function discountCodePrice(HookEvent $event) {
    $product = $event->arguments('product');
    // get the product field with the price
    $stock = $product->padloper_product_stock;
    // grab the price
    $price = (float) $stock->price;
    # amend price if condition is met
    if (wire('session')->orderDiscountCode === true) {
        $price = 0.01 * $price;
    }
    $event->return = $price;
}

And it would apply the discount.

I could be way off-piste, but I hopefully that gives you an idea of what I'm trying to achieve 🤷🏻‍♂️

  • Like 1
Link to comment
Share on other sites

Hi @alexm,

Thanks for clarifying. I get you now. Although the those hooks would normally work, since the form in cart-edit is being submitted to a 'virtual' URL handled by URL Hooks and because a redirect occurs, execution halts, hence, the 'hooks' get released/discarded and you are left with nada. If anyone knows if my statement is untrue or partially incorrect, please let me know.

Now, back to your issue, easiest solution is to handle updating the cart yourself using $padloper API instead of sending the form (action) to the virtual padloper URL. This will allow you to process your custom form inputs yourself. Updating the cart is as simple as this:

<?php

namespace ProcessWire;
// *** UPDATE CART FOR AN EXISTING CART ITEM ***
// @note: $id: this is the id of this product as a cart item in the database
// it is not the page ID of the product!
// @note: these inputs should match the fields in your custom 'cart-edit' form
$id = (int) $input->post->padloper_cart_update_product_id;
$quantity = (int) $input->post->padloper_cart_update_product_quantity;
// update the cart ($key=>$value pair of cartItemID => cartItemQuantity)
$updatedCartProduct = [$id => $quantity];
// update the cart
$padloper->updateCart($updatedCartProduct, $rem_products = null, $isRedirect = false);
// handle your custom form input
$discountCode = $sanitizer->text($input->post->discount_code);
// ETC...

Your and PadloperCart::getProductPrice', null, 'discountCodePrice' Hook doesn't need changing.

Hope this helps. Please let me know if you need further help with this.

  • Thanks 1
Link to comment
Share on other sites

@kongondo Legend! Ta.

I did it like so

if ($input->post->padloper_cart_products || $input->post->padloper_cart_remove_product || $input->post->discount_code) {
  
  $products = $input->post->padloper_cart_products;
  $rem_products = $input->post->padloper_cart_remove_product;
  $padloper->updateCart($products, $rem_products, $isRedirect = false);
  $discountCode = $sanitizer->text($input->post->discount_code);
  if (!empty($discountCode)) {
    
    if ($discountCode == "T/O-FF") {
      $session->set('orderDiscountCode', $discountCode);
    } else {
      $session->set('orderDiscountCode', $discountCode);
    }
  }
  
  $session->redirect($page->url);
}

Works a charm coupled with your example price update hook. I'll post link to this chat in the other topic where someone was asking for a solution 😊

  • Like 1
Link to comment
Share on other sites

@alexm,Glad you got it sorted!

11 hours ago, alexm said:
    if ($discountCode == "T/O-FF") {
      $session->set('orderDiscountCode', $discountCode);
    } else {
      $session->set('orderDiscountCode', $discountCode);
    }

Aren't these conditions resulting in the same thing?

  • Like 1
Link to comment
Share on other sites

6 minutes ago, kongondo said:

Aren't these conditions resulting in the same thing?

@kongondo Yes, I was playing around with some other idea and I've left this in with my haste! There is no requirement for that conditional whatsoever 😂

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...