将外部产品网址覆盖为“添加到购物车"产品按钮 [英] Override External Product URL to "Add to Cart" product button

查看:100
本文介绍了将外部产品网址覆盖为“添加到购物车"产品按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Amazon外部产品的站点上工作,但希望将用户指向该外部URL,首先将其添加到购物车中.我有此功能,可以将每个产品的默认按钮"文本更改为添加到购物车".

I work on site that use External products from Amazon, but want instead pointing users to that external URL, first to add to cart that product. I have this function, that change Default Button text for each product, to Add to cart.

function sv_wc_external_product_button( $button_text, $product ) {

    if ( 'external' === $product->get_type() ) {
        // enter the default text for external products
        return $product->button_text ? $product->button_text : 'Add To Cart';
    }
    return $button_text;
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 
'sv_wc_external_product_button', 10, 2 );

但是此功能未将产品添加到购物车.

But this function not add product to cart.

如何使此功能添加到购物车中?

How to make this function to Add selected product to cart?

谢谢.

推荐答案

通过简单的产品和自定义字段外部链接,这是一种完全不同的方式.

在此答案中,我们将使用简单的产品而不是外部产品.

In this answer we will use simple products instead of external products.

1)我们在产品选项设置中添加了一个外部URL"自定义字段,并保存了数据.

1) We add an "External URL" custom field in product option settings and we save the data.

在简单的产品常规选项设置中添加自定义字段:

Add a custom field in simple product general option settings:

add_action( 'woocommerce_product_options_general_product_data', 'simple_product_with_external_url' );
function simple_product_with_external_url() {
    global $woocommerce, $post;

    // only WC products
    if( $post->post_type != 'product' )
        return;

    // Get an instance of WC_Product object
    $product = wc_get_product( $post->ID );

    // Only for simple products
    if ( 'simple' == $product->get_type() ):

        echo '<div class="options_group">';

        // External Url
        woocommerce_wp_text_input(
            array(
                'id'          => '_ext_url_cust',
                'label'       => 'External Url',
                'description' => 'Custom external URL',
                'desc_tip'    => 'true',
                'placeholder' => 'Enter here your custom external URL'
            )
        );

        echo '</div>';

    endif;
}

保存自定义字段数据(如果它是简单商品并且不为空):

Save the custom field data if it's a simple product and not empty:

add_action( 'woocommerce_process_product_meta', 'save_simple_product_with_external_url' , 10, 1);
function save_simple_product_with_external_url( $post_id ) {

    // Get an instance of WC_Product object
    $product = wc_get_product( $post_id );

    $external_url = $_POST['_ext_url_cust'];

    // Save data if it's not empty and only a simple product
    if( !empty( $external_url ) && 'simple' == $product->get_type() )
        update_post_meta( $post_id, '_ext_url_cust', $external_url );
}

2)如果在将商品添加到购物车时未在WooCommerce中设置购物车重定向,这将不适用于商店页面和存档页面.

因此,我们将商店页面和存档页面上的购物车按钮(仅适用于我们的简单产品,带有自定义链接重定向),而将链接的自定义按钮替换为单个产品页面. >

So we will replace add-to-cart button (just for our simple products with a custom link redirection) on shop pages and archives pages by a linked custom button to single product pages.

替换商店页面和档案页面中的购物车按钮(对于具有自定义外部网址的简单产品):

Replacing add-to-cart button in shop pages and archives pages (for simple products with custom external url):

add_filter( 'woocommerce_loop_add_to_cart_link',
'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );

function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {

    if ( method_exists( $product, 'get_id' ) ) {
        $product_id = $product->get_id();
    } else {
        $product_id = $product->id;
    }

    $external_url = get_post_meta( $product_id, '_ext_url_cust', true );

    if ( ! empty( $external_url ) ) {
        // Set HERE your button link
        $link = get_permalink($product_id);
        $html = '<a href="'.$link.'" class="button alt add_to_cart_button">'.__("Read More", "woocommerce").'</a>';
    }
    return $html;
}

3)如果自定义字段值不为空,则将产品首先添加到购物车,然后重定向到外部URL (我们在单个产品页面中的自定义字段值)

3) If the custom field value is not empty, the product is added to cart first and then redirected to the external URL (our custom field value in single product pages)

添加到购物车后的外部URL重定向(当简单产品中的自定义字段不为空时):

External URL redirection after adding to cart (when custom field is not empty in simple products):

add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_simple_product_with_external_url', 10, 1 );
function redirect_simple_product_with_external_url( $url ) {

    $product_id = absint( $_REQUEST['add-to-cart'] );

    $external_url = get_post_meta( $product_id, '_ext_url_cust', true );
    $product = wc_get_product( $product_id );

    if ( 'simple' == $product->get_type() && ! empty( $external_url ) )
        $url = $external_url;

    return $url;
}

代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

此代码已经过测试,可在WooCommerce 2.6.x版上运行

This code is tested and works on WooCommerce version 2.6.x

这篇关于将外部产品网址覆盖为“添加到购物车"产品按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