我已经找到了functions.php的这段代码,这使我步入了自己想要的位置。
我在这里使用的代码Adding affiliate links to Woocommerce variations
[Website,有附加代码的地方
但是
它将删除我的下拉变体选择和可变产品描述,并将所有可变产品数据放置在表格中。
我希望能够为每个变体添加多个会员零售商按钮(带有自定义文本),而不仅仅是一个。
所有帮助深表感谢。我不擅长编写代码,因此恐怕我需要任何建议。谢谢。
// Display Fields
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );
//JS to add fields for new variations
add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
// Save Fields
add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 2 );
function variable_fields( $loop, $variation_data ) {
?>
<tr>
<td>
<div>
<label><?php _e( 'Affiliate URL', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_affiliate_url[<?php echo $loop; ?>]" value="<?php echo $variation_data['_my_affiliate_url'][0]; ?>"/>
</div>
</td>
</tr>
<?php
}
function variable_fields_js() {
?>
<tr>
<td>
<div>
<label><?php _e( 'My Custom Field', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_affiliate_url[' + loop + ']" />
</div>
</td>
</tr>
<?php
}
function variable_fields_process( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) :
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
$variable_custom_field = $_POST['my_affiliate_url'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $variable_custom_field[$i] ) ) {
update_post_meta( $variation_id, '_my_affiliate_url', stripslashes( $variable_custom_field[$i] ) );
}
endfor;
endif;
}
//front-end variations
function woocommerce_variable_add_to_cart() {
global $product, $post;
$variations = $product->get_available_variations();
foreach ($variations as $key => $value) {
?>
<form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>"method="post" enctype='multipart/form-data'>
<input type="hidden" name="variation_id" value="<?php echo $value['variation_id']?>" />
<input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
<?php
if(!empty($value['attributes'])){
foreach ($value['attributes'] as $attr_key => $attr_value) {
?>
<input type="hidden" name="<?php echo $attr_key?>" value="<?php echo $attr_value?>">
<?php
}
}
?>
<table>
<tbody>
<tr>
<td>
<b><?php echo implode('/', $value['attributes']);?></b>
</td>
<td>
<?php echo $value['price_html'];?>
</td>
<td>
<a class="single_add_to_cart_button button alt" target="_blank" href="<?php echo get_post_meta($value['variation_id'], '_my_affiliate_url', true); ?>" ><?php echo apply_filters('single_add_to_cart_text', __( 'Add to cart', 'woocommerce' ), $product->product_type); ?></a>
</td>
</tr>
</tbody>
</table>
</form>
<?php
}
}
上面的代码似乎在variant.php中覆盖了此脚本
defined( 'ABSPATH' ) || exit;
?>
<script type="text/template" id="tmpl-variation-template">
<div class="woocommerce-variation-description">{{{ data.variation.variation_description }}}</div>
<div class="woocommerce-variation-price">{{{ data.variation.price_html }}}</div>
<div class="woocommerce-variation-custom_field">{{{ data.variation.custom_field}}}</div>
<div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>
</script>
<script type="text/template" id="tmpl-unavailable-variation-template">
<p><?php esc_html_e( 'Sorry, this product is unavailable. Please choose a different combination.', 'woocommerce' ); ?></p>
</script>
我已经找到了functions.php的这段代码,这使我步入了自己想要的位置。我在这里使用的代码在Woocommerce变体网站中添加会员链接,并附带...