本文介绍了woocommerce_loop_add_to_cart_link过滤器挂钩如何深入工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Woocommerce基本问题,无法在网上找到任何地方.

I have some basic Woocommerce questions and cannot find anywhere online.

  • 从下面的代码中,$ args来自哪里?
  • $ product和$ args如何分配给%s?

谢谢!

global $product;

echo apply_filters( 'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
    sprintf( '<a href="%s" data-quantity="%s" class="%s" %s>%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
        esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
        isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
        esc_html( $product->add_to_cart_text() )
    ),
$product, $args );

推荐答案

1)模板文件loop/add_to_cart.php中的$ args变量的解释:

1) Explanations for $args variable in template file loop/add_to_cart.php:

您问题中的代码来自模板文件 loop/add_to_cart.php .
它由 content-product.php 模板调用 woocommerce_after_shop_loop_item 挂钩上的文件:

The code in your question come from the template file loop/add_to_cart.php.
It is called by content-product.php template file on woocommerce_after_shop_loop_item hook:

/**
 * Hook: woocommerce_after_shop_loop_item.
 *
 * @hooked woocommerce_template_loop_product_link_close - 5
 * @hooked woocommerce_template_loop_add_to_cart - 10
 */
do_action( 'woocommerce_after_shop_loop_item' );

如您所见,模板函数 woocommerce_template_loop_add_to_cart() 可以完成这项工作,它位于WooCommerce插件中的includes/wc-template-functions.php下.

As you can see the template function woocommerce_template_loop_add_to_cart() does this job and it is located in WooCommerce plugin under includes/wc-template-functions.php.

因此默认参数为:

$defaults = array(
    'quantity'   => 1,
    'class'      => implode(
        ' ',
        array_filter(
            array(
                'button',
                'product_type_' . $product->get_type(),
                $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
                $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
            )
        )
    ),
    'attributes' => array(
        'data-product_id'  => $product->get_id(),
        'data-product_sku' => $product->get_sku(),
        'aria-label'       => $product->add_to_cart_description(),
        'rel'              => 'nofollow',
    ),
);

它们通过过滤器挂钩woocommerce_loop_add_to_cart_args进行解析:

They are parsed through the filter hook woocommerce_loop_add_to_cart_args:

$args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );

...允许对此参数进行更改.

… allowing to make changes on this arguments.


2)如何将$product$args分配给%s :


2) How $product and $args are assigned to %s:

这特定于PHP printf()sprintf()函数,其中每个%s是占位符.

This is specific to PHP printf() and sprintf() function where each %s are a placeholder.

'<a href="%s" data-quantity="%s" class="%s" %s>%s</a>'中:

  • 第一个占位符%s将替换为esc_url( $product->add_to_cart_url() )
  • 第二个占位符%s,由esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 )
  • and so on…
  • the 1st placeholder %s will be replaced by esc_url( $product->add_to_cart_url() )
  • the 2nd placeholder %s by esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 )
  • and so on…

printf() sprintf() php函数.

Documentation for printf() and sprintf() php functions.

这篇关于woocommerce_loop_add_to_cart_link过滤器挂钩如何深入工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 12:40