WooCommerce 存档页面上的动态数据问题 - 用产品查询按钮替换“添加到购物车”

问题描述 投票:0回答:1

我想用产品查询按钮替换添加到购物车按钮,以便访问者可以看到产品查询按钮而不是添加到购物车按钮。我正在使用 Elementor 表单在存档页面的弹出窗口中显示动态数据,例如产品图像和标题。该表单在单个产品页面上效果很好,但在存档页面上效果不佳。默认只显示第一个产品名称和图片。

我使用以下代码删除按钮并打开弹出窗口,但无法获取数据。

add_action('wp_enqueue_scripts', 'hello_elementor_child_scripts_styles', 20);

// Remove the "Add to Cart" button
add_action('woocommerce_before_shop_loop_item_title', 'remove_add_to_cart_button', 10);

function remove_add_to_cart_button() {
    remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
}

// Add the "Get Price" button
add_action('woocommerce_after_shop_loop_item', 'add_get_price_button', 11);

function add_get_price_button($product) {
    if (is_a($product, 'WC_Product')) {
        $product_id = $product->get_id();
        $product_title = $product->get_title();
        $product_description = $product->get_description();
        $product_url = $product->get_permalink();

        echo '<a href="#elementor-action%3Aaction%3Dpopup%3Aopen%26settings%3DeyJpZCI6IjM4MiIsInRvdGdsZSI6ZmFsc2V9&product_id=' . esc_attr($product_id) . '" class="button get_price_button" data-product-id="' . esc_attr($product_id) . '" data-product-title="' . esc_attr($product_title) . '" data-product-description="' . esc_attr($product_description) . '" data-elementor-open-lightbox="yes" data-elementor-lightbox-id="382">Get Price</a>';
    }
}
php wordpress woocommerce product elementor
1个回答
0
投票

$product
钩子中没有可用的
woocommerce_after_shop_loop_item
变量参数…请尝试以下修改后的代码:

add_action('wp_enqueue_scripts', 'hello_elementor_child_scripts_styles', 20);

// Remove the "Add to Cart" button
add_action('woocommerce_before_shop_loop_item_title', 'remove_add_to_cart_button', 10);
function remove_add_to_cart_button() {
    remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
}

// Add the "Get Price" button
add_action('woocommerce_after_shop_loop_item', 'add_get_price_button', 11);
function add_get_price_button() {
    global $product;
    
    if ( ! is_a($product, 'WC_Product') ) {
        $product = wc_get_product( get_the_ID() );
    }

    if ( is_a($product, 'WC_Product') ) {
        $product_id = $product->get_id();
        $product_title = $product->get_title();
        $product_description = $product->get_description();
        $product_url = $product->get_permalink();

        echo '<a href="#elementor-action%3Aaction%3Dpopup%3Aopen%26settings%3DeyJpZCI6IjM4MiIsInRvdGdsZSI6ZmFsc2V9&product_id=' . esc_attr($product_id) . '" class="button get_price_button" data-product-id="' . esc_attr($product_id) . '" data-product-title="' . esc_attr($product_title) . '" data-product-description="' . esc_attr($product_description) . '" data-elementor-open-lightbox="yes" data-elementor-lightbox-id="382">Get Price</a>';
    }
}

应该可以。

© www.soinside.com 2019 - 2024. All rights reserved.