_x( "Discount Manager", "Discount Manager" ), 'name' => _x( "Discount Manager", "Discount Manager" ), 'menu_name' => 'Discount Manager', 'add_new' => _x( "Add New Entry", "Discount Manager" ), 'add_new_item' => __( "Add New" ), 'edit_item' => __( "Edit Entry" ), 'new_item' => __( "New Entry" ), 'view_item' => __( "View Entry" ), 'search_items' => __( "Search Entries" ), 'not_found' => __( "No Entries Found" ), 'not_found_in_trash' => __( "No Entries Found in Trash" ), 'parent_item_colon' => '' ); // Register post type register_post_type( 'sup_discount_manager', array( 'labels' => $labels, 'public' => true, 'menu_position' => 100, 'has_archive' => false, 'hierarchical' => false, 'publicly_queryable' => false, 'menu_icon' => plugins_url( 'icon.png', __FILE__ ), 'capabilities' => array( 'edit_post' => 'update_core', 'read_post' => 'update_core', 'delete_post' => 'update_core', 'edit_posts' => 'update_core', 'edit_others_posts' => 'update_core', 'delete_posts' => 'update_core', 'publish_posts' => 'update_core', 'read_private_posts' => 'update_core' ), 'rewrite' => array( 'slug' => '', 'with_front' => false ), 'supports' => array( 'title' ) ) ); } add_action( 'init', 'supadu_create_discount_manager', 0 ); if( function_exists( 'acf_add_local_field_group' ) ): acf_add_local_field_group( array( 'key' => 'group_578769945f83a', 'title' => 'Discount Manager', 'fields' => array( array( 'key' => 'field_57876cd5fdbed', 'label' => 'Discount Scope', 'name' => 'discount_manager_scope', 'type' => 'select', 'instructions' => '', 'required' => 1, 'conditional_logic' => 0, 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), 'choices' => array( 'All' => 'All', 'ISBN List' => 'ISBN List', ), 'default_value' => array(), 'allow_null' => 0, 'multiple' => 0, 'ui' => 0, 'ajax' => 0, 'return_format' => 'value', 'placeholder' => '', ), array( 'key' => 'field_57f798e16b2d6', 'label' => 'ISBNs', 'name' => 'discount_manager_isbns', 'type' => 'text', 'instructions' => 'Please enter a comma separated ISBN list', 'required' => 0, 'conditional_logic' => array( array( array( 'field' => 'field_57876cd5fdbed', 'operator' => '==', 'value' => 'ISBN List', ), ), ), 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), 'default_value' => '', 'placeholder' => '', 'prepend' => '', 'append' => '', 'maxlength' => '', ), array( 'key' => 'field_590841f7ffe4c', 'label' => 'Discount Parameter', 'name' => 'discount_manager_parameter', 'type' => 'text', 'instructions' => '', 'required' => 1, 'conditional_logic' => 0, 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), 'default_value' => '', 'placeholder' => '', 'prepend' => '', 'append' => '', 'maxlength' => '', ), array( 'key' => 'field_590843924682e', 'label' => 'Start Date', 'name' => 'discount_manager_startdate', 'type' => 'date_picker', 'instructions' => '', 'required' => 0, 'conditional_logic' => 0, 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), 'display_format' => 'd/m/Y', 'return_format' => 'd/m/Y', 'first_day' => 1, ), array( 'key' => 'field_59b16a0ffed76', 'label' => 'End Date', 'name' => 'discount_manager_enddate', 'type' => 'date_picker', 'instructions' => '', 'required' => 0, 'conditional_logic' => 0, 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), 'display_format' => 'd/m/Y', 'return_format' => 'd/m/Y', 'first_day' => 1, ), array( 'key' => 'field_590844aee4f69', 'label' => 'Currency', 'name' => 'discount_manager_currency', 'type' => 'select', 'instructions' => '', 'required' => 1, 'conditional_logic' => 0, 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), 'choices' => array( 'All' => 'All', 'GBP' => 'GBP', 'USD' => 'USD', ), 'default_value' => array(), 'allow_null' => 0, 'multiple' => 0, 'ui' => 0, 'ajax' => 0, 'return_format' => 'value', 'placeholder' => '', ), array( 'key' => 'field_59b2751e77dd5', 'label' => 'Discount Type', 'name' => 'discount_manager_type', 'type' => 'select', 'instructions' => '', 'required' => 1, 'conditional_logic' => 0, 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), 'choices' => array( 'Percentage' => 'Percentage', 'Flat' => 'Flat', 'Other' => 'Other', ), 'default_value' => array(), 'allow_null' => 0, 'multiple' => 0, 'ui' => 0, 'ajax' => 0, 'return_format' => 'value', 'placeholder' => '', ), array( 'key' => 'field_59b275d677dd6', 'label' => 'Discount Value', 'name' => 'discount_manager_value', 'type' => 'number', 'instructions' => 'Please enter a number', 'required' => 1, 'conditional_logic' => 0, 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), 'default_value' => '', 'placeholder' => '', 'prepend' => '', 'append' => '', 'min' => '', 'max' => '', 'step' => '', ), ), 'location' => array( array( array( 'param' => 'post_type', 'operator' => '==', 'value' => 'sup_discount_manager', ), ), ), 'menu_order' => 0, 'position' => 'normal', 'style' => 'default', 'label_placement' => 'top', 'instruction_placement' => 'label', 'hide_on_screen' => '', 'active' => 1, 'description' => '', ) ); endif; /** * @return array */ function supadu_discount_manager_get_discounts() { $discounts = array(); //AT - this needs to be changed to only get discounts that are active or relevant to the book // [3:56 PM] David Kane: you should be doing a WP Query on the meta data for any discounts with the isbn && and discounts with "all" // [3:56 PM] David Kane: or something // [3:56 PM] David Kane: and in date range yes $args = array( 'post_type' => 'sup_discount_manager', ); $my_query = new WP_Query( $args ); if( isset( $my_query->posts ) && count( $my_query->posts ) > 0 ) { foreach( $my_query->posts as $post ) { $tmp = get_post_meta( $post->ID ); foreach( $tmp as $key => $value ) { $tmp[$key] = reset( $value ); } $tmp['discount_manager_title'] = $post->post_title; $postDate = strtotime( $post->post_modified ); $discounts[$postDate] = $tmp; } } return $discounts; } /** * @param $supapress * @param $discounts * @param $returnObject * * @return string */ function supadu_discount_manager_get_discount($supapress, $discounts, $returnObject = false) { $myDiscounts = array(); $myDiscount = ''; if( isset( $supapress ) && !empty( $discounts ) ) { foreach( $discounts as $dateModified => $discount ) { $inDateRange = supadu_discount_manager_date_in_range( $discount['discount_manager_startdate'], $discount['discount_manager_enddate'] ); $inCurrency = supadu_discount_manager_in_currency( $supapress->currency, $discount['discount_manager_currency'] ); $price = supadu_discount_manager_get_price( $supapress->prices, $supapress->currency ); $discountAmount = supadu_discount_manager_real_amount( $price, $discount ); if( $discount['discount_manager_scope'] == 'All' ) { if( $inDateRange && $inCurrency ) { $myDiscounts[$discountAmount] = $discount; } } else { $isbns = explode( ',', $discount['discount_manager_isbns'] ); $inISBNs = !empty( $isbns ) && in_array( $supapress->isbn, $isbns ); if( $inDateRange && $inCurrency && $inISBNs ) { $myDiscounts[$discountAmount] = $discount; } } } } if( !empty( $myDiscounts ) ) { krsort( $myDiscounts ); $myDiscount = reset( $myDiscounts ); if( $returnObject ) { return $myDiscount; } $myDiscount = $myDiscount['discount_manager_parameter']; } return $returnObject ? array() : $myDiscount; } /** * @param $startDate * @param $endDate * * @return bool */ function supadu_discount_manager_date_in_range($startDate, $endDate) { $return = true; $today = date( 'Ymd' ); if( isset( $startDate ) && $startDate !== '' ) { $return = $return && $startDate <= $today; } if( isset( $endDate ) && $endDate !== '' ) { $return = $return && $endDate >= $today; } return $return; } /** * @param $currency * @param $discountCurrency * * @return bool */ function supadu_discount_manager_in_currency($currency, $discountCurrency) { return $currency === $discountCurrency || $discountCurrency === 'All'; } /** * @param $price * @param $discount * * @return string */ function supadu_discount_manager_real_amount($price, $discount) { if( $discount['discount_manager_type'] === 'Flat' ) { $return = number_format( $discount['discount_manager_value'], 2 ); } elseif( $discount['discount_manager_type'] === 'Percentage' ) { $return = number_format( ( $discount['discount_manager_value'] * $price ) / 100, 2 ); } else { $return = false; } if( $return !== false ) { $return = number_format($price - $return,2); } else { $return = null; } return $return; } /** * @param $prices * @param $currency * * @return mixed */ function supadu_discount_manager_get_price($prices, $currency) { $price = false; if( !empty( $prices ) && $currency !== '' ) { foreach( $prices as $value ) { if( $value->locale === $currency ) { $price = $value->amount; break; } } } return $price; } Owen Hatherley introduces 'THE CHAPLIN MACHINE' - Pluto Press
Pluto Press Logo

Books

Authors

Events

On the Blog

sdqsdqsd