woocommerce各种核心相关功能,增加自定义字段,可前台调用直接跳转至电商

需求是不显示价格,要能跳转到自己的商城,于是只能是在woocommerce后台增加商城链接的自定义字段并且在前台调用。
但是考虑产品过多,后期运营每个填太麻烦,于是又增加了链接字段空的时候,点击链接默认跳转至搜索产品slug,特别需要填链接的再手动填。

<?php

// //禁用产品数据部分
// function remove_tab($tabs){
//     unset($tabs['inventory']); // it is to remove inventory tab
//     //unset($tabs['advanced']); // it is to remove advanced tab
//     //unset($tabs['linked_product']); // it is to remove linked_product tab
//     //unset($tabs['attribute']); // it is to remove attribute tab
//     unset($tabs['variations']); // it is to remove variations tab
//     return($tabs);
// }
// add_filter('woocommerce_product_data_tabs', 'remove_tab', 10, 1);



//用户购买时,填写表单字段修改
add_filter( 'woocommerce_checkout_fields', 'zke_remove_fields', 9999 );
function zke_remove_fields( $woo_checkout_fields_array ) {

// 	unset( $woo_checkout_fields_array['billing']['billing_first_name'] );
    unset( $woo_checkout_fields_array['billing']['billing_last_name'] );
// 	unset( $woo_checkout_fields_array['billing']['billing_phone'] );
// 	unset( $woo_checkout_fields_array['billing']['billing_email'] );
// 	unset( $woo_checkout_fields_array['order']['order_comments'] ); // remove order notes
    unset( $woo_checkout_fields_array['billing']['billing_company'] ); // remove company field
    unset( $woo_checkout_fields_array['billing']['billing_country'] );
// 	unset( $woo_checkout_fields_array['billing']['billing_address_1'] );
// 	unset( $woo_checkout_fields_array['billing']['billing_address_2'] );
    unset( $woo_checkout_fields_array['billing']['billing_city'] );
// 	unset( $woo_checkout_fields_array['billing']['billing_state'] ); // remove state field
// 	unset( $woo_checkout_fields_array['billing']['billing_postcode'] ); // remove zip code field

    
    return $woo_checkout_fields_array;
}

// 购买表单必填项修改,ture为必填,false为可选
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    $fields['billing']['billing_first_name']['required'] = true;
    $fields['billing']['billing_last_name']['required'] = true;
    $fields['billing']['billing_country']['required'] = false;
    $fields['billing']['billing_address_1']['required'] = true;
    $fields['billing']['billing_address_2']['required'] = true;
    $fields['billing']['billing_city']['required'] = true;
    $fields['billing']['billing_postcode']['required'] = false;
    $fields['billing']['billing_email']['required'] = false;
//	$fields['billing']['billing_country']['required'] = false;
//	$fields['billing']['billing_state']['required'] = false;
    return $fields;
}




// 删除商城列表展示标题下方价格和评级
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
remove_action( 'woocommerce_after_shop_loop_item_rating', 'woocommerce_template_loop_rating', 10 );

//删除商品列表页排序
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );

//删除面包屑
// remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );


// 商品列表展示标题下方显示描述前10个字符
function output_product_excerpt() { 
    global $post; 
    // $excerpt = $post->post_excerpt; 
    $excerpt = get_post_excerpt();
    $excerpt9 = mb_substr($excerpt,0,34,"utf-8");//wordpress内置方法$post->post_excerpt和主题不兼容,使用wpjam中的内置函数get_post_excerpt()获取摘要
    if ($excerpt != "..."){
        echo '<div class="zke-wooexcerpt">'. $excerpt9 . '……</div>';
    }else{
        echo '<div class="zke-wooexcerpt" >点击了解更多>></div>';
    }
} 
add_action('woocommerce_after_shop_loop_item_title', 'output_product_excerpt', 35); 


//取消购买
add_filter( 'woocommerce_is_purchasable', '__return_false');
remove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10); 
remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20);



