Detect if a product has options on cart module

  • Posts: 162
  • Thank you received: 0
5 years 9 months ago #295085

Hi,

how can I detect if a a product has options on the cart module?

I see you use this code on the add_to_cart_ajax.php:

$has_options = !empty($this->row->has_options);

How can I pull this data to use in the cart.php file?

thanks!

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

  • Posts: 81539
  • Thank you received: 13069
  • MODERATOR
5 years 9 months ago #295088

Hi,

The add_to_cart_ajax view file is used on listings were the has_options information is generated because it is needed to know if be can display the add to cart button or the "choose options" button.
On the cart module, we don't know to know about that so there is no has_options variable already generated.
If you want to know if a product has options, you need to do like the code which displays the options. So you need to do a foreach on the products of the cart $this->element->products and if the attribute cart_product_option_parent_id of the product is equal to the cart_product_id of the current product then that first product is an option of the second.
For example:

$has_options = false;
foreach($this->element->products as $j => $optionElement) {
	if($optionElement->cart_product_option_parent_id != $product->cart_product_id)
		continue;
	$has_options = true;
	break;
}

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

  • Posts: 162
  • Thank you received: 0
5 years 9 months ago #295210

Hi,

the code that you provided doesn't work for me.

Looking at the database table, I've found a simple way to do this:

/**
 * If the parent product ID is equal with 0 then the product doesn't have child products(options). If it has a number bigger then 0 it means that the product has child products(options).
 */
$has_options = true;
if ($product->product_parent_id == 0) {
  $has_options = false;
}

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

Time to create page: 0.059 seconds
Powered by Kunena Forum