Created
December 29, 2017 17:10
-
-
Save WP-Panda/a16f2da4fc5f514a46d36197c4d887ab to your computer and use it in GitHub Desktop.
This file contains 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 characters
<?php | |
/* | |
Plugin Name: Cr Woo Additional Services | |
Plugin URI: http://wp-panda.com/?plugin='cr-woo-additional-services' | |
Description: Плагин для добавления дополнительных услуг к продаваемым товрарам | |
Version: 1.0.0 | |
Author: Wp Panda | |
Author URI: http://wp-panda.com/ | |
Text Domain: cr-woo-additional-services | |
License: GPLv2 or later | |
*/ | |
register_activation_hook( __FILE__, 'cr_woo_additional_services_activate' ); | |
/** | |
* Fired during plugin activation | |
* | |
* @version 1.0.0 | |
* @since 1.0.0 | |
* @see https://codex.wordpress.org/Function_Reference/register_activation_hook | |
*/ | |
function cr_woo_additional_services_activate() { | |
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
$error = sprintf( __( 'Please install or activate the <a href="%s" title="">WooCommerce</a> to run this plugin.', 'cr-woo-additional-services' ), esc_url( 'https://wordpress.org/plugins/woocommerce/' ) ); | |
} | |
if ( ! empty( $error ) ) { | |
deactivate_plugins( plugin_basename( __FILE__ ) ); | |
$message = sprintf( '<div class="error"><p>%1$s</p><p><i>%2$s</i> %3$s.</p></div>', $error, 'Cr Woo Additional Services', __( 'has been deactivated', 'cr-woo-additional-services' ) ); | |
wp_die( $message, 'Freight For Woocommerce', array( 'back_link' => true ) ); | |
} | |
} | |
/** | |
* Load the plugin text domain for translation. | |
* | |
* @version 1.0.0 | |
* @since 1.0.0 | |
* @see https://codex.wordpress.org/Function_Reference/load_plugin_textdomain | |
*/ | |
add_action( 'plugins_loaded', 'cr_woo_additional_services_textdomain' ); | |
function cr_woo_additional_services_textdomain() { | |
$locale = apply_filters( 'plugin_locale', get_locale(), 'cr-woo-additional-services' ); | |
load_textdomain( 'cr-woo-additional-services', trailingslashit( WP_LANG_DIR ) . 'cr-woo-additional-services/cr-woo-additional-services-' . $locale . '.mo' ); | |
load_plugin_textdomain( 'cr-woo-additional-services', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); | |
} | |
add_action( 'admin_notices', 'cr_woo_additional_services_notice' ); | |
/** | |
* Load the plugin text domain for translation. | |
* | |
* @version 1.0.1 | |
* @since 1.0.0 | |
*/ | |
function cr_woo_additional_services_notice() { | |
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
$error = sprintf( __( 'Please install or activate the <a href="%s" title="">WooCommerce</a> to run this plugin.', 'cr-woo-additional-services' ), esc_url( 'https://wordpress.org/plugins/woocommerce/' ) ); | |
printf( '<div class="error notice is-dismissible"><p>%1$s</p><p><i>%2$s</i> %3$s.</p></div>', $error, 'Cr Woo Additional Services', __( 'has been deactivated', 'cr-woo-additional-services' ) ); | |
deactivate_plugins( plugin_basename( __FILE__ ) ); | |
} | |
} | |
/** | |
* Add frontend assets | |
* | |
* @version 1.0.0 | |
* @since 1.0.0 | |
* @see https://developer.wordpress.org/reference/functions/wp_enqueue_script/ | |
*/ | |
function cr_woo_additional_services_frontend_assets() { | |
wp_register_script( 'cr-woo-additional-services', plugins_url( 'assets/js/cr-woo-additional-services.js', __FILE__ ), array(), '1.0.0', true ); | |
wp_register_style( 'cr-woo-additional-services', plugins_url( 'assets/css/cr-woo-additional-services.css', __FILE__ ) ); | |
} | |
add_action( 'wp_enqueue_scripts', 'cr_woo_additional_services_frontend_assets' ); | |
add_action( 'woocommerce_product_options_general_product_data', 'cr_woo_additional_services_add_field' ); | |
/** | |
* Add field for coupon | |
* | |
* @version 1.0.0 | |
* @since 1.0.0 | |
* @see https://github.com/woothemes/woocommerce/blob/master/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php#L78 | |
*/ | |
function cr_woo_additional_services_add_field() { | |
$use = get_option( 'cr_woo_additional_services_on' ); | |
if ( $use === 'yes' ) { | |
global $woocommerce, $post; | |
echo '<div class="options_group">'; | |
woocommerce_wp_text_input( | |
array( | |
'id' => '_cr_woo_additional', | |
'wrapper_class' => 'show_if_simple', | |
'label' => __( 'Стоимость услуги', 'cr-woo-additional-services' ), | |
'description' => __( 'Введите стоимость услуги подшива!', 'cr-woo-additional-services' ) | |
) | |
); | |
echo '</div>'; | |
} | |
} | |
add_action( 'woocommerce_process_product_meta', 'cr_woo_additional_services_save_field' ); | |
/** | |
* Save field | |
* | |
* @since 1.0.0 | |
* @version 1.0.0 | |
* @see https://github.com/woothemes/woocommerce/blob/master/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php#L279 | |
* | |
*/ | |
function cr_woo_additional_services_save_field( $post_id ) { | |
$woocommerce_checkbox = isset( $_POST[ '_cr_woo_additional' ] ) ? esc_attr( $_POST[ '_cr_woo_additional' ] ) : ''; | |
update_post_meta( $post_id, '_cr_woo_additional', $woocommerce_checkbox ); | |
} | |
add_action( 'woocommerce_after_add_to_cart_button', 'cr_woo_additional_services_show_coupon', 35 ); | |
/** | |
* Output partners text for thankyou page | |
* | |
* @since 1.0.0 | |
* @version 1.0.0 | |
* @see https://github.com/woothemes/woocommerce/blob/e39274e75391a186413da0bc5c217fa4c8bf2885/templates/checkout/thankyou.php#L38 | |
* | |
* @param $text | |
* @param $order | |
* | |
* @return string | |
*/ | |
function cr_woo_additional_services_show_coupon() { | |
global $post; | |
$use = get_option( 'cr_woo_additional_services_on' ); | |
if ( empty( $use ) || $use !== 'yes' ) { | |
return ''; | |
} | |
$vat = get_post_meta( $post->ID, '_cr_woo_additional', true ); | |
if ( ! empty( $vat ) ) { | |
wp_enqueue_script( 'cr-woo-additional-services' ); | |
wp_enqueue_style( 'cr-woo-additional-services' ); | |
if ( (int) $vat !== 0 ) { | |
$str = 'Стоимость услуги "подшив" - ' . wc_price( $vat ) . '<br>*Требуется предоплата'; | |
} else { | |
$str = 'Услуга <b>"Подшив"</b> - бесплатна *Требуется предоплата за товар'; | |
} | |
?> | |
<!--<div class="cr_wp_clear"></div>--> | |
<div class="cr_woo_additional_group"> | |
<div class="cr_woo_additional_group_trigger"> | |
<a href="javascript:void(0);" class="cr_woo_additional_group_trigger_button"><i class="cr-icon wp-pandacut" aria-hidden="true"></i> Уменьшить высоту</a> | |
</div> | |
<div class="cr_wp_clear"></div> | |
<div class="cr_woo_additional_group_group"> | |
<input type="text" name="cr_woo_addional" class="cr_woo_addional input-text qtyr text" value=""> | |
<label for="cr_woo_additional"> Введите нужную высоту, и нажмите кнопку “Добавить в корзину”</label> | |
<div class="cr_wp_clear"></div> | |
<p class="cr_woo_add_ser_desk"><?php echo $str; ?></p> | |
<div class="cr_wp_clear"></div> | |
<a href="javascript:void(0);" class="cr_woo_additional_group_trigger_close"><b>Отменить услугу подшива</b><i class="cr-icon wp-pandaclose" aria-hidden="true"></i></a> | |
</div> | |
</div> | |
<?php } | |
} | |
add_filter( 'woocommerce_general_settings', 'cr_woo_additional_services_settings' ); | |
/** | |
* Add additional tab | |
* | |
* @since 1.0.0 | |
* @version 1.0.0 | |
* | |
* @param $tabs | |
* | |
* @return mixed | |
*/ | |
function cr_woo_additional_services_tab( $tabs ) { | |
global $post; | |
$vat = get_post_meta( $post->ID, '_cr_woo_additional', true ); | |
$text = get_option( 'cr_woo_additional_services_desk' ); | |
if ( ! empty( $vat ) && ! empty( $text ) ) { | |
$tabs[ 'test_tab' ] = array( | |
'title' => __( 'Подшив', 'woocommerce' ), | |
'priority' => 500, | |
'callback' => 'cr_woo_additional_services_tab_content' | |
); | |
} | |
return $tabs; | |
} | |
add_filter( 'woocommerce_product_tabs', 'cr_woo_additional_services_tab' ); | |
/** | |
* Add new tab content | |
* | |
* @since 1.0.0 | |
* @version 1.0.0 | |
*/ | |
function cr_woo_additional_services_tab_content() { | |
$text = get_option( 'cr_woo_additional_services_desk' ); | |
printf( '<h2>%s</h2>%s', __( 'Подшив', 'woocommerce' ), $text ); | |
} | |
/** | |
* Add setting section | |
* | |
* @since 1.0.0 | |
* @version 1.0.0 | |
* @see https://github.com/woothemes/woocommerce/blob/c6975fa7d5b5e403b19d59154e081c26342a75fe/includes/admin/settings/class-wc-settings-general.php#L48 | |
* | |
* @param $settings | |
* | |
* @return array | |
*/ | |
function cr_woo_additional_services_settings( $settings ) { | |
$updated_settings = array(); | |
$updated_settings[] = array( | |
'name' => __( 'Cr Woo Additional Services', 'cr-woo-additional-services' ), | |
'type' => 'title', | |
'desc' => __( 'Следующие опции используются для настройки дополнительной услуги - подшив', 'cr-woo-additional-services' ), | |
'id' => 'cr_woo_additional_services' | |
); | |
$updated_settings[] = array( | |
'name' => __( 'Включить/Выключить', 'cr-woo-additional-services' ), | |
'id' => 'cr_woo_additional_services_on', | |
'type' => 'checkbox', | |
'default' => 1, | |
'css' => 'min-width:300px;', | |
'desc' => __( 'Включить опцию подшива', 'text-domain' ), | |
); | |
$updated_settings[] = array( | |
'name' => __( 'Значение поля для бесплатной услуги', 'cr-woo-additional-services' ), | |
'id' => 'cr_woo_additional_services_free', | |
'type' => 'text', | |
'default' => 'free', | |
'desc_tip' => __( 'Введите значение поля для бесплатной услуги', 'cr-woo-additional-services' ), | |
); | |
$updated_settings[] = array( | |
'name' => __( 'Текст Вкладки "Подшив"', 'cr-woo-additional-services' ), | |
'id' => 'cr_woo_additional_services_desk', | |
'type' => 'textarea', | |
'class' => 'short', | |
'css' => 'width:60%; height: 65px;', | |
'default' => '', | |
'desc_tip' => __( 'Введите текст которвй бкдет отображаться во вкладке подшив. <br><span class="dashicons dashicons-warning"></span> Если текст отсутствует вкладка включена не будет.', 'cr-woo-additional-services' ), | |
); | |
$updated_settings[] = array( 'type' => 'sectionend', 'id' => 'cr_woo_additional_services' ); | |
$section = array_merge( $settings, $updated_settings ); | |
return $section; | |
} | |
/** | |
* Добавление подшива в корзину | |
* | |
* @param $cart_item_data | |
* @param $product_id | |
* @param $variation_id | |
* | |
* @return mixed | |
*/ | |
function cr_woo_additional_services_selection( $cart_item_data, $product_id, $variation_id ) { | |
if ( ! empty( $_REQUEST[ 'cr_woo_addional' ] ) ) { | |
$cart_item_data[ 'cr_woo_addional' ] = $_REQUEST[ 'cr_woo_addional' ]; | |
} | |
return $cart_item_data; | |
} | |
add_filter( 'woocommerce_add_cart_item_data', 'cr_woo_additional_services_selection', 100, 3 ); | |
if ( ! function_exists( 'cr_woo_additional_services_cart_item_name' ) ) { | |
/** | |
* Изменение тайтла в корзине | |
*/ | |
function cr_woo_additional_services_cart_item_name( $string, $order_item, $key ) { | |
if ( ! empty( $order_item[ 'cr_woo_addional' ] ) ) { | |
wp_enqueue_script( 'cr-woo-additional-services' ); | |
wp_enqueue_style( 'cr-woo-additional-services' ); | |
$string .= '<div class="cr-wp-hr"></div><i class="cr-icon wp-pandacut" aria-hidden="true"></i>Подшив - ' . esc_attr( str_replace( 'см', '', $order_item[ 'cr_woo_addional' ] ) ) . ' см'; | |
} | |
return $string; | |
} | |
} | |
add_filter( 'woocommerce_cart_item_name', 'cr_woo_additional_services_cart_item_name', 10, 3 ); | |
function filter_woocommerce_order_item_name( $item_name, $item, $false ) { | |
if ( ! empty( $item[ 'item_meta' ][ 'Подшив' ] ) ) { | |
wp_enqueue_style( 'cr-woo-additional-services' ); | |
$item_name .= '<div class="cr-wp-hr"></div><i class="cr-icon wp-pandacut" aria-hidden="true"></i>Подшив - ' . esc_attr( str_replace( 'см', '', (int) $item[ 'item_meta' ][ 'Подшив' ][ 0 ] ) ) . ' см'; | |
} | |
return $item_name; | |
} | |
add_filter( 'woocommerce_order_item_name', 'filter_woocommerce_order_item_name', 10, 3 ); | |
if ( ! function_exists( 'cr_woo_additional_services_cart_item_quantity' ) ) { | |
/** | |
* Изменение количкства в корзине | |
* | |
* @param $product_quantity | |
* @param $cart_item_key | |
* @param $cart_item | |
* | |
* @return string | |
*/ | |
error_reporting( E_ERROR ); | |
function cr_woo_additional_services_cart_item_quantity( $product_quantity, $cart_item_key, $cart_item ) { | |
if ( ! empty( $cart_item[ 'cr_woo_addional' ] ) ) { | |
$product_quantity .= '<div class="cr-wp-hr"></div>' . $cart_item [ 'quantity' ]; | |
} | |
return $product_quantity; | |
} | |
add_filter( 'woocommerce_cart_item_quantity', 'cr_woo_additional_services_cart_item_quantity', 10, 3 ); | |
} | |
if ( ! function_exists( 'cr_woo_additional_services_item_price' ) ) { | |
/** | |
* Изменение цены в корзине | |
* | |
* @param $string | |
* @param $order_item | |
* @param $key | |
* | |
* @return string | |
*/ | |
function cr_woo_additional_services_item_price( $string, $order_item, $key ) { | |
if ( ! empty( $order_item[ 'cr_woo_addional' ] ) ) { | |
$prise = get_post_meta( $order_item[ 'product_id' ], '_cr_woo_additional', true ); | |
$string .= '<div class="cr-wp-hr"></div>' . wc_price( (int) $prise ); | |
} | |
return $string; | |
} | |
add_filter( 'woocommerce_cart_item_price', 'cr_woo_additional_services_item_price', 10, 3 ); | |
} | |
/** | |
* Изменение подитога товара | |
* | |
* @param $subtotal | |
* @param $cart_item | |
* @param $cart_item_key | |
* | |
* @return string | |
*/ | |
function cr_woo_additional_services_cart_item_subtotal( $subtotal, $cart_item, $cart_item_key ) { | |
if ( ! empty( $cart_item[ 'cr_woo_addional' ] ) ) { | |
$prise = get_post_meta( $cart_item[ 'product_id' ], '_cr_woo_additional', true ); | |
$newsubtotal = $subtotal . '<div class="cr-wp-hr"></div>' . wc_price( $cart_item[ 'quantity' ] * (int) $prise ); | |
} else { | |
$newsubtotal = $subtotal; | |
} | |
return sprintf( '%s', $newsubtotal ); | |
} | |
add_filter( 'woocommerce_cart_item_subtotal', 'cr_woo_additional_services_cart_item_subtotal', 99, 3 ); | |
if ( ! function_exists( 'woocommerce_custom_surcharge' ) ) { | |
/** | |
* Подшив | |
*/ | |
function cr_woo_additional_services_surcharge() { | |
global $woocommerce; | |
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { | |
return; | |
} | |
$newsubtotal = array(); | |
foreach ( $woocommerce->cart->cart_contents as $cart_item ) { | |
if ( ! empty( $cart_item[ 'cr_woo_addional' ] ) ) { | |
$prise = get_post_meta( $cart_item[ 'product_id' ], '_cr_woo_additional', true ); | |
$newsubtotal[] = ( $cart_item[ 'quantity' ] * (int) $prise ); | |
} | |
} | |
$surcharge = array_sum( $newsubtotal ); | |
if ( ! empty( $surcharge ) ) { | |
$woocommerce->cart->add_fee( 'Подшив', $surcharge, true, '' ); | |
} | |
} | |
add_action( 'woocommerce_cart_calculate_fees', 'cr_woo_additional_services_surcharge' ); | |
} | |
add_filter( 'woocommerce_get_cart_item_from_session', 'cr_woo_additional_services_get_cart_items_from_session', 1, 3 ); | |
/** | |
* Передача подшива из корзины в ордер сессию | |
*/ | |
if ( ! function_exists( 'cr_woo_additional_services_get_cart_items_from_session' ) ) { | |
function cr_woo_additional_services_get_cart_items_from_session( $item, $values, $key ) { | |
if ( ! empty( $values[ 'cr_woo_addional' ] ) ) { | |
$item[ 'Подшив' ] = $values[ 'cr_woo_addional' ]; | |
} | |
return $item; | |
} | |
} | |
add_action( 'woocommerce_add_order_item_meta', 'wdm_add_values_to_order_item_meta', 1, 2 ); | |
if ( ! function_exists( 'wdm_add_values_to_order_item_meta' ) ) { | |
/** | |
* Передача подшива в мета ордера | |
* | |
* @param $item_id | |
* @param $values | |
*/ | |
function wdm_add_values_to_order_item_meta( $item_id, $values ) { | |
global $woocommerce, $wpdb; | |
$user_custom_values = $values[ 'Подшив' ]; | |
if ( ! empty( $user_custom_values ) ) { | |
wc_add_order_item_meta( $item_id, 'Подшив', $user_custom_values ); | |
} | |
} | |
} | |
/**Убираем czrjt*/ | |
function filter_woocommerce_order_items_meta_display( $output, $instance ) { | |
$pos = strripos( $output, 'Подшив' ); | |
if ( $pos ) { | |
return ''; | |
} else { | |
return $output; | |
} | |
} | |
// add the filter | |
add_filter( 'woocommerce_order_items_meta_display', 'filter_woocommerce_order_items_meta_display', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment