Advertisement
AquariusPL

Untitled

May 8th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.45 KB | None | 0 0
  1.  * List available attributes on the product page in a drop-down selection
  2.  */
  3. function list_attributes_on_product_page() {
  4.     global $product;
  5.     $attributes = $product->get_attributes();
  6.  
  7.     if ( ! $attributes ) {
  8.         return;
  9.     }
  10.  
  11.     //from original script, but here we want to use it for variable products
  12.     /*if ($product->is_type( 'variable' )) {
  13.         return;
  14.     }*/
  15.  
  16.     echo '<div style="padding-bottom:15px;">';
  17.  
  18.     foreach ( $attributes as $attribute ) {
  19.  
  20.         //If product is variable, and attribute is used for variation: woocommerce already handle this input - so it can also be used with attributes of simple products (not variables)
  21.     if($product->is_type( 'variable' ) && $attribute['variation']) {
  22.         continue;
  23.     }
  24.  
  25.         //get taxonomy for the attribute - eg: Size
  26.         $taxonomy = get_taxonomy($attribute['name']);
  27.  
  28.         //get terms - eg: small
  29.         $options = wc_get_product_terms( $product->get_id(), $attribute['name'], array( 'fields' => 'all' ) );
  30.         $label = str_replace('Product ', '', $taxonomy->label);
  31.         //display select input
  32.         ?>
  33.         <div style="padding-bottom:8px;">
  34.             <label for="attribute[<?php echo $attribute['id']; ?>]"><?php echo $label; ?></label>
  35.             <br />
  36.             <!-- add required attribute or not, handle default with "selected" attribute depending your needs -->
  37.             <select name="attribute[<?php echo $attribute['id']; ?>]" id="attribute[<?php echo $attribute['id']; ?>]">
  38.                 <option value disabled selected>Choose an option</option>
  39.                 <?php foreach ( $options as $pa ): ?>
  40.                     <option value="<?php echo $pa->name; ?>"><?php echo $pa->name; ?></option>
  41.                 <?php endforeach; ?>
  42.             </select>
  43.         </div>
  44.         <?php
  45.     }
  46.  
  47.     echo '</div>';
  48. }
  49. add_action('woocommerce_before_add_to_cart_button', 'list_attributes_on_product_page');
  50.  
  51. /**
  52.  * Add selected attributes to cart items
  53.  */
  54. add_filter('woocommerce_add_cart_item_data', 'add_attributes_to_cart_item', 10, 3 );
  55. function add_attributes_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
  56.     $attributes = $_POST['attribute'] ?? null;
  57.  
  58.     if (empty( $attributes ) ) {
  59.         return $cart_item_data;
  60.     }
  61.  
  62.     $cart_item_data['attributes'] = serialize($attributes);
  63.  
  64.     return $cart_item_data;
  65. }
  66.  
  67. /**
  68.  * Display attributes in cart
  69.  */
  70. add_filter( 'woocommerce_get_item_data', 'display_attributes_in_cart', 10, 2 );
  71. function display_attributes_in_cart( $item_data, $cart_item ) {
  72.     if ( empty( $cart_item['attributes'] ) ) {
  73.         return $item_data;
  74.     }
  75.  
  76.     foreach (unserialize($cart_item['attributes']) as $attributeID => $value) {
  77.         $attribute = wc_get_attribute($attributeID);
  78.         $item_data[] = array(
  79.             'key'     => $attribute->name,
  80.             'value'   => $value,
  81.             'display' => '',
  82.         );
  83.     }
  84.  
  85.     return $item_data;
  86. }
  87.  
  88. /**
  89.  * Add attribute data to order items
  90.  */
  91. add_action( 'woocommerce_checkout_create_order_line_item', 'add_attributes_to_order_items', 10, 4 );
  92. function add_attributes_to_order_items( $item, $cart_item_key, $values, $order ) {
  93.     if ( empty( $values['attributes'] ) ) {
  94.         return;
  95.     }
  96.  
  97.     foreach (unserialize($values['attributes']) as $attributeID => $value) {
  98.         $attribute = wc_get_attribute($attributeID);
  99.         $item->add_meta_data( $attribute->name, $value );
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement