Advertisement
palsushobhan

show-vendor-coupon-on-single-product-page

Aug 29th, 2022
1,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.41 KB | None | 0 0
  1. add_action('woocommerce_single_product_summary', function() {
  2.     echo do_shortcode('[coupon_applicable_on_product]');
  3. });
  4.  
  5. add_shortcode('coupon_applicable_on_product', function($attr) {
  6.     global $WCFM, $post;
  7.     if( !apply_filters( 'wcfm_is_pref_store_coupons', true ) ) return;
  8.     if ( !is_product() || !function_exists( 'wc_coupons_enabled' ) || ( function_exists( 'wc_coupons_enabled' ) && !wc_coupons_enabled() ) ) return;
  9.     $store_id = $post->post_author;
  10.     if( !$store_id ) return;
  11.     $is_store_offline = get_user_meta( $store_id, '_wcfm_store_offline', true );
  12.     if ( $is_store_offline ) return;
  13.     $is_disable_vendor = get_user_meta( $store_id, '_disable_vendor', true );
  14.     if ( $is_disable_vendor ) return;
  15.     if( !$WCFM->wcfm_vendor_support->wcfm_vendor_has_capability( $store_id, 'manage_coupons' ) ) return;
  16.     $coupon_args = array(
  17.         'posts_per_page'   => -1,
  18.         'offset'           => 0,
  19.         'category'         => '',
  20.         'category_name'    => '',
  21.         'orderby'          => 'date',
  22.         'order'            => 'DESC',
  23.         'include'          => '',
  24.         'exclude'          => '',
  25.         //'meta_key'         => 'show_on_store',
  26.         //'meta_value'       => 'yes',
  27.         'post_type'        => 'shop_coupon',
  28.         'post_mime_type'   => '',
  29.         'post_parent'      => '',
  30.         'author'           => $store_id,
  31.         'post_status'      => array('publish'),
  32.         'suppress_filters' => 0
  33.     );
  34.     $wcfm_store_coupons = get_posts( $coupon_args );
  35.     if( empty( $wcfm_store_coupons ) ) return;
  36.     $content = '';
  37.     foreach( $wcfm_store_coupons as $wcfm_store_coupon ) {
  38.         $wc_coupon = new WC_Coupon( $wcfm_store_coupon->ID );
  39.         if ( $wc_coupon->get_date_expires() && ( current_time( 'timestamp', true ) > $wc_coupon->get_date_expires()->getTimestamp() ) ) continue;
  40.         $product_ids = explode(',', get_post_meta( $wcfm_store_coupon->ID, 'product_ids', true));
  41.         $excluded_product_ids = explode(',', get_post_meta( $wcfm_store_coupon->ID, 'exclude_product_ids', true));
  42.         if( !empty($excluded_product_ids) && in_array($post->ID, $excluded_product_ids) ) continue;
  43.         if( !empty($product_ids) && !in_array($post->ID, $product_ids) ) continue;
  44.         $free_shipping = ( get_post_meta( $wcfm_store_coupon->ID, 'free_shipping', true) == 'yes' ) ? 'enable' : '';
  45.         if( $free_shipping ) {
  46.             $content .= '<span class="wcfmmp-store-coupon-single tips text_tip" data-tip="' . __( 'FREE Shipping Coupon', 'wc-multivendor-marketplace' ) . ($wc_coupon->get_date_expires() ? '<br>' . __( 'Expiry Date: ', 'wc-multivendor-marketplace' ) . $wc_coupon->get_date_expires()->date_i18n( 'F j, Y' ) : '' ) . '<br>' . $wcfm_store_coupon->post_excerpt . '">' . $wcfm_store_coupon->post_title . '</span>';
  47.         } else {
  48.             $content .= '<span class="wcfmmp-store-coupon-single tips text_tip" data-tip="' . esc_html( wc_get_coupon_type( $wc_coupon->get_discount_type() ) ) . ': ' . esc_html( wc_format_localized_price( $wc_coupon->get_amount() ) ) . ($wc_coupon->get_date_expires() ? '<br>' . __( 'Expiry Date: ', 'wc-multivendor-marketplace' ) . $wc_coupon->get_date_expires()->date_i18n( 'F j, Y' ) : '' ) . '<br>' . $wcfm_store_coupon->post_excerpt . '">' . $wcfm_store_coupon->post_title . '</span>';
  49.         }
  50.     }
  51.     if($content) {
  52.         echo '<div class="wcfmmp_store_coupons">' . $content . '</div>';
  53.     }
  54. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement