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

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

问题描述

我在使用亚马逊外部产品的网站上工作,但希望将用户指向该外部 URL,首先将该产品添加到购物车.我有这个功能,将每个产品的默认按钮文本更改为添加到购物车.

function sv_wc_external_product_button( $button_text, $product ) {if ('外部' === $product->get_type() ) {//输入外部产品的默认文本返回 $product->button_text ?$product->button_text : '加入购物车';}返回 $button_text;}add_filter('woocommerce_product_single_add_to_cart_text','sv_wc_external_product_button', 10, 2 );

但此功能不会将产品添加到购物车.

如何使用此功能将所选产品添加到购物车?

谢谢.

解决方案

2020 年更新

<块引用>

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

<块引用>

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

<块引用>

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

<块引用>

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

add_action('woocommerce_product_options_general_product_data', 'simple_product_with_external_url');函数 simple_product_with_external_url() {全球 $product_object;echo '<div class="options_group show_if_simple hidden">';//外部网址woocommerce_wp_text_input(数组('id' =>'_ext_url_cust','标签' =>'外部网址','说明' =>'自定义外部 URL','desc_tip' =>'真的','占位符' =>'在此处输入您的自定义外部 URL') );回声'</div>';}

如果它是一个简单的产品并且不为空,则保存自定义字段数据:

add_action('woocommerce_admin_process_product_object', 'save_simple_product_with_external_url');函数 save_simple_product_with_external_url( $product ) {if( $product->is_type('simple') && isset($_POST['_ext_url_cust']) ) {$product->update_meta_data('_ext_url_cust', sanitize_url($_POST['_ext_url_cust']));}}

<块引用>

2) 如果我们没有在 WooCommerce 中设置将产品添加到购物车时的购物车重定向,这将不适用于商店页面和档案页面.

<块引用>

因此,我们将在商店页面和存档页面上替换添加到购物车"按钮(仅针对我们带有自定义链接重定向的简单产品),将自定义按钮链接到单个产品页面.>

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

add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );函数quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {$external_url = $product->get_meta('_ext_url_cust');如果(!空($external_url)){$html = sprintf('<a href="%s" class="button alt add_to_cart_button">%s</a>', $product->get_permalink(), __("阅读更多",woocommerce"));}返回 $html;}

<块引用>

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

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

add_filter('woocommerce_add_to_cart_redirect', 'redirect_simple_product_with_external_url');函数redirect_simple_product_with_external_url( $url ) {if( isset($_REQUEST['add-to-cart']) && absint( $_REQUEST['add-to-cart'] ) > 0 )返回 get_post_meta( absint( $_REQUEST['add-to-cart'] ), '_ext_url_cust', true );返回 $url;}

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

此代码经过测试,可在 WooCommerce 版本 3+ 上运行

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?

Thanks.

解决方案

Updated 2020

This is a complete different way with simple products and a custom field external link.

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

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

Add a custom field on general product option settings for simple products only :

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

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

    // 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>';
}

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

add_action( 'woocommerce_admin_process_product_object', 'save_simple_product_with_external_url' );
function save_simple_product_with_external_url( $product ) {
    if( $product->is_type('simple') && isset($_POST['_ext_url_cust']) ) {
        $product->update_meta_data( '_ext_url_cust', sanitize_url($_POST['_ext_url_cust']) );
    }
}

2) This will not work on shop pages and archives pages, if we don't set in WooCommerce the cart redirection when adding a product to cart.

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 ) {
    $external_url = $product->get_meta('_ext_url_cust');

    if ( ! empty($external_url) ) {
        $html = sprintf( '<a href="%s" class="button alt add_to_cart_button">%s</a>', $product->get_permalink(), __("Read More", "woocommerce") );
    }
    return $html;
}

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)

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' );
function redirect_simple_product_with_external_url( $url ) {
    if( isset($_REQUEST['add-to-cart']) && absint( $_REQUEST['add-to-cart'] ) > 0 )
        return get_post_meta( absint( $_REQUEST['add-to-cart'] ), '_ext_url_cust', true );

    return $url;
}

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

This code is tested and works on WooCommerce version 3+

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

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