将附属链接添加到 Woocommerce 变体 [英] Adding affiliate links to Woocommerce variations

查看:28
本文介绍了将附属链接添加到 Woocommerce 变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向 Woocommerce 变体添加附属链接.这个想法是为每个产品变体都有一个唯一的 http 链接/URL(附属链接).我可以在 Woocommerce 后端输入的链接/网址,当客户点击添加到购物车"按钮时,会加载一个新网页(基于该产品的相应网址)

对于没有变化的产品,这很容易实现.但是没有这样的开箱即用功能可以为具有变化的产品实现相同的目标.

我在这里找到了一个解决方案我实现了代码,但是每当我尝试在后端输入 URL 并尝试保存更改时,链接就会消失,不知道我的代码出了什么问题.

//显示字段add_action('woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2);//JS 为新变体添加字段add_action('woocommerce_product_after_variable_attributes_js', 'variable_fields_js');//保存字段add_action('woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1);函数变量字段( $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];?>"/>

</td></tr><?php}函数 variable_fields_js() {?><tr><td><div><label><?php _e('我的自定义字段', 'woocommerce');?></label><input type="text" size="5" name="my_affiliate_url[' + loop + ']"/>

</td></tr><?php}函数 variable_fields_process( $post_id ) {如果 (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];如果 ( isset( $variable_custom_field[$i] ) ) {update_post_meta( $variation_id, '_my_affiliate_url', stripslashes( $variable_custom_field[$i] ) );}结束;万一;}//前端变体函数 woocommerce_variable_add_to_cart() {全球 $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 ); ?>"/><?phpif(!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}}?><表格><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 按钮 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', __( '加入购物车', 'woocommerce' ), $product->product_type);?></a></td></tr></tbody></表单><?php}}

解决方案

已经有一段时间了,所以您可能已经解决了这个问题.但未来任何像我一样来到这里寻找答案的人.

替换:

add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1 );

与:

add_action( 'woocommerce_save_product_variation', 'variable_fields_process', 10, 2 );

自 WooCommerce 2.4.4 起更改了保存变体

I am trying to add affiliate links to the Woocommerce variations. The idea is to have a unique http link/URL(affiliate link) for each product variation. A link/URL that I can enter in the Woocommerce backend and when a customer clicks on the 'Add to cart' button, a new web-page is loaded (based on the corresponding URL of that product)

This can be easily achieved for the products with no variations. But there is no such out of the box functionality for achieving the same thing for the products with variations.

I came across a solution over here I implemented the code but whenever I try to enter the URL in the backend and try to save my changes, the link disappears, don't know what's going wrong with my code.

    // 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, 1 );

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
        }
}

解决方案

It's been a while, so you may have resolved this. But anyone in the future who comes here looking for an answer like I did.

Replace:

add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1 );

with:

add_action( 'woocommerce_save_product_variation', 'variable_fields_process', 10, 2 );

Saving variants changed as of WooCommerce 2.4.4

这篇关于将附属链接添加到 Woocommerce 变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