//取消价格
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );


//增加产品外部购买链接
class ProductMeta
{

    public function __construct()
    {
        add_filter('woocommerce_product_data_tabs', function ($tabs)
            {
                $tabs[ 'tchd_links_info' ] = [
                    'label'    => __('商城链接', 'txtdomain'),
                    'target'   => 'tchd_product_links',
                    'class'    => ['hide_if_external'],
                    'priority' => 25,
                ];
            
                return $tabs;
            });
        if (is_admin()) {
            add_action('woocommerce_product_data_panels', [$this, 'add_fields']);
            add_action('woocommerce_process_product_meta', [$this, 'save_fields']);
        }
        add_action('woocommerce_product_meta_start', [$this, 'show_fields']);
    }

    public function add_fields()
    { 
    ?>
   
    <div id="tchd_product_links" class="panel woocommerce_options_panel hidden"><?php
            echo '<div class="description" ><small style="color:red;" >zk提示:前端商品页跳转至所填链接,不填写直接跳转至各店铺首页</small></div>';
            woocommerce_wp_text_input([
            'id'            => 'jd_product_link',
            'label'         => __('京东产品链接地址', 'txtdomain'),
            'wrapper_class' => '_product_url_field ',
            'placeholder' => 'https://',
            'class' => 'short',
            'description' => '输入京东旗舰店产品链接',
        ]);
        
            woocommerce_wp_text_input([
            'id'            => 'tm1_product_link',
            'label'         => __('北京天猫产品链接', 'txtdomain'),
            'wrapper_class' => ' _product_url_field ',
            'placeholder' => 'https://',
            'class' => 'short',
            'description' => '输入北京天猫店铺产品链接',
        ]);
        
            woocommerce_wp_text_input([
                'id'            => 'tm2_product_link',
                'label'         => __('上海天猫产品链接', 'txtdomain'),
                'wrapper_class' => ' _product_url_field ',
                'placeholder' => 'https://',
                'class' => 'short',
                'description' => '输入上海天猫旗舰店产品链接',
            ]);
    ?></div><?php
    }


    public function save_fields($post_id)
    {
        $product     = wc_get_product($post_id);
        $jd_product_link = isset($_POST[ 'jd_product_link' ]) ? $_POST[ 'jd_product_link' ] : '';
        $tm1_product_link = isset($_POST[ 'tm1_product_link' ]) ? $_POST[ 'tm1_product_link' ] : '';
        $tm2_product_link = isset($_POST[ 'tm2_product_link' ]) ? $_POST[ 'tm2_product_link' ] : '';
        $product->update_meta_data('jd_product_link', sanitize_text_field($jd_product_link));
        $product->update_meta_data('tm1_product_link', sanitize_text_field($tm1_product_link));
        $product->update_meta_data('tm2_product_link', sanitize_text_field($tm2_product_link));
        $product->save();
    }

