skip to Main Content

I have a product where it has custom attribute(delivaryDate is attribute name) but no variations are created with it.

On the product page, the attribute is selected.

On the cart page, I am able to retrieve data on all attributes where it has variations. But the attribute where variations are not created cannot be retrieved.

Code used :

foreach ( WC()->cart->get_cart() as $cart_item ) {
$item_data = $cart_item['data'];
        echo $item_data;
}

enter image description here

Screenshot of dump for reference.

So is a way to retrieve all attributes values even variation not created with those attributes.

I am looking to get attributeName1_attaributeName2 as a value from each product on cart. Any help would be appericated.

Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    I was able to get this done using a plugin :

    Extra product options For WooCommerce and use the below code

    foreach ( WC()->cart->get_cart() as $cart_item ) {$wepo_options = $cart_item['thwepof_options'];foreach($wepo_options as $key => $value){if($key == 'delivery_week'){$deliveryDays[] = $value['value'];}}}
    

  2. When working with WooCommerce, it is always preferred to use their methods.

    For getting attributes this would be, continuing as from $cart_item['data']; that you already have

    // Get the product object for the cart item
    $product = $cart_item['data'];
        
    // Get the product attributes
    $attributes = $product->get_attributes();
     
    // Iterate through each attribute
    foreach ( $attributes as $attribute ) {
      // Get the attribute name only
       $attribute_name = $attribute->get_name();
      // If you want to print the attribute name too
      echo $attribute_name;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search