laciikee

FEE

Aug 3rd, 2022
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.26 KB | None | 0 0
  1. <?php
  2. /**
  3.  * YITH_WCACT_Fee_Product Class.
  4.  *
  5.  * @package YITH\Auctions\Includes
  6.  */
  7.  
  8. if ( ! defined( 'ABSPATH' ) ) {
  9.     exit;
  10. }
  11.  
  12. if ( ! class_exists( 'YITH_WCACT_Fee_Product' ) ) {
  13.     /**
  14.      * YITH_WCACT_Fee_Product
  15.      *
  16.      * @since 2.0.0
  17.      */
  18.     class YITH_WCACT_Fee_Product {
  19.  
  20.         /**
  21.          * Single instance of the class
  22.          *
  23.          * @var   \YITH_WCACT_Fee_Product
  24.          * @since 1.0.0
  25.          */
  26.         protected static $instance;
  27.  
  28.         /**
  29.          * Var Auction fee creation
  30.          *
  31.          * @var int The default product for create fee on auction product
  32.          */
  33.         public $auction_product_fee_id = -1;
  34.  
  35.         /**
  36.          * Constructor
  37.          *
  38.          * @since  2.0.0
  39.          * @author Carlos Rodríguez <[email protected]>
  40.          */
  41.         private function __construct() {
  42.             add_action( 'init', array( $this, 'create_fee_auction_product' ) );
  43.  
  44.             add_filter( 'woocommerce_is_purchasable', array( $this, 'auction_is_purchasable' ), 10, 2 );
  45.  
  46.             add_action( 'woocommerce_checkout_create_order_line_item', array( $this, 'update_order_item_data_for_auction_fee' ), 30, 3 );
  47.  
  48.             add_filter( 'woocommerce_cart_item_name', array( $this, 'fee_product_name' ), 10, 3 );
  49.             add_filter( 'woocommerce_order_item_name', array( $this, 'fee_product_name' ), 10, 3 );
  50.             add_filter( 'woocommerce_cart_item_thumbnail', array( $this, 'fee_product_image' ), 10, 3 );
  51.             add_filter( 'woocommerce_admin_order_item_thumbnail', array( $this, 'fee_product_image_admin_order' ), 10, 3 );
  52.  
  53.             add_action( 'woocommerce_order_status_completed', array( $this, 'register_customer_fee' ), 10, 1 );
  54.             add_action( 'woocommerce_order_status_processing', array( $this, 'register_customer_fee' ), 10, 1 );
  55.  
  56.             /**
  57.              * Hide the default product for auction fee on the admin products list
  58.              * */
  59.             add_action( 'pre_get_posts', array( $this, 'hide_default_auction_fee_product' ) );
  60.         }
  61.  
  62.         /**
  63.          * Returns single instance of the class
  64.          *
  65.          * @return \YITH_WCACT_Fee_Product
  66.          * @since  2.0.0
  67.          */
  68.         public static function get_instance() {
  69.             if ( is_null( self::$instance ) ) {
  70.                 self::$instance = new self();
  71.             }
  72.  
  73.             return self::$instance;
  74.         }
  75.  
  76.         /**
  77.          * Create fee auction product
  78.          *
  79.          * @since 2.0.0
  80.          */
  81.         public function create_fee_auction_product() {
  82.             $this->auction_product_fee_id = get_option( 'yith_wcact_fee_auction_product_id', -1 );
  83.  
  84.             if ( -1 === $this->auction_product_fee_id || ! wc_get_product( $this->auction_product_fee_id ) ) {
  85.                 // Create auction fee product.
  86.                 $args = array(
  87.                     'post_title'     => esc_html__( 'Poplatok za aukciu', 'yith-auctions-for-woocommerce' ),
  88.                     'post_name'      => 'yith_wcact_auction_fee_product',
  89.                     'post_content'   => esc_html__( 'This product has been automatically created by the plugin YITH WooCommerce Auction. You must not edit it, or the plugin might not work properly. The main functionality of this product is to be used for the feature "Ask fee payment before placing a bid"', 'yith-auctions-for-woocommerce' ),
  90.                     'post_status'    => 'private',
  91.                     'post_date'      => gmdate( 'Y-m-d H:i:s' ),
  92.                     'post_author'    => 0,
  93.                     'post_type'      => 'product',
  94.                     'comment_status' => 'closed',
  95.                 );
  96.  
  97.                 $this->auction_product_fee_id = wp_insert_post( $args );
  98.  
  99.                 update_option( 'yith_wcact_fee_auction_product_id', $this->auction_product_fee_id );
  100.                 wp_set_object_terms( $this->auction_product_fee_id, 'simple', 'product_type' );
  101.  
  102.                 // set this default gift card product as virtual.
  103.                 $product = wc_get_product( $this->auction_product_fee_id );
  104.  
  105.                 if ( $product ) {
  106.                     yit_save_prop( $product, '_virtual', 'yes' );
  107.                     yit_save_prop( $product, '_visibility', 'hidden' );
  108.                 }
  109.             } else {
  110.                 $product = wc_get_product( $this->auction_product_fee_id );
  111.  
  112.                 if ( $product && 'simple' !== $product->get_type() ) {
  113.                     wp_set_object_terms( $product->get_id(), 'simple', 'product_type' );
  114.                 }
  115.             }
  116.         }
  117.  
  118.         /**
  119.          * Check if it's fee product in order to make it purchasable
  120.          *
  121.          * @param bool       $purchasable is product purchasable.
  122.          * @param WC_Product $product Product.
  123.          * @since 2.0.0
  124.          */
  125.         public function auction_is_purchasable( $purchasable, $product ) {
  126.             if ( ( $product instanceof WC_Product_Simple ) && ( (int) $product->get_id() === (int) $this->auction_product_fee_id ) ) {
  127.                 return true;
  128.             }
  129.  
  130.             return $purchasable;
  131.         }
  132.  
  133.         /**
  134.          *  Add order item for auction fee product
  135.          *
  136.          * @param  WC_PRODUCT $item product item.
  137.          * @param  string     $cart_item_key cart item key.
  138.          * @param  mixed      $values values.
  139.          * @author Carlos Rodríguez <[email protected]>
  140.          * @since  2.0
  141.          */
  142.         public function update_order_item_data_for_auction_fee( $item, $cart_item_key, $values ) {
  143.             if ( isset( $values['yith_wcact_if_fee_product'] ) && $values['yith_wcact_if_fee_product'] ) {
  144.                 $item->add_meta_data( '_ywcact_is_fee', true );
  145.                 $item->add_meta_data( '_ywcact_fee_amount', $values['yith_wcact_fee_value'] );
  146.                 $item->add_meta_data( '_ywcact_auction_id', $values['yith_wcact_auction_id'] );
  147.  
  148.                 do_action( 'yith_wcact_order_item_data', $item, $cart_item_key, $values );
  149.             }
  150.         }
  151.  
  152.         /**
  153.          *  Register customer fee on database
  154.          *
  155.          * @param  int $order_id order id.
  156.          * @author Carlos Rodríguez <[email protected]>
  157.          * @since  2.0
  158.          */
  159.         public function register_customer_fee( $order_id ) {
  160.             $order = wc_get_order( $order_id );
  161.  
  162.             if ( $order ) {
  163.                 $order_items = $order->get_items();
  164.  
  165.                 foreach ( $order_items as $order_item_id => $order_item ) {
  166.                     $is_fee_item = wc_get_order_item_meta( $order_item_id, '_ywcact_is_fee', true );
  167.  
  168.                     if ( ! $is_fee_item ) {
  169.                         continue;
  170.                     }
  171.  
  172.                     $fee_amount = wc_get_order_item_meta( $order_item_id, '_ywcact_fee_amount', true );
  173.                     $auction_id = wc_get_order_item_meta( $order_item_id, '_ywcact_auction_id', true );
  174.                     $user_id    = $order->get_user_id();
  175.                     $date       = gmdate( 'Y-m-d H:i:s' );
  176.                     $db         = YITH_Auctions()->bids;
  177.                     $db->register_fee( $user_id, $auction_id, $date, $order_id, $fee_amount );
  178.                 }
  179.             }
  180.         }
  181.  
  182.         /**
  183.          *  Get Fee product title
  184.          *
  185.          * @param string $product_title The product title HTML.
  186.          * @param array  $cart_item     The cart item array.
  187.          * @param bool   $cart_item_key The cart item key.
  188.          *
  189.          * @since  2.0.0
  190.          * @author Carlos Rodriguez <[email protected]>
  191.          * @return string  The product title HTML
  192.          * @use    woocommerce_cart_item_name hook
  193.          */
  194.         public function fee_product_name( $product_title, $cart_item, $cart_item_key = false ) {
  195.             if ( is_array( $cart_item ) && ! empty( $cart_item['yith_wcact_if_fee_product'] ) && ! empty( $cart_item['yith_wcact_auction_id'] ) ) {
  196.                 $product_id = $cart_item['yith_wcact_auction_id'];
  197.  
  198.                 $product_title = apply_filters( 'yith_wcact_cart_auction_fee_start_message', esc_html__( 'Poplatok za aukciu - ', 'yith-auctions-for-woocommerce' ) ) . wc_get_product( $product_id )->get_name();
  199.             }
  200.  
  201.             return apply_filters( 'yith_wcact_cart_product_title', $product_title, $cart_item, $cart_item_key );
  202.         }
  203.  
  204.         /**
  205.          *    Fee product image on cart page.
  206.          *
  207.          * @param string $product_image The product image HTML.
  208.          * @param array  $cart_item     The cart item array.
  209.          * @param bool   $cart_item_key The cart item key.
  210.          */
  211.         public function fee_product_image( $product_image, $cart_item, $cart_item_key = false ) {
  212.             if ( ! empty( $cart_item['yith_wcact_if_fee_product'] ) && ! empty( $cart_item['yith_wcact_auction_id'] ) ) {
  213.                 $product_id = $cart_item['yith_wcact_auction_id'];
  214.                 $product    = wc_get_product( $product_id );
  215.  
  216.                 if ( $product && 'auction' === $product->get_type() ) {
  217.                     $product_image = $product->get_image( 'thumbnail', array( 'title' => '' ), false );
  218.                 }
  219.             }
  220.  
  221.             return $product_image;
  222.         }
  223.  
  224.         /**
  225.          *    Fee product image on admin order page
  226.          *
  227.          * @param string $product_image The product image HTML.
  228.          * @param int    $item_id     The cart item array.
  229.          * @param bool   $item The cart item key.
  230.          */
  231.         public function fee_product_image_admin_order( $product_image, $item_id, $item ) {
  232.             $auction_id = wc_get_order_item_meta( $item_id, '_ywcact_auction_id', true );
  233.  
  234.             if ( $auction_id ) {
  235.                 $product = wc_get_product( $auction_id );
  236.  
  237.                 if ( $product && 'auction' === $product->get_type() ) {
  238.                     $product_image = $product->get_image( 'thumbnail', array( 'title' => '' ), false );
  239.                 }
  240.             }
  241.  
  242.             return $product_image;
  243.         }
  244.  
  245.         /**
  246.          * Avoid to show the default auction fee product
  247.          *
  248.          * @param array $query The query.
  249.          *
  250.          * @author Carlos Rodríguez
  251.          * @since  2.0.0
  252.          */
  253.         public function hide_default_auction_fee_product( $query ) {
  254.             global $pagenow;
  255.  
  256.             if ( $query->is_admin && 'edit.php' === $pagenow && isset( $_GET['post_type'] ) && 'product' === $_GET['post_type'] && apply_filters( 'yith_wcact_pre_get_posts_hide_default_auction_fee_product', true, $query ) ) { // phpcs:ignore
  257.                 $query->set( 'post__not_in', array( get_option( 'yith_wcact_fee_auction_product_id' ) ) );
  258.             }
  259.         }
  260.     }
  261. }
  262.  
  263. /**
  264.  * Unique access to instance of YITH_WCAF_Premium class
  265.  *
  266.  * @return \YITH_WCACT_Fee_Product
  267.  * @since  2.0.0
  268.  */
  269. function YITH_WCACT_Fee_Product() { //phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  270.     return YITH_WCACT_Fee_Product::get_instance();
  271. }
  272.  
Advertisement
Add Comment
Please, Sign In to add comment