且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将外部产品网址覆盖为“添加到购物车"产品按钮

更新时间:2023-11-04 18:07:58

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

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

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