gpt4 book ai didi

php - 在可变的单一产品页面中自定义添加到购物车按钮/链接

转载 作者:行者123 更新时间:2023-12-03 22:54:48 31 4
gpt4 key购买 nike

在 WooCommerce 3.0+ 中,我使用 js 创建了一些选项卡,每个选项卡中包含来自不同类别的产品。我已经设法修改了简单产品的添加到购物车链接,其中点击了 addtocart 按钮它进入下一个选项卡而不刷新并且产品被成功添加到购物车。

if ( has_term( 'jeans-discount', 'product_cat', $post ) ) {
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '<a rel="nofollow" data-target="2" href="javascript:void(0); %s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="custom %s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
),
$product );
}

但是我无法在 variation-add-to-cart.php 中修改可变产品的 addtocart 按钮模板文件:

<button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>

对于简单的产品,addtocart 链接显示为 href="javascript:void(0);/wordpress/woo-slider/?add-to-cart=73"

有没有办法对可变产品 addtocart 链接也执行此操作?

最佳答案

对于 WooCommerce 3.0+ 版,您将以这种方式覆盖 variation-add-to-cart.php 模板:

<?php
/**
* Single variation cart button
*
* @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 3.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

global $product;

?>
<div class="woocommerce-variation-add-to-cart variations_button">
<?php
/**
* @since 3.0.0.
*/
do_action( 'woocommerce_before_add_to_cart_quantity' );

woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),
) );

/**
* @since 3.0.0.
*/
do_action( 'woocommerce_after_add_to_cart_quantity' );

// Set HERE your targeted product category
if ( has_term( 'jeans-discount', 'product_cat', $product->get_id() ) ) {

?>
<button type="submit" data-target="2" class="single_add_to_cart_button button alt" onclick="javascript:void(0);"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>

<?php } else { // Other product categories
?>

<button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>

<?php } ?>

<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="0" />
</div>

我无法对其进行真实测试(因为我没有启用选项卡系统),但这在我的测试服务器上成功运行,没有出现错误问题。


对于 WooCommerce 2.6.x,您将以这种方式覆盖 variation-add-to-cart.php 模板:

<?php
/**
* Single variation cart button
*
* @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

global $product;

// Set HERE your targeted product category
if ( has_term( 'jeans-discount', 'product_cat', $product->id ) ) {

?>
<div class="woocommerce-variation-add-to-cart variations_button">
<?php if ( ! $product->is_sold_individually() ) : ?>
<?php woocommerce_quantity_input( array( 'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : 1 ) ); ?>
<?php endif; ?>
<button type="submit" data-target="2" class="single_add_to_cart_button button alt" onclick="javascript:void(0);"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>

<?php } else { // Other product categories
?>

<div class="woocommerce-variation-add-to-cart variations_button">
<?php if ( ! $product->is_sold_individually() ) : ?>
<?php woocommerce_quantity_input( array( 'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : 1 ) ); ?>
<?php endif; ?>
<button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>

<?php } ?>

<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->id ); ?>" />
<input type="hidden" name="product_id" value="<?php echo absint( $product->id ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="0" />
</div>

关于php - 在可变的单一产品页面中自定义添加到购物车按钮/链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43322538/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com