diff --git a/classes/class-wc-product-gift-wrap.php b/classes/class-wc-product-gift-wrap.php index b0b3996..6d2e06e 100644 --- a/classes/class-wc-product-gift-wrap.php +++ b/classes/class-wc-product-gift-wrap.php @@ -1,6 +1,6 @@ gift_wrap_enabled = get_option( 'product_gift_wrap_enabled' ); - $this->gift_wrap_cost = get_option( 'product_gift_wrap_cost', 0 ); - $this->product_gift_wrap_message = get_option( 'product_gift_wrap_message' ); + public function __construct() + { + $this->gift_wrap_enabled = get_option('product_gift_wrap_enabled'); + $this->gift_wrap_cost = get_option('product_gift_wrap_cost', 0); + $this->product_gift_wrap_message = get_option('product_gift_wrap_message'); + $this->message_also_for_no_gift_wrap = get_option('message_also_for_no_gift_wrap'); } /** @@ -69,29 +81,30 @@ public function __construct() { * * @since 1.3.1 */ - public static function init() { + public static function init() + { $self = new self(); // Load plugin text domain. - add_action( 'init', array( $self, 'load_plugin_textdomain' ) ); + add_action('init', array($self, 'load_plugin_textdomain')); // Display on the front end. - add_action('woocommerce_before_add_to_cart_button', array( $self, 'gift_option_html' ), 10 ); + add_action('woocommerce_before_add_to_cart_button', array($self, 'gift_option_html'), 10); // Filters for cart actions. - add_filter( 'woocommerce_add_cart_item_data', array( $self, 'add_cart_item_data' ), 10, 2 ); - add_filter( 'woocommerce_get_cart_item_from_session', array( $self, 'get_cart_item_from_session' ), 10, 2 ); - add_filter( 'woocommerce_get_item_data', array( $self, 'get_item_data' ), 10, 2 ); - add_filter( 'woocommerce_add_cart_item', array( $self, 'add_cart_item' ), 10, 1 ); - add_action( 'woocommerce_add_order_item_meta', array( $self, 'add_order_item_meta' ), 10, 2 ); + add_filter('woocommerce_add_cart_item_data', array($self, 'add_cart_item_data'), 10, 2); + add_filter('woocommerce_get_cart_item_from_session', array($self, 'get_cart_item_from_session'), 10, 2); + add_filter('woocommerce_get_item_data', array($self, 'get_item_data'), 10, 2); + add_filter('woocommerce_add_cart_item', array($self, 'add_cart_item'), 10, 1); + add_action('woocommerce_add_order_item_meta', array($self, 'add_order_item_meta'), 10, 2); // Write Panels. - add_action( 'woocommerce_product_options_pricing', array( $self, 'write_panel' ) ); - add_action( 'woocommerce_process_product_meta', array( $self, 'write_panel_save' ) ); + add_action('woocommerce_product_options_pricing', array($self, 'write_panel')); + add_action('woocommerce_process_product_meta', array($self, 'write_panel_save')); // Admin. - add_action( 'woocommerce_settings_general_options_end', array( $self, 'display_admin_settings' ) ); - add_action( 'woocommerce_update_options_general', array( $self, 'save_admin_settings' ) ); + add_action('woocommerce_settings_general_options_end', array($self, 'display_admin_settings')); + add_action('woocommerce_update_options_general', array($self, 'save_admin_settings')); } /** @@ -99,11 +112,13 @@ public static function init() { * * @since 1.0.0 */ - public static function install() { - add_option( 'product_gift_wrap_enabled', false ); - add_option( 'product_gift_wrap_cost', '0' ); + public static function install() + { + add_option('product_gift_wrap_enabled', false); + add_option('product_gift_wrap_cost', '0'); // Translators: %s is the price for the gift wrap. - add_option( 'product_gift_wrap_message', sprintf( __( 'Gift wrap this item for %s?', 'product-gift-wrap-for-woocommerce' ), '{price}' ) ); + add_option('product_gift_wrap_message', sprintf(__('Gift wrap this item for %s?', 'product-gift-wrap-for-woocommerce'), '{price}')); + add_option('message_also_for_no_gift_wrap', 'no'); } /** @@ -111,11 +126,12 @@ public static function install() { * * @since 1.0.0 */ - public function load_plugin_textdomain() { - $locale = apply_filters( 'plugin_locale', get_locale(), 'product-gift-wrap-for-woocommerce' ); + public function load_plugin_textdomain() + { + $locale = apply_filters('plugin_locale', get_locale(), 'product-gift-wrap-for-woocommerce'); - load_textdomain( 'product-gift-wrap-for-woocommerce', trailingslashit( WP_LANG_DIR ) . 'product-gift-wrap-for-woocommerce/product-gift-wrap-for-woocommerce-' . $locale . '.mo' ); - load_plugin_textdomain( 'product-gift-wrap-for-woocommerce', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); + load_textdomain('product-gift-wrap-for-woocommerce', trailingslashit(WP_LANG_DIR) . 'product-gift-wrap-for-woocommerce/product-gift-wrap-for-woocommerce-' . $locale . '.mo'); + load_plugin_textdomain('product-gift-wrap-for-woocommerce', false, dirname(plugin_basename(__FILE__)) . '/languages/'); } /** @@ -132,10 +148,11 @@ public function load_plugin_textdomain() { * @author Aelia * @link http://aelia.co */ - protected function get_price_in_currency( $price, $to_currency = null, $from_currency = null ) { + protected function get_price_in_currency($price, $to_currency = null, $from_currency = null) + { // If source currency is not specified, take the shop's base currency as a default. - if ( empty( $from_currency ) ) { - $from_currency = get_option( 'woocommerce_currency' ); + if (empty($from_currency)) { + $from_currency = get_option('woocommerce_currency'); } /** @@ -148,7 +165,7 @@ protected function get_price_in_currency( $price, $to_currency = null, $from_cur * taken from a previous subscription, because it's processing a renewal, and such * renewal should keep the original prices, in the original currency. */ - if ( empty( $to_currency ) ) { + if (empty($to_currency)) { $to_currency = get_woocommerce_currency(); } @@ -158,7 +175,7 @@ protected function get_price_in_currency( $price, $to_currency = null, $from_cur * amount, without any conversion being performed. Your plugin won't even need to know if * the multi-currency plugin is installed or active. */ - return apply_filters( 'wc_aelia_cs_convert', $price, $from_currency, $to_currency ); + return apply_filters('wc_aelia_cs_convert', $price, $from_currency, $to_currency); } /** @@ -167,32 +184,33 @@ protected function get_price_in_currency( $price, $to_currency = null, $from_cur * @access public * @return void */ - public function gift_option_html() { + public function gift_option_html() + { global $post; - $is_wrappable = get_post_meta( $post->ID, '_is_gift_wrappable', true ); + $is_wrappable = get_post_meta($post->ID, '_is_gift_wrappable', true); - if ( '' === $is_wrappable && $this->gift_wrap_enabled ) { + if ('' === $is_wrappable && $this->gift_wrap_enabled) { $is_wrappable = 'yes'; } - if ( 'yes' === $is_wrappable ) { + if ('yes' === $is_wrappable) { - $current_value = ( isset( $_REQUEST['gift_wrap'] ) && ! empty( absint( $_REQUEST['gift_wrap'] ) ) ) ? 1 : 0; + $current_value = (isset($_REQUEST['gift_wrap']) && !empty(absint($_REQUEST['gift_wrap']))) ? 1 : 0; - $cost = get_post_meta( $post->ID, '_gift_wrap_cost', true ); + $cost = get_post_meta($post->ID, '_gift_wrap_cost', true); - if ( '' === $cost ) { + if ('' === $cost) { $cost = $this->gift_wrap_cost; } - $price_text = $cost > 0 ? wc_price( $this->get_price_in_currency( $cost ) ) : __( 'free', 'product-gift-wrap-for-woocommerce' ); + $price_text = $cost > 0 ? wc_price($this->get_price_in_currency($cost)) : __('free', 'product-gift-wrap-for-woocommerce'); - wc_get_template( 'gift-wrap.php', array( + wc_get_template('gift-wrap.php', array( 'product_gift_wrap_message' => $this->product_gift_wrap_message, 'current_value' => $current_value, 'price_text' => $price_text, - ), 'product-gift-wrap-for-woocommerce', WC_PRODUCT_GIFT_WRAP_PATH . '/templates/' ); + ), 'product-gift-wrap-for-woocommerce', WC_PRODUCT_GIFT_WRAP_PATH . '/templates/'); } } @@ -204,14 +222,15 @@ public function gift_option_html() { * @param mixed $product_id Product ID or object. * @return array an Array of item meta */ - public function add_cart_item_data( $cart_item_meta, $product_id ) { - $is_wrappable = get_post_meta( $product_id, '_is_gift_wrappable', true ); + public function add_cart_item_data($cart_item_meta, $product_id) + { + $is_wrappable = get_post_meta($product_id, '_is_gift_wrappable', true); - if ( '' === $is_wrappable && $this->gift_wrap_enabled ) { + if ('' === $is_wrappable && $this->gift_wrap_enabled) { $is_wrappable = 'yes'; } - if ( ! empty( $_POST['gift_wrap'] ) && 'yes' === $is_wrappable ) { + if (!empty($_POST['gift_wrap']) && 'yes' === $is_wrappable) { $cart_item_meta['gift_wrap'] = true; } @@ -226,22 +245,23 @@ public function add_cart_item_data( $cart_item_meta, $product_id ) { * @param mixed $values an array of values. * @return array an array of cart item data */ - public function get_cart_item_from_session( $cart_item, $values ) { - if ( empty( $values['gift_wrap'] ) ) { + public function get_cart_item_from_session($cart_item, $values) + { + if (empty($values['gift_wrap'])) { return $cart_item; } $cart_item['gift_wrap'] = true; - $cost = get_post_meta( $cart_item['product_id'], '_gift_wrap_cost', true ); + $cost = get_post_meta($cart_item['product_id'], '_gift_wrap_cost', true); - if ( '' === $cost ) { + if ('' === $cost) { $cost = $this->gift_wrap_cost; } - $product = wc_get_product( $values['variation_id'] ? $values['variation_id'] : $values['product_id'] ); + $product = wc_get_product($values['variation_id'] ? $values['variation_id'] : $values['product_id']); - $cart_item['data']->set_price( $product->get_price() + $this->get_price_in_currency( $cost ) ); + $cart_item['data']->set_price($product->get_price() + $this->get_price_in_currency($cost)); return $cart_item; } @@ -254,15 +274,24 @@ public function get_cart_item_from_session( $cart_item, $values ) { * @param mixed $cart_item cart item. * @return array an array for the gift wrap data */ - public function get_item_data( $item_data, $cart_item ) { - if ( empty( $cart_item['gift_wrap'] ) ) { + public function get_item_data($item_data, $cart_item) + { + if (empty($cart_item['gift_wrap'])) { + if ($this->message_also_for_no_gift_wrap != 'no') { + $item_data[] = array( + 'name' => __('Gift Wrapped', 'product-gift-wrap-for-woocommerce'), + 'value' => __('No', 'product-gift-wrap-for-woocommerce'), + 'display' => __('No', 'product-gift-wrap-for-woocommerce'), + ); + } + return $item_data; } $item_data[] = array( - 'name' => __( 'Gift Wrapped', 'product-gift-wrap-for-woocommerce' ), - 'value' => __( 'Yes', 'product-gift-wrap-for-woocommerce' ), - 'display' => __( 'Yes', 'product-gift-wrap-for-woocommerce' ), + 'name' => __('Gift Wrapped', 'product-gift-wrap-for-woocommerce'), + 'value' => __('Yes', 'product-gift-wrap-for-woocommerce'), + 'display' => __('Yes', 'product-gift-wrap-for-woocommerce'), ); return $item_data; @@ -275,20 +304,21 @@ public function get_item_data( $item_data, $cart_item ) { * @param mixed $cart_item array of cart item data. * @return array array of cart item data */ - public function add_cart_item( $cart_item ) { - if ( empty( $cart_item['gift_wrap'] ) ) { + public function add_cart_item($cart_item) + { + if (empty($cart_item['gift_wrap'])) { return $cart_item; } - $cost = get_post_meta( $cart_item['product_id'], '_gift_wrap_cost', true ); + $cost = get_post_meta($cart_item['product_id'], '_gift_wrap_cost', true); - if ( '' === $cost ) { + if ('' === $cost) { $cost = $this->gift_wrap_cost; } - $product = wc_get_product( $cart_item['variation_id'] ? $cart_item['variation_id'] : $cart_item['product_id'] ); + $product = wc_get_product($cart_item['variation_id'] ? $cart_item['variation_id'] : $cart_item['product_id']); - $cart_item['data']->set_price( $product->get_price() + $this->get_price_in_currency( $cost ) ); + $cart_item['data']->set_price($product->get_price() + $this->get_price_in_currency($cost)); return $cart_item; } @@ -301,12 +331,13 @@ public function add_cart_item( $cart_item ) { * @param mixed $cart_item cart item data. * @return void */ - public function add_order_item_meta( $item_id, $cart_item ) { - if ( empty( $cart_item['gift_wrap'] ) ) { + public function add_order_item_meta($item_id, $cart_item) + { + if (empty($cart_item['gift_wrap'])) { return; } - wc_add_order_item_meta( $item_id, __( 'Gift Wrapped', 'product-gift-wrap-for-woocommerce' ), __( 'Yes', 'product-gift-wrap-for-woocommerce' ) ); + wc_add_order_item_meta($item_id, __('Gift Wrapped', 'product-gift-wrap-for-woocommerce'), __('Yes', 'product-gift-wrap-for-woocommerce')); } /** @@ -315,34 +346,35 @@ public function add_order_item_meta( $item_id, $cart_item ) { * @access public * @return void */ - public function write_panel() { + public function write_panel() + { global $post; echo '
'; - $is_wrappable = get_post_meta( $post->ID, '_is_gift_wrappable', true ); + $is_wrappable = get_post_meta($post->ID, '_is_gift_wrappable', true); - if ( '' === $is_wrappable && $this->gift_wrap_enabled ) { + if ('' === $is_wrappable && $this->gift_wrap_enabled) { $is_wrappable = 'yes'; } - woocommerce_wp_checkbox( array( - 'id' => '_is_gift_wrappable', - 'wrapper_class' => '', - 'value' => $is_wrappable, - 'label' => __( 'Gift Wrappable', 'product-gift-wrap-for-woocommerce' ), - 'description' => __( 'Enable this option if the customer can choose gift wrapping.', 'product-gift-wrap-for-woocommerce' ), - ) ); - - woocommerce_wp_text_input( array( - 'id' => '_gift_wrap_cost', - 'label' => __( 'Gift Wrap Cost', 'product-gift-wrap-for-woocommerce' ), - 'placeholder' => $this->gift_wrap_cost, - 'desc_tip' => true, - 'description' => __( 'Override the default cost by inputting a cost here.', 'product-gift-wrap-for-woocommerce' ), - ) ); - - wc_enqueue_js( " + woocommerce_wp_checkbox(array( + 'id' => '_is_gift_wrappable', + 'wrapper_class' => '', + 'value' => $is_wrappable, + 'label' => __('Gift Wrappable', 'product-gift-wrap-for-woocommerce'), + 'description' => __('Enable this option if the customer can choose gift wrapping.', 'product-gift-wrap-for-woocommerce'), + )); + + woocommerce_wp_text_input(array( + 'id' => '_gift_wrap_cost', + 'label' => __('Gift Wrap Cost', 'product-gift-wrap-for-woocommerce'), + 'placeholder' => $this->gift_wrap_cost, + 'desc_tip' => true, + 'description' => __('Override the default cost by inputting a cost here.', 'product-gift-wrap-for-woocommerce'), + )); + + wc_enqueue_js(" jQuery('input#_is_gift_wrappable').change(function(){ jQuery('._gift_wrap_cost_field').hide(); @@ -352,7 +384,7 @@ public function write_panel() { } }).change(); - " ); + "); } /** @@ -362,13 +394,14 @@ public function write_panel() { * @param mixed $post_id Product ID. * @return void */ - public function write_panel_save( $post_id ) { - $_is_gift_wrappable = ! empty( $_POST['_is_gift_wrappable'] ) ? 'yes' : 'no'; - $_gift_wrap_cost = ! empty( $_POST['_gift_wrap_cost'] ) ? wc_clean( $_POST['_gift_wrap_cost'] ) : ''; - $_gift_wrap_cost = str_replace( ',', '.', $_gift_wrap_cost ); - - update_post_meta( $post_id, '_is_gift_wrappable', $_is_gift_wrappable ); - update_post_meta( $post_id, '_gift_wrap_cost', $_gift_wrap_cost ); + public function write_panel_save($post_id) + { + $_is_gift_wrappable = !empty($_POST['_is_gift_wrappable']) ? 'yes' : 'no'; + $_gift_wrap_cost = !empty($_POST['_gift_wrap_cost']) ? wc_clean($_POST['_gift_wrap_cost']) : ''; + $_gift_wrap_cost = str_replace(',', '.', $_gift_wrap_cost); + + update_post_meta($post_id, '_is_gift_wrappable', $_is_gift_wrappable); + update_post_meta($post_id, '_gift_wrap_cost', $_gift_wrap_cost); } /** @@ -377,28 +410,36 @@ public function write_panel_save( $post_id ) { * @access public * @return array Plugin settings */ - public function admin_settings() { + public function admin_settings() + { // Init settings. $this->settings = array( array( - 'name' => __( 'Gift Wrapping Enabled by Default?', 'product-gift-wrap-for-woocommerce' ), - 'desc' => __( 'Enable this to allow gift wrapping for products by default.', 'product-gift-wrap-for-woocommerce' ), + 'name' => __('Gift Wrapping Enabled by Default?', 'product-gift-wrap-for-woocommerce'), + 'desc' => __('Enable this to allow gift wrapping for products by default.', 'product-gift-wrap-for-woocommerce'), 'id' => 'product_gift_wrap_enabled', 'type' => 'checkbox', ), array( - 'name' => __( 'Default Gift Wrap Cost', 'product-gift-wrap-for-woocommerce' ), - 'desc' => __( 'The cost of gift wrap unless overridden per-product.', 'product-gift-wrap-for-woocommerce' ), + 'name' => __('Default Gift Wrap Cost', 'product-gift-wrap-for-woocommerce'), + 'desc' => __('The cost of gift wrap unless overridden per-product.', 'product-gift-wrap-for-woocommerce'), 'id' => 'product_gift_wrap_cost', 'type' => 'text', 'desc_tip' => true, ), array( - 'name' => __( 'Gift Wrap Message', 'product-gift-wrap-for-woocommerce' ), + 'name' => __('Gift Wrap Message', 'product-gift-wrap-for-woocommerce'), 'id' => 'product_gift_wrap_message', - 'desc' => __( 'Note: {price} will be replaced with the gift wrap cost.', 'product-gift-wrap-for-woocommerce' ), + 'desc' => __('Note: {price} will be replaced with the gift wrap cost.', 'product-gift-wrap-for-woocommerce'), 'type' => 'text', - 'desc_tip' => __( 'Label shown to the user on the frontend.', 'product-gift-wrap-for-woocommerce' ), + 'desc_tip' => __('Label shown to the user on the frontend.', 'product-gift-wrap-for-woocommerce'), + ), + array( + 'name' => __('Cart Message if also for NO gift wrap', 'product-gift-wrap-for-woocommerce'), + 'id' => 'message_also_for_no_gift_wrap', + 'desc' => __('Select if you want to see explicit message if customer did not select gift wrap option.', 'product-gift-wrap-for-woocommerce'), + 'type' => 'checkbox', + 'desc_tip' => __('Explicite message shown to the user and to shop manager if NO gift wrap required.', 'product-gift-wrap-for-woocommerce'), ), ); @@ -411,8 +452,9 @@ public function admin_settings() { * @access public * @return void */ - public function display_admin_settings() { - woocommerce_admin_fields( $this->admin_settings() ); + public function display_admin_settings() + { + woocommerce_admin_fields($this->admin_settings()); } /** @@ -421,7 +463,8 @@ public function display_admin_settings() { * @access public * @return void */ - public function save_admin_settings() { - woocommerce_update_options( $this->admin_settings() ); + public function save_admin_settings() + { + woocommerce_update_options($this->admin_settings()); } } diff --git a/product-gift-wrap-for-woocommerce.php b/product-gift-wrap-for-woocommerce.php index 2091527..a713e31 100644 --- a/product-gift-wrap-for-woocommerce.php +++ b/product-gift-wrap-for-woocommerce.php @@ -3,10 +3,10 @@ /** * Plugin Name: Product Gift Wrap for WooCommerce * Plugin URI: https://github.com/yehudaTiram/product-gift-wrap-for-woocommerce - * Description: Add an option to your products to enable gift wrapping. Optionally charge a fee. + * Description: Add an option to your products to enable gift wrapping. Optionally charge a fee. Settings is in Woocommerce settings - General * Version: 1.3.1 * Author: Rémy Perona, modified by Yehuda Tiram - * Author URI: https://remyperona.fr + * Author URI: https://atarimtr.co.il * Requires at least: 3.5 * Tested up to: 4.8 * Text Domain: product-gift-wrap-for-woocommerce