    public function show_fields()
    {
        global $post;
        $product = wc_get_product($post->ID);
        $keywords = str_replace("-","+",$product->get_slug());//替换wp中slug中自动生成的-号
        $jd_product_link = $product->get_meta('jd_product_link') ? $product->get_meta('jd_product_link'):'https://mall.jd.com/view_search-423249-134809-130449-0-0-0-0-1-1-60.html?keyword='.$keywords;
        $tm1_product_link = $product->get_meta('tm1_product_link') ? $product->get_meta('tm1_product_link'):'https://tianchuanghengdabj.tmall.com/category.htm?orderType=&viewType=grid&keyword='.$keywords.'&lowPrice=&highPrice=';
        $tm2_product_link = $product->get_meta('tm2_product_link') ? $product->get_meta('tm2_product_link'):'https://tianchuanghengda.tmall.com/?search=y&searcy_type=item&q='.$keywords;
        
        
        echo '
        <style>
         .buying_links .tchd_links{display:inline-block;height:32px;width:140px;margin:0 14px 12px 0}
         .tchd_links a{display:flex;font-size:21px;align-items:center;justify-content:center}
         .buying_links .tchd_links:hover{box-shadow:0 17px 50px 0 rgba(0,0,0,.19);transform:translate3d(0,-5px,0);transition:all 1s cubic-bezier(.5,0,.1,1)}
         .icon{width:1em;height:1em;vertical-align:-0.15em;fill:currentColor;overflow:hidden}
        </style>
        <script type="text/javascript">
        jQuery(document).ready(function(){
            jQuery("#zke_bdzixun").click(function(event) {
                if (jQuery(\'#nb_invite_ok\').length > 0) {
                jQuery(\'#nb_invite_ok\').click();
                }
            });
            jQuery("#zixun_tel").hover(function(){
                    jQuery("#zixun_tel").css("background-color","#1E90FF");
                    jQuery("#zke_bdzixun").css("color","white");
                },function(){
                    jQuery("#zixun_tel").css("background-color","#F0F0F0");
                    jQuery("#zke_bdzixun").css("color","#1d1d1f");
              });
        });
        </script>
        <hr style="height: 1px;margin-top: 7px;margin-bottom:14px; border: none; border-top: 1px solid #d3ced2;" /><div id="zixun_tel" style="font-size:14px;font-weight:300;margin-bottom: 4px;background-color: #F0F0F0;border-radius: 12px;color: #1d1d1f;display: inline-block;padding:4px 9px;"><a id="zke_bdzixun" href="javascript:void(0);" style="color:#1d1d1f;"><svg class="icon" style="margin-right:2px;"  aria-hidden="true"><use xlink:href="#icon-shouye"></use></svg>采购请咨询:400-800-1650</a></div><div class="buying_links" style="height:124px;margin-bottom:14px;">
        ';?>
        
        <div class="tchd_links" style="background-color:#d42415;" ><a href="<?php echo $jd_product_link; ?>" style="color: #fff;"  target="_blank" rel="nofollow"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-sr_jingdong"></use></svg>京东旗舰店</a></div>
        
        <div class="tchd_links" style="background-color:#ff5811;" ><a href=" <?php echo $tm1_product_link; ?>" style="color: #fff;"  target="_blank" rel="nofollow"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-tianmaot"></use></svg>天猫专卖店</a></div>
        
        <div class="tchd_links" style="background-color:#FF0036;" ><a href="<?php echo $tm2_product_link;?>" style="color: #fff;"  target="_blank" rel="nofollow"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-tianmaot"></use></svg>天猫旗舰店</a></div>
        <?php echo '</div>';
        

    }
}

new ProductMeta;


//woocommerce的倒序功能
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
 
function custom_woocommerce_get_catalog_ordering_args( $args ) {
    $orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    if ( 'oldest_to_recent' == $orderby_value ) {
        $args['orderby'] = 'date';
        $args['order'] = 'ASC';
    }
    return $args;
}
 
function custom_woocommerce_catalog_orderby( $sortby ) {
    $sortby['oldest_to_recent'] = __( '产品从旧到新排序', 'woocommerce' );
    return $sortby;
}

// Change "You may also like" into "搭配这些更好用"
add_filter('gettext', 'translate_like');
add_filter('ngettext', 'translate_like');
function translate_like($translated) {
    $translated = str_ireplace('您可能还会喜欢', '搭配这些更好用', $translated);
    return $translated;
}

 

本文作者:𝙕𝙆𝘾𝙊𝙄

文章名称:woocommerce各种核心相关功能,增加自定义字段,可前台调用直接跳转至电商

文章链接:https://www.zkcoi.com/365up/program/2347.html

本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
𝙕𝙆𝘾𝙊𝙄𝙕𝙆𝘾𝙊𝙄
上一篇 2022年10月19日
下一篇 2022年10月19日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

私聊博主

立即联系
一般有空就回复

qrcode_web

微信扫码联系我

分享本页
返回顶部