Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Define the new custom meta product field
- **/
- $product_addons = array(
- array(
- 'name' => 'Deliverydate',
- 'description' => '',
- 'type' => 'custom',
- 'position' => 0,
- 'options' => array(
- array(
- 'label' => 'Deliverydate',
- 'price' => ''
- )
- ),
- 'required' => 1
- )
- );
- add_action('woocommerce_before_add_to_cart_button', 'product_custom_addons');
- /**
- * Add custom input fields to the single product
- **/
- function product_custom_addons() {
- global $post, $product_addons;
- if (is_array($product_addons) && sizeof($product_addons)>0) foreach ($product_addons as $addon) :
- if (!isset($addon['name'])) continue;
- ?>
- <div class="product-addon product-addon-<?php echo sanitize_title($addon['name']); ?>">
- <?php if ($addon['name']) : ?><h3><?php echo wptexturize($addon['name']); ?> <?php if ($addon['type']=='file_upload') echo sprintf(__('(max size %s)', 'wc_product_addons'), $this->max_upload_size()); ?></h3><?php endif; ?>
- <?php if ($addon['description']) : ?><p><?php echo wptexturize($addon['description']); ?></p><?php endif; ?>
- <?php
- switch ($addon['type']) :
- case "custom" :
- foreach ($addon['options'] as $option) :
- $current_value = (isset($_POST['addon-' . sanitize_title( $addon['name'] ) . '-' . sanitize_title( $option['label'] )])) ? $_POST['addon-' . sanitize_title( $addon['name'] ) . '-' . sanitize_title( $option['label'] )] : '';
- $price = ($option['price']>0) ? ' (' . woocommerce_price($option['price']) . ')' : '';
- echo '<p class="form-row form-row-wide"><label>'. wptexturize($option['label']) . $price .': <input type="text" class="input-text" name="addon-' . sanitize_title( $addon['name'] ) . '-' . sanitize_title( $option['label'] ) .'" value="'.$current_value.'" /></label></p>';
- endforeach;
- break;
- endswitch;
- ?>
- <div class="clear"></div>
- </div>
- <?php
- endforeach;
- }
- add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_custom_data', 10, 2 );
- /**
- * Add custom input data to the cart item
- **/
- function add_cart_item_custom_data( $cart_item_meta, $product_id ) {
- global $woocommerce, $product_addons;
- $cart_item_meta['addons'] = array();
- if (is_array($product_addons) && sizeof($product_addons)>0) foreach ($product_addons as $addon) :
- if (!isset($addon['name'])) continue;
- switch ($addon['type']) :
- case "custom" :
- // Posted var = label, value = custom
- foreach ($addon['options'] as $option) :
- $posted = (isset( $_POST['addon-' . sanitize_title( $addon['name'] ) . '-' . sanitize_title( $option['label'] )] )) ? $_POST['addon-' . sanitize_title( $addon['name'] ) . '-' . sanitize_title( $option['label'] )] : '';
- if (!$posted) continue;
- $cart_item_meta['addons'][] = array(
- 'name' => esc_attr( $option['label'] ),
- 'value' => esc_attr( stripslashes( trim( $posted ) ) ),
- 'price' => esc_attr( $option['price'] )
- );
- endforeach;
- break;
- endswitch;
- endforeach;
- return $cart_item_meta;
- }
- add_filter( 'woocommerce_get_cart_item_from_session', 'get_cart_custom_item_from_session', 10, 2 );
- /**
- * Get custom meta data from session
- **/
- function get_cart_custom_item_from_session( $cart_item, $values ) {
- if (isset($values['addons'])) :
- $cart_item['addons'] = $values['addons'];
- $cart_item = add_custom_cart_item( $cart_item );
- endif;
- return $cart_item;
- }
- add_filter( 'woocommerce_get_item_data', 'get_item_custom_data', 10, 2 );
- /**
- * Get product item data
- **/
- function get_item_custom_data( $other_data, $cart_item ) {
- if (isset($cart_item['addons'])) :
- foreach ($cart_item['addons'] as $addon) :
- $name = $addon['name'];
- if ($addon['price']>0) $name .= ' (' . woocommerce_price($addon['price']) . ')';
- $other_data[] = array(
- 'name' => $name,
- 'value' => $addon['value'],
- 'display' => isset($addon['display']) ? $addon['display'] : ''
- );
- endforeach;
- endif;
- return $other_data;
- }
- add_filter( 'woocommerce_add_cart_item', 'add_custom_cart_item', 10, 1 );
- /**
- * Add cart item
- **/
- function add_custom_cart_item( $cart_item ) {
- // Adjust price if addons are set
- if (isset($cart_item['addons'])) :
- $extra_cost = 0;
- foreach ($cart_item['addons'] as $addon) :
- if ($addon['price']>0) $extra_cost += $addon['price'];
- endforeach;
- $cart_item['data']->adjust_price( $extra_cost );
- endif;
- return $cart_item;
- }
- add_action( 'woocommerce_order_item_meta', 'order_item_custom_meta', 10, 2 );
- /**
- * Add custom meta data to order
- **/
- function order_item_custom_meta( $item_meta, $cart_item ) {
- // Add the fields
- if (isset($cart_item['addons'])) :
- foreach ($cart_item['addons'] as $addon) :
- $name = $addon['name'];
- if ($addon['price']>0) $name .= ' (' . strip_tags(woocommerce_price($addon['price'])) . ')';
- $item_meta->add( $name, $addon['value'] );
- endforeach;
- endif;
- }
- add_filter( 'woocommerce_add_to_cart_validation', 'validate_add_custom_cart_item', 10, 3 );
- /**
- * Validate the custom meta field before adding to cart
- **/
- function validate_add_custom_cart_item( $passed, $product_id, $qty ) {
- global $woocommerce, $product_addons;
- if (is_array($product_addons) && sizeof($product_addons)>0) foreach ($product_addons as $addon) {
- if (!isset($addon['name'])) continue;
- if (!isset($addon['required'])) continue;
- if ($addon['required']) {
- switch ($addon['type']) :
- case "custom" :
- foreach ($addon['options'] as $option) {
- $posted = (isset( $_POST['addon-' . sanitize_title( $addon['name'] ) . '-' . sanitize_title( $option['label'] )] )) ? $_POST['addon-' . sanitize_title( $addon['name'] ) . '-' . sanitize_title( $option['label'] )] : '';
- if (!$posted || sizeof($posted)==0) {
- $passed = false;
- break;
- }
- }
- break;
- endswitch;
- if (!$passed) {
- $woocommerce->add_error( sprintf( __('"%s" is a required field.', 'woocommerce'), $addon['name']) );
- break;
- }
- }
- do_action('woocommerce_validate_posted_addon_data', $addon);
- }
- return $passed;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement