Wordpress商品详情页新增Buy on amazon/ebay跳转按钮

Wordpress商品详情页新增Buy on amazon/ebay跳转按钮

提示:本文内容严禁在其他平台转载!劳动成果不易,希望能相互体谅。这个亚马逊商品链接跳转不是用插件编辑,而是在woocommerce插件源码中新增。(大家要在弄之前先备份好文件,或者在本地先试。不敢改代码的可以找我要写好的插件版不影响源码,严禁将本插件在其他网上转卖!可以根据实际主题页面定制按钮,加Q:1023652334)

注意:根据文章改代码的,woocommerce插件不要升级,或者非要升级那就再添加一次。插件的不会影响woocommerce的升级

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

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

大概第52行

<div class="options_group pricing show_if_simple show_if_external hidden"></div>里面新增
 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);

实现效果如下:
在这里插入图片描述

分享不易,多多支持!
在这里插入图片描述
在这里插入图片描述

  • 12
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值