get_type() !== WooCommerceInterface::product_type() ) { return; } $ad_types = AdPostMeta::ad_types(); $adtype = WooCommerceInterface::get_product_setting( $product, 'adtype', '' ); if ( ! isset( $ad_types[ $adtype ] ) ) { return; } printf( '
', esc_html__( 'Ad Type:', 'ad-commander' ), esc_html( $ad_types[ $adtype ] ) ); } /** * Store the ad purchase details on the cart item. * * @param array $cart_item_data The current cart item data. * @param int $product_id The product ID added to the cart. * * @return array */ public function add_cart_item_data( $cart_item_data, $product_id ) { $product = wc_get_product( $product_id ); if ( ! $product || $product->get_type() !== WooCommerceInterface::product_type() ) { return $cart_item_data; } $cart_item_data['adcmdr_ad'] = array( 'adtype' => WooCommerceInterface::get_product_setting( $product, 'adtype', '' ), ); return $cart_item_data; } /** * Display the ad type in the cart and checkout. * * @param array $item_data The current item data to display. * @param array $cart_item The cart item. * * @return array */ public function get_item_data( $item_data, $cart_item ) { if ( ! isset( $cart_item['adcmdr_ad']['adtype'] ) ) { return $item_data; } $ad_types = AdPostMeta::ad_types(); $adtype = $cart_item['adcmdr_ad']['adtype']; if ( isset( $ad_types[ $adtype ] ) ) { $item_data[] = array( 'key' => __( 'Ad Type', 'ad-commander' ), 'value' => $ad_types[ $adtype ], ); } return $item_data; } /** * Record the ad purchase details on the order line item. * * @param \WC_Order_Item_Product $item The order item. * @param string $cart_item_key The cart item key. * @param array $values The cart item values. * * @return void */ public function create_order_line_item( $item, $cart_item_key, $values ) { if ( ! isset( $values['adcmdr_ad'] ) ) { return; } $keys = WooCommerceInterface::order_item_meta_keys(); $data = $values['adcmdr_ad']; $item->add_meta_data( $keys['ad_product'], 1 ); $item->add_meta_data( $keys['adtype'], $data['adtype'] ); /** * Human-readable meta for order screens and emails. */ $ad_types = AdPostMeta::ad_types(); if ( isset( $ad_types[ $data['adtype'] ] ) ) { $item->add_meta_data( __( 'Ad Type', 'ad-commander' ), $ad_types[ $data['adtype'] ] ); } } /** * Hide the machine order item meta keys on admin order screens. * * @param array $hidden The currently hidden meta keys. * * @return array */ public function hidden_order_itemmeta( $hidden ) { return array_merge( $hidden, array_values( WooCommerceInterface::order_item_meta_keys() ) ); } /** * Grant the customer capability when an order containing an ad product reaches an eligible status. * * @param int $order_id The order ID. * @param string $old_status The previous order status. * @param string $new_status The new order status. * * @return void */ public function grant_customer_capability( $order_id, $old_status = '', $new_status = '' ) { if ( ! in_array( $new_status, WooCommerceInterface::ad_order_statuses(), true ) ) { return; } $order = wc_get_order( $order_id ); if ( ! $order ) { return; } $has_ad_item = false; foreach ( $order->get_items() as $item ) { if ( WooCommerceInterface::is_ad_order_item( $item ) ) { $has_ad_item = true; break; } } if ( ! $has_ad_item ) { return; } $user = $order->get_user(); if ( $user && ! $user->has_cap( AdCommander::customer_capability() ) ) { $user->add_cap( AdCommander::customer_capability() ); } } }