WordPress商品详情页新增Buy on amazonebay跳转按钮

[TOC]

提示:这个亚马逊商品链接跳转不是用插件编辑,而是在woocommerce插件源码中新增。新增后,woocommerce插件不要升级,或者非要升级那就再添加一次。(之所以开发这个,因为插件要几十美金。自己又刚弄WordPress插件开发不会,只有动代码了。大家要在弄之前先备份好文件,或者在本地先试。不敢改代码的可以到https://download.csdn.net/download/qq_39801208/11501934,这是我后面弄的插件形式,可以私聊我。也不错)

一、后台商品编辑页面新增字段

找到

wp-content\plugins\woocommerce\includes\admin\meta-boxes\views\ html-product-data-general.php

大概第52行,在

里面新增

 woocommerce_wp_text_input(
    array(
            'id'          => '_amazonurl',
            'value'       => $product_object->get_amazonurl( 'edit' ),
            'data_type'   => 'amazonurl',
            'placeholder' => 'http://',
            'label'       => __( '亚马逊商品链接', 'woocommerce' ) ,
            'description' => __( 'Enter the external URL to the product.', 'woocommerce' ),
            )
        );  

二、后台商品的数据保存

1.在 wp-content\plugins\woocommerce\includes\data-stores\class-wc-product-data-store-cpt.php

大概在第31 行 protected $internal_meta_keys = array( )里 新增

'_amazonurl',   

大概第331行 protected function read_product_data( &$product ) {}里新增 (根据不同版本填写)

'amazonurl'=> get_post_meta( $id, '_amazonurl', true ), //或者'_amazonurl'=>'amazonurl',          

大概 第501行 在protected function update_post_meta( &$product, $force = false ) { }里面新增

'_amazonurl' => 'amazonurl',        

2.在 wp-content\plugins\woocommerce\includes\admin\meta-boxes\class-wc-meta-box-product-data.php

大概第362行 $errors = $product->set_props()里新增

'amazonurl' => esc_url_raw( wp_unslash( $_POST['_amazonurl'] ) ),  

3.在 wp-content\plugins\woocommerce\includes\abstract\abstract-wc-product.php

大概第68行 protected $data = array()里 增加

'amazonurl' => '',

大概第296行 public function get_sale_price( $context = ‘view’ ) {}下面新增

public function get_amazonurl( $context = 'view' ) {    
    return $this->get_prop( 'amazonurl', $context );
}

大概第843行 public function set_sale_price( $price ) {}下面新增

public function set_amazonurl( $amazonurl ) {           
    $this->set_prop( 'amazonurl', htmlspecialchars_decode( $amazonurl ) );  
}

后台实现效果

三、前端商品页面的实现

1.找到 wp-content\plugins\woocommerce\includes\abstract\abstract-wc-product.php

大概第 1754行 public function get_price_html( $deprecated = ‘’ ) {}下面新增

public function get_amazonurl_html(){       
    return apply_filters( 
        'woocommerce_product_amazonurl', $this->get_amazonurl(), $this );
    }

大概第1820行 public function single_add_to_cart_text() {}下面新增

public function single_amazonurl() {    
    return apply_filters( 'woocommerce_product_single_amazonurl', __( 'Buy on Amazon', 'woocommerce' ), $this );
}

2.在wp-content\plugins\woocommerce\templates\single-product 新增amazonurl.php

<?php
/**
 * Single Product amazonurl
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product/amazonurl.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see     https://docs.woocommerce.com/document/template-structure/
 * @author  WooThemes
 * @package WooCommerce/Templates
 * @version 3.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

global $product;

?>
<style>
    .amazon{
        clear:both;display:blcok;width:100%;height:45px;line-height:45px;background-color:red;text-align:center;margin-bottom:10px;border-radius:5px;font-weight:bold;
         cursor:pointer;
    } 
    .amazon a{

        color:#fff;
    }
    .amazon:hover{

        opacity:0.9;
    }
    .amazon a:hover{
        background-color:red;
        color:#fff;
        border-radius:5px;
        cursor:pointer;

    }
</style>
<?php 
        $b=$product->get_amazonurl_html();
        $a=$product->single_amazonurl();  if($b!==''){
            echo "<div class='amazon' ><a style='display:block; color:#fff;width:100%;font-size:14px'  href='$b'        target='blank' rel='nofollow'>$a</a></div>";
            }else{}  ?>

3.在wp-content\plugins\woocommerce\includes\wc-template-functions.php

大概第1433行 if ( ! function_exists( ‘woocommerce_template_single_title’ ) ) {}下面新增

if ( ! function_exists( 'woocommerce_template_single_amazonurl' ) ) {
    function woocommerce_template_single_amazonurl() {
        wc_get_template( 'single-product/amazonurl.php' );
    }
}

4.在wp-content\plugins\woocommerce\includes\wc-template-hooks.php

大概第150行 add_action(‘woocommerce_single_product_summary’,‘woocommerce_template_single_meta’, 40 );下面新增

add_action('woocommerce_single_product_summary','woocommerce_template_single_amazonurl',70);

实现效果如下: