Forked from plugin-republic/custom-fields-for-woocommerce.php
Created
December 31, 2020 14:51
-
-
Save itskawsar/a56f862dba0df3ebe883478ac89c8d7d to your computer and use it in GitHub Desktop.
Revisions
-
plugin-republic revised this gist
Dec 20, 2020 . 1 changed file with 12 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,20 @@ <?php /** * * You can find the complete tutorial for this here: * https://pluginrepublic.com/woocommerce-custom-fields * * Alternatively, check out the plugin * https://pluginrepublic.com/wordpress-plugins/woocommerce-product-add-ons-ultimate/ * */ // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Display the custom text field * @since 1.0.0 -
plugin-republic revised this gist
Dec 20, 2020 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,8 @@ <?php // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Display the custom text field * @since 1.0.0 -
plugin-republic revised this gist
Dec 20, 2020 . 1 changed file with 0 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,4 @@ <?php // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) { -
plugin-republic revised this gist
Dec 13, 2020 . 1 changed file with 2 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,7 @@ <?php /** * https://pluginrepublic.com/wordpress-plugins/woocommerce-product-add-ons-ultimate/ */ // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) { -
Gareth Harris revised this gist
Jun 13, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ * Description: Add custom fields to WooCommerce products * Version: 1.0.0 * Author: Gareth Harris * Author URI: https://pluginrepublic.com/ * Text Domain: cfwc * WC requires at least: 3.4.0 * WC tested up to: 3.4.2 -
Catapult-Themes created this gist
Jun 12, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,145 @@ <?php /** * Plugin Name: Custom Fields for WooCommerce * Description: Add custom fields to WooCommerce products * Version: 1.0.0 * Author: Gareth Harris * Author URI: https://catapultthemes.com/ * Text Domain: cfwc * WC requires at least: 3.4.0 * WC tested up to: 3.4.2 */ // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Display the custom text field * @since 1.0.0 */ function cfwc_create_custom_field() { $args = array( 'id' => 'custom_text_field_title', 'label' => __( 'Custom Text Field Title', 'cfwc' ), 'class' => 'cfwc-custom-field', 'desc_tip' => true, 'description' => __( 'Enter the title of your custom text field.', 'ctwc' ), ); woocommerce_wp_text_input( $args ); } add_action( 'woocommerce_product_options_general_product_data', 'cfwc_create_custom_field' ); /** * Save the custom field * @since 1.0.0 */ function cfwc_save_custom_field( $post_id ) { $product = wc_get_product( $post_id ); $title = isset( $_POST['custom_text_field_title'] ) ? $_POST['custom_text_field_title'] : ''; $product->update_meta_data( 'custom_text_field_title', sanitize_text_field( $title ) ); $product->save(); } add_action( 'woocommerce_process_product_meta', 'cfwc_save_custom_field' ); /** * Display custom field on the front end * @since 1.0.0 */ function cfwc_display_custom_field() { global $post; // Check for the custom field value $product = wc_get_product( $post->ID ); $title = $product->get_meta( 'custom_text_field_title' ); if( $title ) { // Only display our field if we've got a value for the field title printf( '<div class="cfwc-custom-field-wrapper"><label for="cfwc-title-field">%s</label><input type="text" id="cfwc-title-field" name="cfwc-title-field" value=""></div>', esc_html( $title ) ); } } add_action( 'woocommerce_before_add_to_cart_button', 'cfwc_display_custom_field' ); /** * Validate the text field * @since 1.0.0 * @param Array $passed Validation status. * @param Integer $product_id Product ID. * @param Boolean $quantity Quantity */ function cfwc_validate_custom_field( $passed, $product_id, $quantity ) { if( empty( $_POST['cfwc-title-field'] ) ) { // Fails validation $passed = false; wc_add_notice( __( 'Please enter a value into the text field', 'cfwc' ), 'error' ); } return $passed; } add_filter( 'woocommerce_add_to_cart_validation', 'cfwc_validate_custom_field', 10, 3 ); /** * Add the text field as item data to the cart object * @since 1.0.0 * @param Array $cart_item_data Cart item meta data. * @param Integer $product_id Product ID. * @param Integer $variation_id Variation ID. * @param Boolean $quantity Quantity */ function cfwc_add_custom_field_item_data( $cart_item_data, $product_id, $variation_id, $quantity ) { if( ! empty( $_POST['cfwc-title-field'] ) ) { // Add the item data $cart_item_data['title_field'] = $_POST['cfwc-title-field']; $product = wc_get_product( $product_id ); // Expanded function $price = $product->get_price(); // Expanded function $cart_item_data['total_price'] = $price + 100; // Expanded function } return $cart_item_data; } add_filter( 'woocommerce_add_cart_item_data', 'cfwc_add_custom_field_item_data', 10, 4 ); /** * Update the price in the cart * @since 1.0.0 */ function cfwc_before_calculate_totals( $cart_obj ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { return; } // Iterate through each cart item foreach( $cart_obj->get_cart() as $key=>$value ) { if( isset( $value['total_price'] ) ) { $price = $value['total_price']; $value['data']->set_price( ( $price ) ); } } } add_action( 'woocommerce_before_calculate_totals', 'cfwc_before_calculate_totals', 10, 1 ); /** * Display the custom field value in the cart * @since 1.0.0 */ function cfwc_cart_item_name( $name, $cart_item, $cart_item_key ) { if( isset( $cart_item['title_field'] ) ) { $name .= sprintf( '<p>%s</p>', esc_html( $cart_item['title_field'] ) ); } return $name; } add_filter( 'woocommerce_cart_item_name', 'cfwc_cart_item_name', 10, 3 ); /** * Add custom field to order object */ function cfwc_add_custom_data_to_order( $item, $cart_item_key, $values, $order ) { foreach( $item as $cart_item_key=>$values ) { if( isset( $values['title_field'] ) ) { $item->add_meta_data( __( 'Custom Field', 'cfwc' ), $values['title_field'], true ); } } } add_action( 'woocommerce_checkout_create_order_line_item', 'cfwc_add_custom_data_to_order', 10, 4 );