将外部产品 URL 覆盖为 "Add to Cart" 产品按钮

Override External Product URL to "Add to Cart" product button

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

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 );

但是这个功能不能将产品添加到购物车。

如何实现将所选产品添加到购物车的功能?

谢谢。

我修好了。对于外部产品,要用其他通用文本替换默认 "Buy This Product",请将此函数添加到 functions.php 文件到主题中:

add_filter( 'woocommerce_product_add_to_cart_text' , 
'wpf_custom_add_cart_text_archive',11);


function wpf_custom_add_cart_text_archive() {

   global $product;


   $product_type = $product->product_type;


   switch ( $product_type ) {

          case 'external':

                 return __( 'Add to Cart', 'woocommerce' );

                 break;

          case 'grouped':

                 return __( 'View products', 'woocommerce' );

                 break;

          case 'simple':

                 return __( 'Add to cart', 'woocommerce' );

                 break;

          case 'variable':

                 return __( 'Select options', 'woocommerce' );

                 break;

          default:

                 return __( 'Read more', 'woocommerce' );

   }

}

   add_filter( 'woocommerce_product_single_add_to_cart_text', 
'wpf_custom_add_cart_text',11);

还有这个:

function wpf_custom_add_cart_text() {

  return __( 'Add to Cart', 'woocommerce' );

 }

替换所有文本。

Use instead.

您应该尝试使用 woocommerce_product_add_to_cart_url 过滤器挂钩来更改添加到购物车 link(此处用于分组产品),这样:

add_filter( 'woocommerce_product_add_to_cart_url', 'override_external_product_url', 10, 2 );
function override_external_product_url( $url, $product ){
    if ( 'external' === $product->get_type() ) {
        //Get product ID -- WooCommerce compatibility
        if ( method_exists( $product, 'get_id' ) ) {
            $product_id = $product->get_id();
        } else {
            $product_id = $product->id;
        }
        // custom add to cart url example
        $url = home_url( "/product/?add-to-cart=$product_id");
    }
    return $url;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

Update: But this will not add to cart this external product before redirecting to an external url even if it works displaying the add-to-cart url (as add-to-cart is ajax driven).

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

如果是简单产品且不为空,请保存自定义字段数据:

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.

替换商店页面和存档页面中的添加到购物车按钮(对于具有自定义外部 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)

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

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

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

此代码已经过测试并适用于 WooCommerce 版本 3+