Discount based on other products in cart

  • Posts: 57
  • Thank you received: 2
11 years 4 months ago #110351

I want to alter the price for products in the cart (ProductPriceForQuantity) based on other products in the cart (coupons or discounts do not work for me here) but:

onBeforeCalculateProductPriceForQuantity(&$product)
and
function onAfterCalculateProductPriceForQuantity(&$product)
do not include the cart information,

onAfterCartProductsLoad is too late.

the added „hikashop_product_price_for_quantity_in_cart“ function at the bottom of the „currency.php“ did not work, because inside the class it won’t be triggered, and outside I have no access to the class itself (or better I don’t know how). And the „custom_quantity_tax“ plugin seems to override as it also contains the function. By the way, is it really the correct way to add the function to the file “currency.php”. What about Hika-Updates? Or did I misunderstood the documentation?

Do you have any hint for me?

Cheers,
Ralf

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

  • Posts: 82758
  • Thank you received: 13346
  • MODERATOR
11 years 4 months ago #110357

Hi,

To get the cart content, you can follow our developer documentation code samples:
www.hikashop.com/en/support/documentatio...umentation.html#code

Aslo, as explained in the template override section of the developer documentation, you can add the hikashop_product_price_for_quantity_in_cart function definition in a joomla system plugin:
www.hikashop.com/en/support/documentatio...tation.html#override
If you add it in the currency.php file you will indeed loose it when updating so I don't recommend it.

The following user(s) said Thank You: Hans987

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

  • Posts: 57
  • Thank you received: 2
11 years 4 months ago #110361

Thanks,

maybe, this would be helpful for others:


function onAfterCalculateProductPriceForQuantity(&$product) {
$pc=$product->product_code;
$pctid=substr($pc,7,1);
if ($pctid=="B") {
$pcid=substr($pc, 0, 7);//family
$pcfid=substr($pc,-4);//style pack

$class = hikashop_get('class.cart');
if(!empty($product->cart_id)) {
$element = $class->get($product->cart_id,true,$product->cart_type);
foreach($element as &$value)
if ($value->product_code != $pc && substr($value->product_code,7,1)=="A" && substr($value->product_code,-4)==$pcfid) {
$product->prices[0]->price_value=$product->prices[0]->price_value*50/100;
}
}
}
}


Cheers,
Ralf

The following user(s) said Thank You: nicolas

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

  • Posts: 57
  • Thank you received: 2
11 years 4 months ago #110532

A little extension:

I need to visualize the discounted price.
Is it possible to use any unused variable in the $product parameter of onAfterCalculateProductPriceForQuantity(&$product) or to create a new one?
Or do you have any other hint?

Many Cheers,
Ralf

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

  • Posts: 12953
  • Thank you received: 1778
11 years 4 months ago #110552

Hi,

Sure I think that you can do it if the variable is not used, but can you tell me exactly where do you want to visualize your discounted price, with a screenshot for example ?

Thanks :).

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

  • Posts: 57
  • Thank you received: 2
11 years 4 months ago #110580

pos 3 is discounted, because the "connected" pos 2 is also in cart. And the discount should be more visible for the buyer

Cheers,
Ralf

Attachments:

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

  • Posts: 82758
  • Thank you received: 13346
  • MODERATOR
11 years 4 months ago #110612

All the prices (discounted, not discounted, etc) are already calculated when the cart is loaded.
If you want to change the display of the prices on the cart you can edit the file "listing_price" of the view "checkout" via the menu Display->Views and add the code you want to display the prices like you want in the checkout.

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

  • Posts: 57
  • Thank you received: 2
11 years 4 months ago #110695

yes, but I wanted to calculate the price based on other products in the cart, this was only possible in "onAfterCalculateProductPriceForQuantity(&$product) ".
And now I want to visualize the calculation to the user, which is only possible on the "listing_price", I think.
So how can I get a connection?

Cheers,
Ralf

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

  • Posts: 82758
  • Thank you received: 13346
  • MODERATOR
11 years 4 months ago #110763

That's simple.
The $product variable that you have in the onAfterCalculateProductPriceForQuantity trigger is then attached to the cart's products array which is then used to display the cart products listing and in it the prices.
So in listing_price, in the variable $this->row you actually have the same thing as in $product in onAfterCalculateProductPriceForQuantity.

So if you do:
$product->my_var = 'test';
in onAfterCalculateProductPriceForQuantity, you will then be able to display the 'test' value in the listing_price view file by doing:
<?php echo $this->row->my_var; ?>

The following user(s) said Thank You: Hans987

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

Time to create page: 0.075 seconds
Powered by Kunena Forum