小编war*_*ape的帖子

在 WooCommerce 中相关产品的同一类别之前显示交叉销售

我正在编写一些代码来修改相关产品部分,如下所示:

  • 如果产品有交叉销售产品,首先显示那些产品,然后与其他同类产品一起填写最多 4 个产品*

或者

  • 如果产品没有交叉销售产品,则显示来自同一类别的 4 个产品*

到目前为止,这是我过滤相关产品的功能:

add_filter( 'woocommerce_related_products', 'fivem_add_linked_to_related_products', 9999, 3 );
function fivem_add_linked_to_related_products( $related_posts, $product_id, $args ) {

    $product = wc_get_product( $product_id );
    $cross_sell_ids = $product->get_cross_sell_ids();
    $product_categories = $product->get_category_ids();

    // Get cross sell products
    $cross_sell_products = get_posts( array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'fields' => 'ids',
        'post__in' => $cross_sell_ids,
        'posts_per_page' => 4,
        'exclude' => array( $product_id ),
    ));

    // Calculate how many filler products are needed
    $category_product_count = 4 - count( $cross_sell_products );

    // Exclude …
Run Code Online (Sandbox Code Playgroud)

php wordpress product categories woocommerce

5
推荐指数
1
解决办法
543
查看次数

在 WooCommerce 的电子邮件和订单页面中保存和显示产品自定义数据

我正在使用一些代码来添加自定义字段,以使我的客户能够自定义他们的产品。

这是我当前的代码:

/* Add meta fields to all products */
/* SRC: https://wisdmlabs.com/blog/add-custom-data-woocommerce-order-2/ */

/* Step 1: Adding Custom Field for Product */
add_action('woocommerce_before_add_to_cart_button','rs_add_custom_fields');
/**
 * Adds custom field for Product
 * @return [type] [description]
 */
function rs_add_custom_fields()
{

    global $product;

    ob_start();

    ?>

        <div class="wdm-custom-fields">
            <input type="text" placeholder="neutral_bag_count" name="neutral_bag_count">
            <input type="text" placeholder="bright_bag_count" name="bright_bag_count">
            <input type="text" placeholder="pastel_bag_count" name="pastel_bag_count">
            <input type="text" placeholder="reg_tendon_count" name="reg_tendon_count">
            <input type="text" placeholder="gid_tendon_count" name="gid_tendon_count">
        </div>
        <div class="clear"></div>

    <?php

    $content = ob_get_contents();
    ob_end_flush();

    return $content;
}

/* Step 2: Add …
Run Code Online (Sandbox Code Playgroud)

php wordpress custom-fields orders woocommerce

3
推荐指数
1
解决办法
2215
查看次数

从 Firebase.auth() 捕获错误响应以显示给用户

我正在使用 React Native/Firebase/Redux 构建一个简单的登录系统。我正在尝试找出如何捕获由于登录尝试失败而发生的错误。

这是我的 authscreen.js:

const [alertShowing, setAlertShowing] = useState(false);
const [alertMessage, setAlertMessage] = useState('');
...

  function handleLogin() {
    const response = dispatch(login(email, password));
    console.log(response);
  }


Run Code Online (Sandbox Code Playgroud)

动作.js:

export const login = (email, password) => {
  return async (dispatch) => {
    try {
      const response = await Firebase.auth().signInWithEmailAndPassword(email, password);
      dispatch(getUser(response.user.uid));
    } catch (e) {
        return e;
    }
  };
};
Run Code Online (Sandbox Code Playgroud)

上面的 console.log(response) 正确地向我显示了错误消息,但这显然对用户来说不是很有用。另请注意,使用正确的凭据我可以正确登录。

我真正想做的handleLogin()是检查响应是否是错误,如果是,setlAlertShowing(true)以及setAlertMessage我从 useDispatch 挂钩返回的内容,以便我可以很好地向用户显示它。

我该怎么办?TIA。

javascript react-native firebase-authentication react-redux

0
推荐指数
1
解决办法
3013
查看次数